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
