class
SubsetRandomSampler
extends
SamplerSubsetRandomSampler(indices: list[int], generator: object = None)Yield a random permutation of a fixed list of indices each epoch.
Useful when the caller already knows which subset of the dataset should be visited (e.g., a precomputed train/val split) and only wants shuffling among that subset.
Parameters
indiceslist of intIndices into the parent dataset to sample from.
generatoroptional= NoneSeed-like object accepted for API compatibility; the current
implementation defers to the global
random state.Notes
The index pool itself is fixed at construction time; only the
order changes between epochs. Useful with precomputed
cross-validation folds — store the per-fold index list once, then
instantiate one SubsetRandomSampler per fold.
Examples
>>> fold_indices = [3, 5, 7, 9, 11]
>>> sampler = SubsetRandomSampler(fold_indices)
>>> sorted(list(sampler)) == fold_indices
TrueMethods (3)
dunder
__init__
→None__init__(indices: list[int], generator: object = None)Store the index pool and optional generator handle.
Parameters
indiceslist of intIndices to sample from.
generatoroptional= NoneAccepted for API compatibility.
dunder
__iter__
→Iterator[int]__iter__()Yield self.indices in a freshly shuffled order each epoch.
dunder
__len__
→int__len__()Return the size of the index pool.