gbnet.lgbmodule
Classes
LightGBM Module that wraps LightGBM boosting into a PyTorch Module. |
|
Helper class for use with LightGBM as a backend for LGBModule |
Module Contents
- class gbnet.lgbmodule.LGBModule(batch_size, input_dim, output_dim, params={}, min_hess=0)[source]
Bases:
gbnet.base.BaseGBModuleLightGBM Module that wraps LightGBM boosting into a PyTorch Module.
This module allows integration of LightGBM gradient boosting with PyTorch neural networks. It maintains the boosting model state and handles both training and inference.
- Parameters:
batch_size (int) – Size of training data
input_dim (int) – Dimension of input features
output_dim (int) – Dimension of output predictions
params (dict, optional) – Parameters passed to LightGBM. Defaults to {}.
min_hess (float, optional) – Minimum hessian value submitted to LightGBM. Defaults to 0.
- Variables:
batch_size (int) – Size of mini-batches
input_dim (int) – Input feature dimension
output_dim (int) – Output prediction dimension
params (dict) – LightGBM parameters
bst (lightgbm.Booster) – The underlying LightGBM booster
FX (torch.nn.Parameter) – Current predictions tensor
train_dat (lightgbm.Dataset) – Training dataset used for caching
min_hess (float) – Minimum hessian threshold
- _input_checking_setting(input_dataset)[source]
- Parameters:
input_dataset (Union[lightgbm.Dataset, numpy.ndarray, pandas.DataFrame])
- forward(input_dataset, return_tensor=True)[source]
Forward pass through the LightGBM module.
- Parameters:
input_dataset (Union[lgb.Dataset, np.ndarray, pd.DataFrame]) – Input data for prediction. Can be a LightGBM Dataset, numpy array, or pandas DataFrame.
return_tensor (bool, optional) – Whether to return predictions as a PyTorch tensor. Defaults to True.
- Returns:
Model predictions. Returns a PyTorch tensor if return_tensor=True, otherwise returns a numpy array.
- Return type:
Union[torch.Tensor, np.ndarray]
The forward pass handles both train and eval - In train mode, maintains state between iterations and updates internal FX tensor - In eval mode, generates predictions on new data using the trained model
- gb_step()[source]
Performs a gradient boosting step to update the model.
This method: 1. Computes gradients and hessians from the current predictions 2. Creates a LightGBM objective using the computed gradients/hessians 3. Updates the internal boosting model by either:
Updating the existing model if one exists
Training a new model for 1 boosting round if no model exists
The gradients are scaled by batch size and hessians are clipped to a minimum value to ensure numerical stability.
- Returns:
None