class

SubsetRandomSampler

extendsSampler
SubsetRandomSampler(indices: list[int], generator: object = None)
source

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 int
Indices into the parent dataset to sample from.
generatoroptional= None
Seed-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
True

Methods (3)

dunder

__init__

None
__init__(indices: list[int], generator: object = None)
source

Store the index pool and optional generator handle.

Parameters

indiceslist of int
Indices to sample from.
generatoroptional= None
Accepted for API compatibility.
dunder

__iter__

Iterator[int]
__iter__()
source

Yield self.indices in a freshly shuffled order each epoch.

dunder

__len__

int
__len__()
source

Return the size of the index pool.