class

Sampler

Sampler()
source

Abstract base class for index samplers used by DataLoader.

A sampler iterates over integer indices into a map-style dataset. Subclasses must implement __iter__ (yielding indices) and __len__ (total number of indices produced per epoch).

Notes

Samplers decouple which samples are visited from how they are fetched — the dataset answers __getitem__(i) and the sampler decides the sequence of i values. This separation is what lets DataLoader swap iteration policies (sequential / random / weighted / distributed) without touching the dataset.

Examples

>>> class Even(Sampler):
...     def __init__(self, n): self.n = n
...     def __iter__(self): return iter(range(0, self.n, 2))
...     def __len__(self): return (self.n + 1) // 2
>>> list(Even(6))
[0, 2, 4]

Methods (2)

dunder

__iter__

Iterator[int]
__iter__()
source

Yield integer sample indices.

Returns

Iterator[int]

Iterator over 0 <= i < len(data_source) values, in an order defined by the concrete sampler.

dunder

__len__

int
__len__()
source

Return the number of indices yielded in one full pass.