class

RandomSampler

extendsSampler
RandomSampler(data_source: Dataset, replacement: bool = False, num_samples: int | None = None, generator: object = None)
source

Yield indices in random order, with or without replacement.

Parameters

data_sourceDataset
Dataset whose __len__ defines the index range [0, n).
replacementbool= False
If True, indices are drawn with replacement (any index can appear multiple times). If False (default), indices are a random permutation of range(n) and each index appears exactly once per epoch.
num_samplesint= None
Number of indices to draw per epoch. Defaults to len(data_source). Only meaningful when replacement=True; otherwise it truncates the permutation.
generatoroptional= None
Seed-like object forwarded to random.Random for reproducibility when replacement=True.

Notes

Without replacement, each epoch is a fresh uniform permutation of range(n) — equivalent to shuffling. With replacement, indices are i.i.d. uniform draws and num_samples controls the per-epoch budget independently of n; this lets the caller oversample (num_samples > n) for stochastic training regimes.

Examples

>>> sampler = RandomSampler(my_dataset)
>>> for idx in sampler:
...     x = my_dataset[idx]

Methods (4)

dunder

__init__

None
__init__(data_source: Dataset, replacement: bool = False, num_samples: int | None = None, generator: object = None)
source

Configure the random sampler.

Parameters

data_sourceDataset
Source dataset.
replacementbool= False
See class docstring.
num_samplesint= None
See class docstring.
generatoroptional= None
See class docstring.
prop

num_samples

int
num_samples: int
source

Effective number of samples per epoch.

Returns the explicit num_samples argument if provided, otherwise len(data_source).

dunder

__iter__

Iterator[int]
__iter__()
source

Yield num_samples indices according to the configured strategy.

With replacement=True each index is drawn uniformly with replacement using a seeded random.Random. With replacement=False a fresh permutation of range(n) is produced via the module-level random.shuffle and truncated to num_samples.

dunder

__len__

int
__len__()
source

Return num_samples.