RandomSampler
SamplerRandomSampler(data_source: Dataset, replacement: bool = False, num_samples: int | None = None, generator: Generator | int | None = 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). Without replacement a value above n
chains whole permutations (every index appears
num_samples // n times, plus a partial pass); a value below
n truncates the permutation.lucid.Generator
advances from epoch to epoch, so a fixed one reproduces the whole
sequence of epochs; an int seeds a private generator once,
with the same effect. None (default) draws a fresh seed from
the default generator every epoch, so lucid.manual_seed
reproduces the order.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
>>> import lucid
>>> from lucid.utils.data import RandomSampler, TensorDataset
>>> my_dataset = TensorDataset(lucid.randn(10, 3))
>>> sampler = RandomSampler(my_dataset)
>>> for idx in sampler:
... x = my_dataset[idx]
>>> sorted(sampler) == list(range(10)) # a permutation of the indices
TrueUsed by 2
Constructors
1__init__
→None__init__(data_source: Dataset, replacement: bool = False, num_samples: int | None = None, generator: Generator | int | None = None)Configure the random sampler.
Parameters
data_sourceDatasetreplacementbool= Falsenum_samplesint= NoneRaises
TypeErrorgenerator is neither a lucid.Generator, a seed,
nor None.Properties
1Dunder methods
2Yield num_samples indices according to the configured strategy.
With replacement=True each index is an independent uniform
draw from range(n). With replacement=False the indices
are a fresh permutation of range(n) (chained, then truncated,
to reach num_samples). Both draw from generator, or
— when it is None — from a per-epoch generator seeded off the
default one.
Return num_samples.