Data Datamodule#
datamodule
#
| CLASS | DESCRIPTION |
|---|---|
DataModuleWeighted |
PyTorch Lightning DataModule for weighted datasets with flexible splitting. |
Classes#
DataModuleWeighted(dataset: DatasetWeightedType | dict, batch_size: int = 1000, train_stride: int = 1, val: float = 0.2, use_random_split: bool = True, shuffle: bool = False, stratify: bool = False)
#
Bases: LightningDataModule
PyTorch Lightning DataModule for weighted datasets with flexible splitting.
Provides comprehensive data management for machine learning workflows with support for weighted datasets, optional targets, flexible train/validation splits, and optimized dataloader caching.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
Dataset instance or dictionary with "X" key (required) and optional "weights", "target" keys. Subset is not supported to avoid double indexing.
TYPE:
|
batch_size
|
Batch size for all dataloaders.
TYPE:
|
train_stride
|
Training stride for subsampling data (1 means no subsampling, all data used for training).
TYPE:
|
val
|
Validation split fraction in range [0.0, 1.0]. Set to 0.0 to disable validation.
TYPE:
|
use_random_split
|
Whether to use random data split (True) or sequential split (False).
TYPE:
|
shuffle
|
Whether to shuffle data in dataloaders.
TYPE:
|
stratify
|
Whether to stratify splits based on targets. Requires dataset to have targets.
TYPE:
|
Properties
dataset : DatasetWeighted The underlying dataset.
dataset_strided : Subset The strided (subsampled) dataset.
batch_size : int Current batch size.
val : float Validation split fraction.
use_random_split : bool Whether using random or sequential splitting.
shuffle : bool Whether dataloaders shuffle data.
train_stride : int Training stride for subsampling data.
train_dataloader : DataLoaderWeighted Dataloader for training dataset.
val_dataloader : DataLoaderWeighted | None Dataloader for validation dataset, or None if no validation split.
test_dataloader : DataLoaderWeighted Dataloader for testing on full dataset.
predict_dataloader : DataLoaderWeighted Dataloader for prediction on full dataset.
Examples:
Basic usage with DatasetWeighted:
>>> from spectre.data import DataModuleWeighted, DatasetWeighted
>>> import torch
>>> X = torch.randn(1000, 10)
>>> weights = torch.rand(1000)
>>> target = torch.randint(0, 2, (1000,))
>>> dataset = DatasetWeighted(X=X, weights=weights, target=target)
>>> datamodule = DataModuleWeighted(
... dataset=dataset,
... batch_size=128,
... train_stride=2,
... val=0.2,
... use_random_split=True,
... shuffle=True,
... )
>>> datamodule.setup()
>>> train_loader = datamodule.train_dataloader()
>>> val_loader = datamodule.val_dataloader()
>>> test_loader = datamodule.test_dataloader()
>>> predict_loader = datamodule.predict_dataloader()
Notes
- The
setupmethod must be called before accessing dataloaders to ensure proper dataset splitting.
| METHOD | DESCRIPTION |
|---|---|
setup |
Setup train and validation datasets with chosen splitting strategy. |
predict_dataloader |
Return dataloader for prediction on full dataset. |
train_dataloader |
Return dataloader for training dataset. |
val_dataloader |
Return dataloader for validation dataset, or None if no validation split. |
test_dataloader |
Return dataloader for testing on full dataset. |
check_setup |
Check if the datamodule has been set up and raise error if not. |
Source code in spectre/data/datamodule.py
115 116 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 193 194 195 | |
Functions#
setup(stage: str | None = None) -> None
#
Setup train and validation datasets with chosen splitting strategy.
Creates train and validation splits using either random or sequential splitting based on the validation fraction and splitting method specified during initialization.
| PARAMETER | DESCRIPTION |
|---|---|
stage
|
PyTorch Lightning stage (unused but required by interface).
TYPE:
|
Source code in spectre/data/datamodule.py
predict_dataloader() -> DataLoaderWeighted
#
Return dataloader for prediction on full dataset.
Source code in spectre/data/datamodule.py
train_dataloader() -> DataLoaderWeighted
#
Return dataloader for training dataset.
Source code in spectre/data/datamodule.py
val_dataloader() -> DataLoaderWeighted | None
#
Return dataloader for validation dataset, or None if no validation split.
Source code in spectre/data/datamodule.py
test_dataloader() -> DataLoaderWeighted
#
Return dataloader for testing on full dataset.
Source code in spectre/data/datamodule.py
check_setup() -> None
#
Check if the datamodule has been set up and raise error if not.