Map samples to labels or features by index
Training on an indexed collection of examples
PyTorch · Class
torch.utils.data.Dataset is the abstract base for map-style datasets. Subclasses provide keyed sample retrieval with __getitem__ and may provide their size through __len__.
Subclass Dataset to represent indexed samples. Implement __getitem__ for retrieving a sample by key; implement __len__ when the dataset has a known size so indexing utilities and data-loading components can determine its extent.
class torch.utils.data.DatasetA Dataset subclass exposes individual samples through __getitem__; when implemented, __len__ reports the dataset size.
from torch.utils.data import Dataset
class Numbers(Dataset):
def __init__(self, values):
self.values = values
def __len__(self):
return len(self.values)
def __getitem__(self, index):
return self.values[index]
dataset = Numbers([10, 20, 30])
print(dataset[1], len(dataset))Defines a map-style dataset whose samples are retrieved by index and whose size is reported with len().
Training on an indexed collection of examples
Feeding custom data to a training loop
Wrapping in-memory or file-backed records
Dataset is the base abstraction for map-style data. A subclass should implement __getitem__ so a key can retrieve one sample, and it may implement __len__ to report the dataset size.
Follow-up: How would you model a stream with no reliable length? Use IterableDataset and implement __iter__ instead of relying on indexed access.
A map-style dataset represents a mapping from keys, commonly indices, to samples. DataLoader can consume it and provide iteration, batching, sampling, and optional multi-process loading.
Follow-up: What changes for an iterable-style dataset? Samples are produced through __iter__, which is useful when data arrives as a stream rather than from a finite indexable collection.
Dataset does not require a constructor signature of its own; subclasses define initialization for their storage and metadata. The required map-style retrieval hook is __getitem__, while __len__ is optional in the base class.
Follow-up: Why might omitting __len__ matter? Components that expect dataset size or use default length-dependent behavior may not be able to determine the dataset extent.
torch.utils.data.DataLoadertorch.utils.data.IterableDatasettorch.utils.data.Sampler