Skip to main content

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:

  1. Reset model weights before each fold
  2. Train and test each fold independently
  3. Save a checkpoint per fold
  4. Build an ensemble that averages logits from all folds
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lightning_kfold-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lightning_kfold-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

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

Hashes for lightning_kfold-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ebd6100c8008fd6fee647416a5e4584568ec6855219a0682ab1b898245c264ee
MD5 e3bfac231d70f68c9db24be94bff4bb8
BLAKE2b-256 fe8501c0263c5d31dd62a92590fe8367f6617aa1ada5c93f1aba445126029f1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for lightning_kfold-0.1.0.tar.gz:

Publisher: release.yml on urmzd/lightning-kfold

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lightning_kfold-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lightning_kfold-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67915536ed51f833d2cf9a27511dbe7572ae864908a0bb5c20e74b773943daaa
MD5 67dcf10ff86ce32a08a68360d996c815
BLAKE2b-256 6fc5c3144e792ec446edbdd4bdffe5405bccd6a49a87e2450a92b2710da9a886

See more details on using hashes here.

Provenance

The following attestation bundles were made for lightning_kfold-0.1.0-py3-none-any.whl:

Publisher: release.yml on urmzd/lightning-kfold

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page