Data Datamodule Kfold#
datamodule_kfold
#
| CLASS | DESCRIPTION |
|---|---|
DataModuleWeightedKFold |
PyTorch Lightning DataModule with k-fold cross-validation support. |
Classes#
DataModuleWeightedKFold(dataset: DatasetWeightedType | dict, n_splits: int = 5, batch_size: int = 1000, shuffle: bool = True, stratify: bool = False, stratify_batches: bool = False, random_seed: int | None = None)
#
Bases: LightningDataModule
PyTorch Lightning DataModule with k-fold cross-validation support.
Provides stratified or random k-fold splitting for cross-validation workflows. Integrates with PyTorch Lightning Trainer for automated CV training.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
Dataset instance or dictionary with "X" key (required) and optional "weights", "target" keys.
TYPE:
|
n_splits
|
Number of folds for cross-validation.
TYPE:
|
batch_size
|
Batch size for all dataloaders.
TYPE:
|
shuffle
|
Whether to shuffle data before splitting into folds.
TYPE:
|
stratify
|
Whether to use stratified splitting for folds (requires targets).
TYPE:
|
stratify_batches
|
Whether to use stratified sampling within each batch (requires targets). This is independent of fold stratification.
TYPE:
|
random_seed
|
Random seed for reproducible splits.
TYPE:
|
Properties
dataset : DatasetWeighted The underlying dataset.
n_splits : int Number of folds.
current_fold : int Current fold index (0 to n_splits-1).
batch_size : int Batch size for dataloaders.
shuffle : bool Whether data is shuffled before splitting.
stratify : bool Whether stratified splitting is used for folds.
stratify_batches : bool Whether stratified sampling is used within batches.
random_seed : int | None Random seed for reproducibility.
Examples:
Basic k-fold cross-validation:
>>> import torch
>>> from spectre.data import DatasetWeighted, DataModuleWeightedKFold
>>> import pytorch_lightning as pl
>>> X = torch.randn(1000, 20)
>>> dataset = DatasetWeighted(X)
>>> kfold_dm = DataModuleWeightedKFold(dataset, n_splits=5, batch_size=128)
>>> # Train on each fold
>>> for fold in range(5):
... kfold_dm.set_fold(fold)
... kfold_dm.setup()
... trainer = pl.Trainer(max_epochs=10)
... # trainer.fit(model, kfold_dm)
Stratified k-fold for classification:
>>> X = torch.randn(1000, 20)
>>> targets = torch.randint(0, 3, (1000,))
>>> dataset = DatasetWeighted(X, target=targets)
>>> kfold_dm = DataModuleWeightedKFold(
... dataset,
... n_splits=5,
... stratified=True,
... random_seed=42,
... )
>>> for fold in range(5):
... kfold_dm.set_fold(fold)
... kfold_dm.setup()
... # Training on fold with balanced class distribution
Notes
- Stratified splitting requires dataset to have targets
- Call
set_fold()beforesetup()for each fold - Supports all PyTorch Lightning Trainer features
| METHOD | DESCRIPTION |
|---|---|
set_fold |
Set the current fold for training and validation. |
get_fold_indices |
Get train and validation indices for a specific fold. |
setup |
Setup train and validation datasets for current fold. |
train_dataloader |
Return dataloader for training dataset. |
val_dataloader |
Return dataloader for validation dataset. |
check_setup |
Check if the datamodule has been set up and raise error if not. |
Source code in spectre/data/datamodule_kfold.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
Functions#
set_fold(fold_idx: int) -> None
#
Set the current fold for training and validation.
| PARAMETER | DESCRIPTION |
|---|---|
fold_idx
|
Fold index (0 to n_splits-1).
TYPE:
|
Source code in spectre/data/datamodule_kfold.py
get_fold_indices(fold_idx: int) -> tuple[torch.Tensor, torch.Tensor]
#
Get train and validation indices for a specific fold.
| PARAMETER | DESCRIPTION |
|---|---|
fold_idx
|
Fold index (0 to n_splits-1).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
Train and validation indices. |
Source code in spectre/data/datamodule_kfold.py
setup(stage: str | None = None) -> None
#
Setup train and validation datasets for current fold.
| PARAMETER | DESCRIPTION |
|---|---|
stage
|
PyTorch Lightning stage (unused but required by interface).
TYPE:
|
Source code in spectre/data/datamodule_kfold.py
train_dataloader() -> DataLoaderWeighted
#
Return dataloader for training dataset.
Source code in spectre/data/datamodule_kfold.py
val_dataloader() -> DataLoaderWeighted
#
Return dataloader for validation dataset.
Source code in spectre/data/datamodule_kfold.py
check_setup() -> None
#
Check if the datamodule has been set up and raise error if not.