Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

Dataset

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__.

How it works

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.

Syntax

class torch.utils.data.Dataset

Returns

A Dataset subclass exposes individual samples through __getitem__; when implemented, __len__ reports the dataset size.

Key points

  • Dataset is an abstract base for user-defined datasets.
  • Use Dataset for map-style collections where keys identify individual samples.
  • Implement __getitem__ so indexed or keyed access returns one sample.
  • An optional __len__ method should return the number of samples when the size is known.
  • The map-style protocol combines sample lookup with a length-reporting operation.
  • Pass a Dataset to DataLoader to obtain an iterable that can apply sampling and loading behavior.

Common mistakes

  • Implement __len__ accurately when the dataset has a known size; many dataset and loader behaviors use it to determine dataset length.

Example: Implementing a map-style Dataset

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().

When to use it

Map samples to labels or features by index

Training on an indexed collection of examples

Provide a DataLoader-compatible dataset

Feeding custom data to a training loop

Centralize sample retrieval and dataset sizing

Wrapping in-memory or file-backed records

Interview questions

QWhat methods should a map-style Dataset implement?

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.

QHow does Dataset relate to DataLoader?

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.

QDoes Dataset require __len__?

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.DataLoader
  • torch.utils.data.IterableDataset
  • torch.utils.data.Sampler

Source: torch.utils.data. Checked 2026-10-15.