Shortcuts

Filter

class torchdata.datapipes.iter.Filter(datapipe: IterDataPipe, filter_fn: Callable, drop_empty_batches: bool = True)

Filters out elements from the source datapipe according to input filter_fn (functional name: filter).

Parameters
  • datapipe – Iterable DataPipe being filtered

  • filter_fn – Customized function mapping an element to a boolean.

  • drop_empty_batches – By default, drops a batch if it is empty after filtering instead of keeping an empty list

Example

>>> from torchdata.datapipes.iter import IterableWrapper
>>> def is_even(n):
...     return n % 2 == 0
>>> dp = IterableWrapper(range(5))
>>> filter_dp = dp.filter(filter_fn=is_even)
>>> list(filter_dp)
[0, 2, 4]

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources