Map-style DataPipes¶
A Map-style DataPipe is one that implements the __getitem__()
and __len__()
protocols, and represents a map
from (possibly non-integral) indices/keys to data samples.
For example, when accessed with mapdatapipe[idx]
, could read the idx
-th image and its
corresponding label from a folder on the disk.
- class torchdata.datapipes.map.MapDataPipe(*args, **kwds)¶
Map-style DataPipe.
All datasets that represent a map from keys to data samples should subclass this. Subclasses should overwrite
__getitem__()
, supporting fetching a data sample for a given, unique key. Subclasses can also optionally overwrite__len__()
, which is expected to return the size of the dataset by manySampler
implementations and the default options ofDataLoader
.These DataPipes can be invoked in two ways, using the class constructor or applying their functional form onto an existing MapDataPipe (recommend, available to most but not all DataPipes).
Note
DataLoader
by default constructs an index sampler that yields integral indices. To make it work with a map-style DataPipe with non-integral indices/keys, a custom sampler must be provided.Example
>>> from torchdata.datapipes.map import SequenceWrapper, Mapper >>> dp = SequenceWrapper(range(10)) >>> map_dp_1 = dp.map(lambda x: x + 1) # Using functional form (recommended) >>> list(map_dp_1) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> map_dp_2 = Mapper(dp, lambda x: x + 1) # Using class constructor >>> list(map_dp_2) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> batch_dp = map_dp_1.batch(batch_size=2) >>> list(batch_dp) [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
Here is the list of available Map-style DataPipes:
DataPipes¶
Create mini-batches of data (functional name: |
|
Concatenate multiple Map DataPipes (functional name: |
|
Lazily load data from |
|
Apply the input function over each item from the source DataPipe (functional name: |
|
Wraps a sequence object into a MapDataPipe. |
|
Shuffle the input DataPipe via its indices (functional name: |
|
Aggregates elements into a tuple from each of the input DataPipes (functional name: |