Attention
June 2024 Status Update: Removing DataPipes and DataLoader V2
We are re-focusing the torchdata repo to be an iterative enhancement of torch.utils.data.DataLoader. We do not plan on continuing development or maintaining the [DataPipes] and [DataLoaderV2] solutions, and they will be removed from the torchdata repo. We’ll also be revisiting the DataPipes references in pytorch/pytorch. In release torchdata==0.8.0 (July 2024) they will be marked as deprecated, and in 0.9.0 (Oct 2024) they will be deleted. Existing users are advised to pin to torchdata==0.8.0 or an older version until they are able to migrate away. Subsequent releases will not include DataPipes or DataLoaderV2. Please reach out if you suggestions or comments (please use this issue for feedback)
ShuffledFlatMapper¶
- class torchdata.datapipes.iter.ShuffledFlatMapper(datapipe: IterDataPipe, fn: Optional[Callable] = None, input_col=None, buffer_size: int = 100)¶
Applies a function over each item from the source DataPipe, then collects the iterables returned in a buffer, then, at every iteration, chooses at random one of the iterables in the buffer and yields one item from this iterable (functional name:
shuffled_flatmap
).When the buffer is full, the DataPipe will begin to yield elements from iterables within the buffer. New iterables will be added to the buffer once the existing ones run out of elements. .. note:
The output from ``fn`` must be an Iterable. Otherwise, an error will be raised. If ``fn`` is ``None``, source DataPipe will be just flattened vertically, provided that items can be unpacked.
- Parameters:
datapipe – Source IterDataPipe
fn – the function to be applied to each element in the DataPipe, the output must be a Sequence
input_col –
Index or indices of data which
fn
is applied, such as:None
as default to applyfn
to the data directly.Integer(s) is/are used for list/tuple.
Key(s) is/are used for dict.
buffer_size – the max number of iterables this DataPipe can hold at a time (default to
100
)
Example
>>> from torchdata.datapipes.iter import IterableWrapper >>> source_dp = IterableWrapper([[1, 2, 3, 4], 'abcd', 'ABCD']) >>> shuffled_flatmapped_dp = source_dp.shuffled_flatmap(buffer_size=2) >>> list(shuffled_flatmapped_dp) ['a', 'b', 'c', 1, 'd', 'A', 'B', 'C', 2, 'D', 3, 4] >>> >>> # To shuffle all the elements, you can combine `shuffled_flatmap` with `in_batch_shuffle` like this: >>> fully_shuffled_flatmapped_dp = source_dp.in_batch_shuffle() >>> fully_shuffled_flatmapped_dp = fully_shuffled_flatmapped_dp.shuffled_flatmap() >>> list(fully_shuffled_flatmapped_dp) ['b', 3, 'c', 'd', 'C', 'A', 'a', 2, 'B', 'D', 4, 1]