r""" Subset of a dataset at specified indices. Args: dataset (Dataset): The whole Dataset indices (sequence): Indices in the whole set selected for subset
| 370 | |
| 371 | |
| 372 | class Subset(Dataset[T_co]): |
| 373 | r""" |
| 374 | Subset of a dataset at specified indices. |
| 375 | |
| 376 | Args: |
| 377 | dataset (Dataset): The whole Dataset |
| 378 | indices (sequence): Indices in the whole set selected for subset |
| 379 | """ |
| 380 | |
| 381 | dataset: Dataset[T_co] |
| 382 | indices: Sequence[int] |
| 383 | |
| 384 | def __init__(self, dataset: Dataset[T_co], indices: Sequence[int]) -> None: |
| 385 | self.dataset = dataset |
| 386 | self.indices = indices |
| 387 | |
| 388 | def __getitem__(self, idx): |
| 389 | if isinstance(idx, list): |
| 390 | return self.dataset[[self.indices[i] for i in idx]] |
| 391 | return self.dataset[self.indices[idx]] |
| 392 | |
| 393 | def __getitems__(self, indices: List[int]) -> List[T_co]: |
| 394 | # add batched sampling support when parent dataset supports it. |
| 395 | # see torch.utils.data._utils.fetch._MapDatasetFetcher |
| 396 | if callable(getattr(self.dataset, "__getitems__", None)): |
| 397 | return self.dataset.__getitems__([self.indices[idx] for idx in indices]) # type: ignore[attr-defined] |
| 398 | else: |
| 399 | return [self.dataset[self.indices[idx]] for idx in indices] |
| 400 | |
| 401 | def __len__(self): |
| 402 | return len(self.indices) |
| 403 | |
| 404 | |
| 405 | def random_split(dataset: Dataset[T], lengths: Sequence[Union[int, float]], |
no outgoing calls
searching dependent graphs…