class

Subset

extendsDataset
Subset(dataset: Dataset, indices: list[int])
source

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

datasetDataset
Parent dataset to view into.
indiceslist of int
Indices 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])
source

Initialise the instance. See the class docstring for parameter semantics.

dunder

__len__

int
__len__()
source

Return the number of indices in the subset.

dunder

__getitem__

Tensor or tuple of Tensor
__getitem__(idx: int)
source

Return dataset[indices[idx]].

Parameters

idxint
Position within the subset (not the parent dataset).

Returns

Tensor or tuple of Tensor

Sample from the parent dataset at the remapped index.