Subset

class paddle.io. Subset ( dataset: Dataset[_T], indices: Sequence[int] ) [source]

Subset of a dataset at specified indices.

Parameters
  • dataset (Dataset) – The whole Dataset.

  • indices (sequence) – Indices in the whole set selected for subset.

Returns

A Dataset which is the subset of the original dataset.

Return type

List[Dataset]

Examples

>>> import paddle

>>> class RangeDataset(paddle.io.Dataset):  # type: ignore[type-arg]
...     def __init__(self, start, stop):
...         self.start = start
...         self.stop = stop
...
...     def __getitem__(self, index):
...         return index + self.start
...
...     def __len__(self):
...         return self.stop - self.start

>>> # Example 1:
>>> a = paddle.io.Subset(dataset=RangeDataset(1, 4), indices=[0, 2])
>>> print(list(a))
[1, 3]

>>> # Example 2:
>>> b = paddle.io.Subset(dataset=RangeDataset(1, 4), indices=[1, 1])
>>> print(list(b))
[2, 2]