class
Subset
extends
DatasetSubset(dataset: Dataset, indices: list[int])View into a parent dataset restricted to a list of indices.
Useful for train/val splits, k-fold cross-validation, or any time a contiguous subset of a larger dataset is needed without copying the underlying data.
Parameters
datasetDatasetParent dataset to view into.
indiceslist of intIndices into
dataset that compose this subset. Order is
preserved; duplicates are allowed.Notes
A Subset is purely an index-restricted view — the parent
dataset is held by reference and no sample is copied. Subsets may
be nested freely (a Subset of a Subset) because
the parent only needs to satisfy the map-style protocol.
Examples
>>> full = TensorDataset(X, y)
>>> train = Subset(full, list(range(0, 80)))
>>> val = Subset(full, list(range(80, 100)))Methods (3)
dunder
__init__
→None__init__(dataset: Dataset, indices: list[int])Initialise the instance. See the class docstring for parameter semantics.
dunder
__len__
→int__len__()Return the number of indices in the subset.
dunder
__getitem__
→Tensor or tuple of Tensor__getitem__(idx: int)Return dataset[indices[idx]].
Parameters
idxintPosition within the subset (not the parent dataset).
Returns
Tensor or tuple of TensorSample from the parent dataset at the remapped index.