Input pipelines
Preparing batched inputs for model training or evaluation.
TensorFlow · Class
tf.data.Dataset represents a collection of elements and a pipeline of transformations. Build one from input data, transform it with operations such as map or batch, and iterate over the resulting elements.
A Dataset represents elements together with a logical pipeline of transformations. Create it from tensors or another source, apply operations such as map and batch, then iterate over the resulting elements for model input or other processing.
@staticmethod
from_tensor_slices(tensors, name=None) -> 'DatasetV2'| Name | Type | Description |
|---|---|---|
tensors | tf.Tensor or nested structure | Input tensors or compatible nested tensor structure to slice along the first dimension. |
name | str or None | Optional operation name. |
A DatasetV2 whose elements are slices of tensors along their first dimension.
import tensorflow as tf
dataset = tf.data.Dataset.from_tensor_slices(([1, 2, 3], [10, 20, 30]))
dataset = dataset.map(lambda x, y: (x * 2, y)).batch(2)
for features, labels in dataset.as_numpy_iterator():
print(features, labels)Creates paired elements, transforms the feature values, batches them, and iterates as NumPy arrays.
Preparing batched inputs for model training or evaluation.
Applying feature conversion or normalization before consumption.
Flattening nested datasets into a sequential stream.
Dataset is an abstraction for a collection of elements plus a logical plan of transformations. A typical pipeline creates a source, transforms it for preprocessing, and iterates over the result.
Follow-up: How would you flatten datasets while retaining order? Use flat_map, which the documentation specifically recommends for that requirement.
from_tensor_slices creates one dataset element per slice along the first dimension. It preserves the input structure while removing that leading dimension from each element.
Follow-up: What constraint applies when slicing multiple tensors? Their first dimensions must have identical sizes.
A practical pipeline can map preprocessing over elements and then batch them before iteration. The resulting dataset can be consumed with an iterator or as_numpy_iterator for NumPy-compatible output.
Follow-up: How can preprocessing be parallelized? Pass num_parallel_calls to map, commonly using a suitable parallelism value such as tf.data.AUTOTUNE.
tf.data.Dataset.maptf.data.Dataset.batchtf.data.Dataset.flat_maptf.data.Dataset.as_numpy_iteratortf.data.Dataset.get_single_element