liveBook · Manning

Tf.data API

As explained in Deep Learning with R, Third Edition

The tf.data API is TensorFlow's API for constructing input pipelines that process and preprocess data efficiently.

The tf.data API builds input pipelines that load, preprocess, and batch data for training. It is useful when loading everything into memory would be impractical or when preprocessing could leave an accelerator waiting for its next batch. section 12.2.3 section 14.4.1

Stream images for object detection

In R, start with image paths, turn them into a dataset, and map a function that reads each file, decodes its JPEG, and applies a preprocessor. Make a separate dataset of class and box labels, then zip images with labels. Batch the pairs, prefetch two batches, and take or skip batches to create validation and training datasets. Images are loaded as needed rather than all expanded into memory at once. section 12.2.3

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

The R/tfdatasets listing shows the complete image-path-to-batch pipeline, including parallel mapping, labels, prefetching, and the validation split. section 12.2.3

The same pattern extends beyond images:

  • For IMDb reviews, map a TextVectorization layer over a dataset to produce bag-of-words inputs while retaining their labels. Parallel CPU preprocessing helps keep a GPU or TPU supplied with data. section 14.4.1
  • For English-to-Spanish translation, map sentence pairs into tokenized inputs, next-token targets, and sample weights that exclude padded labels. Cache, shuffle, batch, and prefetch the results. section 15.2.1
  • For mini-GPT pretraining, interleave text-file shards so CPU workers can read and tokenize separate files. Form token sequences and prefetch batches for training. section 16.2

Although tf.data belongs to TensorFlow, a Keras training workflow can use its datasets with TensorFlow, JAX, or PyTorch backends; Keras converts input tensors for the selected backend. section 14.4.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through dataset construction, mapping, batching, prefetching, caching, and interleaving, but does not explain tf.data internals.
Examples
Shows COCO image batches, IMDb bag-of-words reviews, English-to-Spanish sentence pairs, and interleaved text shards.
What it assumes
Uses R pipeline syntax and Keras training datasets while demonstrating tf.data operations through concrete image and text examples.
What you can do after
Build it from scratch with R/tfdatasets operations to stream, transform, batch, and prefetch model inputs.
How it connects
Connects data preprocessing to TextVectorization, object detection, translation, language-model pretraining, and Keras multi-backend training.

Also explained in

Compare how 2 books explain tf.data API →

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.