iter_batches#
- Dataset.iter_batches(*, prefetch_batches: int = 1, batch_size: int | None = 256, batch_format: str | None = 'default', drop_last: bool = False, local_shuffle_buffer_size: int | None = None, local_shuffle_seed: int | None = None, _collate_fn: Callable[[pyarrow.Table | pandas.DataFrame | Dict[str, ndarray] | cudf.DataFrame], CollatedData] | None = None) Iterable[pyarrow.Table | pandas.DataFrame | Dict[str, ndarray] | cudf.DataFrame][source]#
Return an iterable over batches of data.
This method is useful for model training.
Note
This operation will trigger execution of the lazy transformations performed on this dataset.
Examples
>>> import ray >>> ds = ray.data.read_images("s3://anonymous@ray-example-data/image-datasets/simple") >>> for batch in ds.iter_batches(batch_size=2, batch_format="numpy"): ... print(batch) {'image': array([[[[...]]]], dtype=uint8)} {'image': array([[[[...]]]], dtype=uint8)}
Note
Breaking out of the for-loop above shuts the streaming executor down so it stops producing blocks into the object store. If you keep your own reference to the iterator (
it = iter(...)), cleanup is deferred until that reference is dropped — callit.close()to release resources eagerly.Some libraries (for example PyTorch Lightning’s
limit_train_batches) hold aniter()reference internally to cap how many batches are consumed. In those cases preferds.limit(n)on the dataset so iteration ends naturally afternrows.Time complexity: O(1)
- Parameters:
prefetch_batches (int) – The number of batches to fetch ahead of the current batch. Ray Data prepares batches in background threads while your code consumes the current one. If greater than 0, a thread pool formats the batches. The pool has
prefetch_batchesthreads, up to a limit of 4. To change the limit, set theRAY_DATA_MAX_FORMAT_THREADPOOL_NUM_WORKERSenvironment variable. If 0, Ray Data doesn’t prefetch blocks and formats each batch without the thread pool. Defaults to 1.batch_size (int | None) – The number of rows in each batch, or
Noneto use entire blocks as batches (blocks may contain different numbers of rows). The final batch may include fewer thanbatch_sizerows ifdrop_lastisFalse. Defaults to 256.batch_format (str | None) – If
"default"or"numpy", batches areDict[str, numpy.ndarray]. If"pandas", batches arepandas.DataFrame. If"pyarrow", batches arepyarrow.Table. If"cudf"[Experimental], batches arecudf.DataFrame.drop_last (bool) – Whether to drop the last batch if it’s incomplete.
local_shuffle_buffer_size (int | None) – If not
None, Ray Data randomly shuffles the data using a local in-memory shuffle buffer, and this value serves as the minimum number of rows that must be in the buffer to yield a batch. When there are no more rows to add to the buffer, Ray Data drains the remaining rows. A larger buffer makes the shuffle more random but might increase the latency to the first batch. If this value is smaller thanbatch_size, Ray Data rounds it up tobatch_size. Local shuffling raises aValueErrorifbatch_sizeisNone.local_shuffle_seed (int | None) – The seed to use for the local random shuffle.
_collate_fn (Callable[[pyarrow.Table | pandas.DataFrame | Dict[str, ndarray] | cudf.DataFrame], CollatedData] | None) – A custom function to collate each batch of data.
- Returns:
An iterable over batches of data.
- Return type:
Iterable[pyarrow.Table | pandas.DataFrame | Dict[str, ndarray] | cudf.DataFrame]