Create mini-batches
Training and evaluation input pipelines
TensorFlow · Module
Dataset.batch groups consecutive dataset elements into batches, adding an outer dimension to each component. It can retain or discard a smaller final batch, while variable-shaped tensors require padded_batch.
Dataset.batch groups consecutive dataset elements into one element by adding an outer batch dimension to each component. Unless requested otherwise, a final group smaller than the requested size is retained.
tf.data.Dataset.batch(batch_size, drop_remainder=False, num_parallel_calls=None, deterministic=None, name=None)| Name | Type | Description |
|---|---|---|
batch_size | tf.int64 scalar Tensor | Number of consecutive dataset elements to combine into each batch. |
drop_remainder | bool | Whether to discard the final batch when it contains fewer elements than requested; defaults to retaining it. |
num_parallel_calls | int | Optional number of batches to process in parallel. |
deterministic | bool | Optional determinism setting for parallel processing. |
name | string | Optional name for the operation. |
A tf.data.Dataset whose elements are batches formed from consecutive input elements.
batch is the standard tf.data transformation for simple batching.tf.stack-like rules for each component.padded_batch rather than regular batching for tensors whose shapes vary.padded_batch when shapes differ.import tensorflow as tf
dataset = tf.data.Dataset.range(8)
batched = dataset.batch(3, drop_remainder=True)
print(list(batched.as_numpy_iterator()))Creates batches of three consecutive integers and discards the incomplete final batch.
Training and evaluation input pipelines
Models or accelerators requiring a fixed leading dimension
Structured examples containing features and labels
Dataset.batch do?It groups consecutive elements into batches and adds an outer dimension to each component. The normal operation requires corresponding component tensors to have matching shapes.
Follow-up: What should you use for variable-shaped tensors? Use padded_batch, which batches them by padding specified dimensions.
drop_remainder?drop_remainder=False keeps a final partial batch when the dataset size is not divisible by batch_size. With True, that smaller final batch is omitted.
Follow-up: Why might True be useful? It guarantees a consistent outer batch dimension for code that requires static batch shapes.
Batching is a multi-element dataset transformation that stacks consecutive elements, applying the same shape constraints as tf.stack independently to each component.
Follow-up: How can batching be combined with element transformations? Chain operations such as map and batch on the dataset; TensorFlow can optimize suitable consecutive transformations.
tf.data.Dataset.padded_batchtf.data.Dataset.maptf.stack