Skip to main content

LightGBM and PyTorch

PyTorch does not train or fine-tune a LightGBM tree ensemble. Converting predictions into tensors and fitting a neural network to them creates a second model; it does not form an end-to-end differentiable LightGBM pipeline.

Why the previous example was removed​

The former page fetched data from the network, standardized every feature for a tree model, converted LightGBM test predictions into a one-dimensional PyTorch input, and trained a neural network on that same test partition. This reused evaluation labels for fitting and made the reported score invalid.

A valid feature pipeline​

Use PyTorch to learn representations only when the raw modality calls for it. Freeze the selected encoder, call eval(), extract features under torch.no_grad(), and preserve the split boundary and row identity. Fit LightGBM on training embeddings, stop on validation embeddings, and evaluate on test embeddings once.

When encoder choices are tuned, the encoder must be fitted again inside each outer training fold. Cached embeddings from a model trained on all observations leak validation information even when LightGBM itself sees only a fold.

A valid ensemble pipeline​

For stacking, generate LightGBM and PyTorch base predictions out of fold. Fit the meta-model on those predictions, then refit base models on the full training set and evaluate the complete system on the isolated test set. Compare with each base model using identical splits, metrics, and compute reporting.

Operational considerations​

Keep preprocessing, category mapping, encoder weights, embedding schema, LightGBM model, and exact runtime versions together. CPU NumPy arrays are a portable interface; zero-copy or GPU paths are version- and platform-sensitive and need their own tests.

Takeaway​

For ordinary tabular data, call LightGBM directly. Add PyTorch only when it contributes a separately validated representation or prediction stage.