liveBook · Manning

Prefetch

As explained in Deep Learning with R, Third Edition

Data prefetching prepares future input elements asynchronously while current elements are being processed.

Data prefetching prepares future input elements asynchronously while the model processes current elements. Buffering keeps execution flowing instead of making each training step wait for loading or preprocessing. section 8.2.4 section 12.2.3

Prefetching an image dataset

In an R pipeline, batch the images and labels, then keep two batches ready: `dataset <- zip_datasets(images, labels) |> dataset_batch(16) |> dataset_prefetch(2)`. The model can process one batch while loading and resizing the next. For uncertain workloads, `dataset_prefetch(buffer_size = tf$data$AUTOTUNE)` lets TensorFlow choose a buffer size. section 12.2.3 section 18.2.2

Use prefetching to improve input-pipeline behavior:

  • Pair it with parallel mapping when preprocessing includes disk reads, decoding, resizing, or tokenization. section 12.2.3 section 16.2
  • Keep datasets streaming rather than loading an entire large image collection into memory. section 12.2.3
  • Pass a Dataset object to `fit()`, especially for distributed training, to support efficient execution. section 18.2.2
Listing 12.8 Creating a dataset to train on
library(tfdatasets, exclude = c("shape"))

images <- metadata$path |> normalizePath() |>
  tensor_slices_dataset() |>
  dataset_map(\(path) {
    path |>
      tf$io$read_file() |> #1
      tf$image$decode_jpeg(channels = 3L) |> #1
      preprocessor()  #1
  }, num_parallel_calls = 8) #1

labels <-  tensor_slices_dataset(list(
  box = box_array, class = class_array
))

dataset <- zip_datasets(images, labels) |>  #2
  dataset_batch(16) |> dataset_prefetch(2)

val_dataset <- dataset |> dataset_take(500) #3
train_dataset <- dataset |> dataset_skip(500)  #3

Inspect a complete image pipeline that decodes and preprocesses files in parallel, batches labels with images, and applies `dataset_prefetch(2)`. section 12.2.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains buffering, streaming, and buffer-size selection with R pipelines, but does not derive scheduling or runtime internals.
Examples
Demonstrates prefetching in image classification, COCO object detection, and mini-GPT text-tokenization pipelines.
What it assumes
Assumes iterable Dataset pipelines and batching, while introducing asynchronous preparation through concrete image and text workflows.
What you can do after
Apply it via `dataset_prefetch()` to overlap input preparation with model computation and select a fixed or automatic buffer.
How it connects
Connects Dataset objects with batching, parallel preprocessing, interleaving, GPU utilization, and distributed training.

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 R, Third Edition. The section links point to the parts of the book it draws on.