Data Dataloader#
dataloader
#
| CLASS | DESCRIPTION |
|---|---|
DataLoaderWeighted |
Dataloader for datasets with advanced indexing and multi-worker support. |
Classes#
DataLoaderWeighted(dataset: DatasetType, batch_size: int = 1000, shuffle: bool = True, drop_last: bool = True, stratify: bool = False, num_workers: int = 0, pin_memory: bool = False, persistent_workers: bool = False, prefetch_factor: int | None = None, collate_fn: Callable | None = None)
#
Dataloader for datasets with advanced indexing and multi-worker support.
Supports multiple PyTorch dataset types with optimized tensor-based indexing. Provides flexible batching with shuffle, multi-worker loading, and device pinning for efficient data loading in machine learning workflows.
| PARAMETER | DESCRIPTION |
|---|---|
dataset
|
Dataset that supports tensor and list indexing.
TYPE:
|
batch_size
|
Batch size for data loading.
TYPE:
|
shuffle
|
Whether to shuffle dataset indices.
TYPE:
|
drop_last
|
Whether to drop the last incomplete batch.
TYPE:
|
stratify
|
Whether to use stratified sampling for balanced class representation. Requires dataset to be DatasetWeighted or DatasetWeightedMemoryMapped with target labels. Each batch will contain proportional representation from each class.
TYPE:
|
num_workers
|
Number of subprocesses for data loading. 0 means data loaded in main process.
TYPE:
|
pin_memory
|
If True, tensors copied to CUDA pinned memory before returning them. Useful when using GPU for faster host-to-device transfers.
TYPE:
|
persistent_workers
|
If True, keeps workers alive between epochs. Only valid when
TYPE:
|
prefetch_factor
|
Number of batches loaded in advance by each worker. Only valid when
TYPE:
|
collate_fn
|
Custom function to merge list of samples into batch. If None, uses default collation.
TYPE:
|
Properties
dataset : DatasetType The underlying dataset.
batch_size : int Current batch size.
dataset_len : int Length of the dataset.
Examples:
Basic usage with different dataset types:
>>> import torch
>>> from spectre.data import DataLoaderWeighted, DatasetWeighted
>>> from torch.utils.data import TensorDataset
>>> data = torch.randn(100, 10)
>>> weights = torch.rand(100)
>>> dataset_w = DatasetWeighted(data, weights)
>>> dataloader_w = DataLoaderWeighted(dataset_w, batch_size=20, shuffle=True)
>>> for batch_data, batch_weights in dataloader_w:
... print(batch_data.shape, batch_weights.shape)
torch.Size([20, 10]) torch.Size([20])
torch.Size([20, 10]) torch.Size([20])
Example with TensorDataset:
>>> tensor_data = torch.randn(100, 10)
>>> tensor_dataset = TensorDataset(tensor_data)
>>> tensor_dataloader = DataLoaderWeighted(
... tensor_dataset,
... batch_size=15,
... shuffle=False,
... )
>>> for batch in tensor_dataloader:
... print(batch[0].shape)
torch.Size([15, 10])
torch.Size([15, 10])
Example with Subset:
>>> full_data = torch.randn(50, 5)
>>> full_dataset = TensorDataset(full_data)
>>> subset_indices = list(range(30))
>>> subset_dataset = Subset(full_dataset, subset_indices)
>>> subset_dataloader = DataLoaderWeighted(
... subset_dataset,
... batch_size=10,
... drop_last=False,
... )
>>> for batch in subset_dataloader:
... print(batch[0].shape)
torch.Size([10, 5])
torch.Size([10, 5])
torch.Size([10, 5])
torch.Size([0, 5])
| ATTRIBUTE | DESCRIPTION |
|---|---|
dataset |
The underlying dataset.
TYPE:
|
batch_size |
Current batch size.
TYPE:
|
dataset_len |
Length of the dataset.
TYPE:
|
Source code in spectre/data/dataloader.py
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |