Per-element preprocessing
Normalize, decode, resize, or otherwise preprocess each training example before batching.
TensorFlow · Module
Applies a function to every element of a TensorFlow dataset and returns a new dataset of transformed elements. It can also change element structure and optionally process elements in parallel.
Dataset.map applies a function to each input element and returns another dataset containing the function's results. The function receives the tensors representing one element and may change both element values and structure; optional parallel calls control asynchronous processing.
map(map_func, num_parallel_calls=None, deterministic=None, name=None)| Name | Type | Description |
|---|---|---|
map_func | function | Function mapping one dataset element to another dataset element. |
num_parallel_calls | tf.int64 scalar Tensor | Optional scalar tf.int64 tensor specifying the number of elements processed asynchronously in parallel. |
deterministic | bool or None | Optional setting controlling deterministic ordering of parallel results. |
name | string or None | Optional name for the operation. |
A new Dataset containing the transformed elements, in the same order as the input when deterministic ordering is used.
import tensorflow as tf
dataset = tf.data.Dataset.range(1, 6)
dataset = dataset.map(lambda x: x + 1)
print(list(dataset.as_numpy_iterator()))Creates a range dataset, increments every element, and prints the transformed values.
Normalize, decode, resize, or otherwise preprocess each training example before batching.
Convert tuples into selected components or construct new nested element structures.
Run independent preprocessing work concurrently to increase input-pipeline throughput.
map_func is applied to each element of the input dataset, and map returns a new dataset containing the transformed elements. The function receives the tensors for one element and returns tensors for one element in the result dataset.
Follow-up: How can mapping change a dataset's shape? It can return a different supported element structure, such as selecting only one component from a tuple or producing multiple components.
num_parallel_calls specifies how many elements may be processed asynchronously in parallel. If omitted, processing is sequential; using parallel calls can improve performance for costly per-element work.
Follow-up: When can ordering be relaxed? Set deterministic=False when the application does not require deterministic output order, allowing potentially better throughput.
map_func can accept and return any type of dataset element supported by TensorFlow's dataset structure rules. This permits changing both values and the structure of elements.
Follow-up: What is a common pipeline optimization after mapping? Cache after map when the mapped result fits the available memory, avoiding repeated preprocessing across epochs.
tf.data.Dataset.batchtf.data.Dataset.cachetf.data.Dataset.flat_maptf.data.Dataset.interleavetf.data.Dataset.prefetchtf.py_function