XGBoost and PyTorch
PyTorch supplies tensors, automatic differentiation, and neural-network training; it does not provide the XGBoost tree learner. Moving tabular rows through a DataLoader before converting them back to NumPy does not improve XGBoost and can silently lose row order or schema.
Why the previous example was removed
The former page fetched a third-party dataset, reconstructed shuffled arrays from a loader, evaluated only hard-label accuracy, and then repeated the same conversion for tuning. PyTorch performed no learned operation. The extra boundary obscured rather than demonstrated a valid integration.
A defensible hybrid
PyTorch can learn embeddings from images, text, audio, or another differentiable model. After choosing that encoder using training-only validation:
- switch it to evaluation mode and generate embeddings under
torch.no_grad(); - preserve row identifiers and the original train/validation/test or group/time partitions;
- train XGBoost on training embeddings and use validation embeddings for early stopping;
- evaluate once on test embeddings;
- repeat encoder fitting inside the outer fold if its hyperparameters are part of the comparison.
For stacking, meta-model training needs out-of-fold predictions. In-sample predictions from an XGBoost model are target-leaking features even if stored as PyTorch tensors.
Device and serialization boundaries
XGBoost accepts several array interfaces, but device compatibility depends on the exact build and configuration. An explicit CPU NumPy boundary is the most portable; GPU interoperability must be benchmarked and tested on the deployment platform. Persist the encoder weights, preprocessing, embedding schema, XGBoost model, and exact package versions together.
Takeaway
Use PyTorch only when it contributes learned features or a separate validated stage. For ordinary tabular XGBoost, the direct scikit-learn-compatible workflow is simpler and less error-prone.