liveBook · Manning

Prefetch

As explained in Deep Learning with Python, Third Edition

A data-input optimization that overlaps data preparation with model execution by preparing upcoming batches in advance.

Loading and preprocessing a batch can leave a model waiting for data. Prefetching overlaps that work with model execution: while the model handles one batch, the input pipeline prepares upcoming batches. section 8.2.4

Keep image batches ready

For disk-loaded JPEGs, map the image-loading function, pair images with labels, then finish the pipeline with `dataset = tf.data.Dataset.zip(images, labels).batch(16).prefetch(2)`. The `prefetch(2)` call keeps two batches buffered so training need not stop at every batch to load and resize images. section 12.2.3

Prefetching works alongside other `tf.data.Dataset` operations: `map()` transforms elements, `batch()` groups them, and `prefetch()` prepares batches ahead of use. A Dataset can be passed directly to Keras `fit()`. For distributed training, call `dataset.prefetch(buffer_size)` before `fit()`; if the buffer size is unclear, try `dataset.prefetch(tf.data.AUTOTUNE)`. section 8.2.4 section 18.2.2

Listing 12.9 Creating a dataset to train on
import tensorflow as tf

def load_image(path):                                               #1
    x = tf.io.read_file(path)
    x = tf.image.decode_jpeg(x, channels=3)
    return preprocessor(x)

images = tf.data.Dataset.from_tensor_slices([x["path"] for x in metadata])
images = images.map(load_image, num_parallel_calls=8)
labels = {"box": box_array, "class": class_array}
labels = tf.data.Dataset.from_tensor_slices(labels)

dataset = tf.data.Dataset.zip(images, labels).batch(16).prefetch(2)   #2
val_dataset, train_dataset = dataset.take(500), dataset.skip(500)     #3

Follow the complete image pipeline from file paths through parallel loading, labels, batching, and prefetching. section 12.2.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines asynchronous preparation and shows buffered pipelines, but stops short of scheduling internals or buffer-size tuning beyond AUTOTUNE.
Examples
Demonstrates `.batch(16).prefetch(2)` for disk-loaded images and `.batch(128).prefetch(8)` for tokenized text.
What it assumes
Introduces Dataset iteration and batching with a NumPy example before showing prefetching as a pipeline method.
What you can do after
Apply it via `tf.data.Dataset.prefetch()` before Keras `fit()`, using `tf.data.AUTOTUNE` when the buffer size is uncertain.
How it connects
Connects prefetching to tf.data.Dataset, batching, parallel map(), and Keras fit().

Also explained in

Compare how 3 books explain prefetch →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.