Data Dataloader Stratified#
dataloader_stratified
#
| CLASS | DESCRIPTION |
|---|---|
StratifiedSampler |
Sampler for stratified sampling across classes. |
StratifiedBatchSampler |
Batch sampler that yields stratified batches without materializing all indices. |
Classes#
StratifiedSampler(dataset: DatasetWeightedType, batch_size: int, shuffle: bool = True)
#
Bases: Sampler
Sampler for stratified sampling across classes.
Ensures balanced representation of classes in each batch by sampling proportionally from each class. Useful for imbalanced datasets.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
Dataset with target labels for stratification.
TYPE:
|
batch_size
|
Batch size for sampling.
TYPE:
|
shuffle
|
Whether to shuffle samples within each class.
TYPE:
|
Examples:
Basic usage:
>>> import torch
>>> from spectre.data import DatasetWeighted, StratifiedSampler
>>> X = torch.randn(100, 10)
>>> targets = torch.randint(0, 3, (100,)) # 3 classes
>>> dataset = DatasetWeighted(X, target=targets)
>>> sampler = StratifiedSampler(dataset, batch_size=15, shuffle=True)
>>> indices = list(sampler)
>>> len(indices)
100
Notes
- Requires dataset to have targets
- Targets should be class labels (integers)
- For continuous targets, discretize before using stratified sampling
Source code in spectre/data/dataloader_stratified.py
StratifiedBatchSampler(dataset: DatasetWeightedType, batch_size: int, drop_last: bool = True, shuffle: bool = True)
#
Bases: BatchSampler
Batch sampler that yields stratified batches without materializing all indices.
Generates batches on-the-fly with balanced class representation, avoiding memory overhead from pre-computing all sample indices. Essential for memory-mapped datasets with millions of samples where materializing indices would consume significant RAM.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
Dataset with target labels for stratification.
TYPE:
|
batch_size
|
Batch size for sampling.
TYPE:
|
drop_last
|
Whether to drop the last incomplete batch.
TYPE:
|
shuffle
|
Whether to shuffle samples within each class and within each batch.
TYPE:
|
Examples:
Basic usage with DataLoader:
>>> import torch
>>> from torch.utils.data import DataLoader
>>> from spectre.data import DatasetWeighted, StratifiedBatchSampler
>>> X = torch.randn(1000, 10)
>>> targets = torch.randint(0, 3, (1000,))
>>> dataset = DatasetWeighted(X, target=targets)
>>> batch_sampler = StratifiedBatchSampler(dataset, batch_size=32)
>>> loader = DataLoader(dataset, batch_sampler=batch_sampler)
>>> for batch in loader:
... print(batch.data.shape)
torch.Size([32, 10])
With multi-worker DataLoader:
>>> batch_sampler = StratifiedBatchSampler(
... dataset,
... batch_size=64,
... drop_last=True,
... shuffle=True,
... )
>>> loader = DataLoader(dataset, batch_sampler=batch_sampler, num_workers=4)
Notes
- Memory-efficient: Does not materialize all indices upfront
- Compatible with PyTorch's multi-worker DataLoader
- Requires dataset to have targets
- Targets should be discrete class labels
- For continuous targets, discretize before using stratified sampling
References
- [1] https://discuss.pytorch.org/t/how-to-do-a-stratified-split/62290