Drop-in stratified K-fold cross-validation with ensemble voting for PyTorch Lightning.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lightning_kfold-0.1.0.tar.gz.
File metadata
- Download URL: lightning_kfold-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebd6100c8008fd6fee647416a5e4584568ec6855219a0682ab1b898245c264ee
|
|
| MD5 |
e3bfac231d70f68c9db24be94bff4bb8
|
|
| BLAKE2b-256 |
fe8501c0263c5d31dd62a92590fe8367f6617aa1ada5c93f1aba445126029f1e
|
Provenance
The following attestation bundles were made for lightning_kfold-0.1.0.tar.gz:
Publisher:
release.yml on urmzd/lightning-kfold
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lightning_kfold-0.1.0.tar.gz -
Subject digest:
ebd6100c8008fd6fee647416a5e4584568ec6855219a0682ab1b898245c264ee - Sigstore transparency entry: 1106624097
- Sigstore integration time:
-
Permalink:
urmzd/lightning-kfold@dfbe4124e8c8e5edb848254c8d90b6a2df8829fb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/urmzd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@dfbe4124e8c8e5edb848254c8d90b6a2df8829fb -
Trigger Event:
push
-
Statement type:
File details
Details for the file lightning_kfold-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lightning_kfold-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67915536ed51f833d2cf9a27511dbe7572ae864908a0bb5c20e74b773943daaa
|
|
| MD5 |
67dcf10ff86ce32a08a68360d996c815
|
|
| BLAKE2b-256 |
6fc5c3144e792ec446edbdd4bdffe5405bccd6a49a87e2450a92b2710da9a886
|
Provenance
The following attestation bundles were made for lightning_kfold-0.1.0-py3-none-any.whl:
Publisher:
release.yml on urmzd/lightning-kfold
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lightning_kfold-0.1.0-py3-none-any.whl -
Subject digest:
67915536ed51f833d2cf9a27511dbe7572ae864908a0bb5c20e74b773943daaa - Sigstore transparency entry: 1106624101
- Sigstore integration time:
-
Permalink:
urmzd/lightning-kfold@dfbe4124e8c8e5edb848254c8d90b6a2df8829fb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/urmzd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@dfbe4124e8c8e5edb848254c8d90b6a2df8829fb -
Trigger Event:
push
-
Statement type: