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.
  • 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 apply fn 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)


>>> 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]


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources