liveBook · Manning

Dataset_map

As explained in Deep Learning with R, Third Edition

dataset_map is a dataset transformation method that applies a callable transformation to each dataset element.

dataset_map applies a callable transformation to every element yielded by a dataset. Use it to reshape, load, convert, or otherwise prepare elements as they flow through a pipeline. section 8.2.4

Listing 8.13 Applying a Dataset transformation using dataset_map()
reshaped_dataset <- dataset |>
  dataset_map(\(element) tf$reshape(element, shape(2, 3)))

reshaped_dataset_iterator <- as_iterator(reshaped_dataset)
for (i in 1:3) {
  element <- iter_next(reshaped_dataset_iterator)
  print(element)
}

tf.Tensor(

[[  1 101 201]

 [301 401 501]], shape = (2, 3), dtype = int32)

tf.Tensor(

[[  2 102 202]

 [302 402 502]], shape = (2, 3), dtype = int32)

tf.Tensor(

[[  3 103 203]

 [303 403 503]], shape = (2, 3), dtype = int32)

Reshapes each six-value dataset element into a 2 × 3 tensor, showing the callable, pipeline composition, and resulting elements. section 8.2.4

A dataset transformation function should:

  • Accept the element yielded by the dataset; the callable receives one element at a time. section 8.2.4
  • Return tensors or structures suitable for subsequent batching or model processing, such as an image and its segmentation mask. section 11.3.1
  • Support pipeline processing with symbolic tensors; test a concrete element separately when interactive inspection is useful. section 11.3.1

Transforming a segmentation dataset

A mapping function can read an input image and its target mask, resize both to a common image size, convert the mask to uint8, and return them together. The resulting dataset can then be cached and batched for model use. section 11.3.1

The same operation can compute values rather than prepare inputs: after unbatching, dataset_map calculates an absolute error for each sample before dataset_reduce accumulates the result. section 13.2.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains callable inputs, reshaping, symbolic execution, and interactive testing, but defers broader dataset-pipeline details.
Examples
Demonstrates reshaping toy vectors, preparing image-mask pairs, and calculating temperature-forecasting errors.
What you can do after
Apply transformations via tfdatasets to reshape, preprocess, or evaluate elements as they move through a dataset pipeline.
How it connects
Connects dataset_map with dataset_batch, dataset_cache, dataset_unbatch, dataset_reduce, and symbolic tensors.

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.