lightning-kfold
Drop-in stratified K-fold cross-validation with ensemble voting for PyTorch Lightning.
Install
uv add lightning-kfold
Usage
1. Define your data module
Subclass KFoldDataModule and implement setup_datasets:
import numpy as np
from torch.utils.data import TensorDataset
from lightning_kfold import KFoldDataModule
class MyDataModule(KFoldDataModule):
def setup_datasets(self):
# Return (train_dataset, test_dataset, train_labels)
train_ds = TensorDataset(train_x, train_y)
test_ds = TensorDataset(test_x, test_y)
labels = train_y.numpy() # 1-D integer array for stratification
return train_ds, test_ds, labels
2. Train with K-fold
from lightning_kfold import KFoldTrainer
model = MyLightningModel()
dm = MyDataModule(num_folds=5, batch_size=32)
dm.setup()
trainer = KFoldTrainer(num_folds=5, max_epochs=10)
ensemble_results = trainer.fit(model, dm)
That's it. KFoldTrainer will:
- Reset model weights before each fold
- Train and test each fold independently
- Save a checkpoint per fold
- Build an ensemble that averages logits from all folds
- Test the ensemble on the held-out test set
3. Use the ensemble directly
from lightning_kfold import EnsembleVotingModel
ensemble = EnsembleVotingModel(
model_cls=MyLightningModel,
checkpoint_paths=["kfold_checkpoints/fold-0.ckpt", ...],
)
# Use for inference
logits = ensemble(input_tensor)
API
| Class | Purpose |
|---|---|
KFoldDataModule |
Abstract data module with stratified splitting. Implement setup_datasets(). |
KFoldTrainer |
Orchestrates fold training, checkpointing, and ensemble creation. |
EnsembleVotingModel |
Loads K checkpoints and averages their logits at test time. |
License
Apache-2.0
Metadata
Release files for lightning-kfold 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| lightning_kfold-0.1.0.tar.gz | 3.5 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lightning_kfold-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.0 kB
Release files / lightning_kfold-0.1.0.tar.gz
| Download URL | lightning_kfold-0.1.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ebd6100c8008fd6fee647416a5e4584568ec6855219a0682ab1b898245c264ee
|
|
BLAKE2b-256 checksum How to use checksums |
fe8501c0263c5d31dd62a92590fe8367f6617aa1ada5c93f1aba445126029f1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 15, 2026.
Transparency logRelease files / lightning_kfold-0.1.0-py3-none-any.whl
| Download URL | lightning_kfold-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67915536ed51f833d2cf9a27511dbe7572ae864908a0bb5c20e74b773943daaa
|
|
BLAKE2b-256 checksum How to use checksums |
6fc5c3144e792ec446edbdd4bdffe5405bccd6a49a87e2450a92b2710da9a886
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 15, 2026.
Transparency logSecurity for lightning-kfold 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for lightning_kfold-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 15, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.