RandomSampler
SamplerRandomSampler(data_source: Dataset, replacement: bool = False, num_samples: int | None = None, generator: object = None)Yield indices in random order, with or without replacement.
Parameters
data_sourceDataset__len__ defines the index range [0, n).replacementbool= FalseTrue, 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= Nonelen(data_source). Only meaningful when replacement=True;
otherwise it truncates the permutation.generatoroptional= Nonerandom.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)
__init__
→None__init__(data_source: Dataset, replacement: bool = False, num_samples: int | None = None, generator: object = None)Configure the random sampler.
Parameters
data_sourceDatasetreplacementbool= Falsenum_samplesint= Nonegeneratoroptional= Nonenum_samples
→intnum_samples: intEffective number of samples per epoch.
Returns the explicit num_samples argument if provided,
otherwise len(data_source).
__iter__
→Iterator[int]__iter__()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.
__len__
→int__len__()Return num_samples.