Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Module

Dataset.batch

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.

How it works

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.

Syntax

tf.data.Dataset.batch(batch_size, drop_remainder=False, num_parallel_calls=None, deterministic=None, name=None)

Parameters

NameTypeDescription
batch_sizetf.int64 scalar TensorNumber of consecutive dataset elements to combine into each batch.
drop_remainderboolWhether to discard the final batch when it contains fewer elements than requested; defaults to retaining it.
num_parallel_callsintOptional number of batches to process in parallel.
deterministicboolOptional determinism setting for parallel processing.
namestringOptional name for the operation.

Returns

A tf.data.Dataset whose elements are batches formed from consecutive input elements.

Key points

  • The transformation combines consecutive input elements into one dataset element.
  • batch is the standard tf.data transformation for simple batching.
  • Shape validation follows tf.stack-like rules for each component.
  • For tuple or structured elements, batching is performed component by component.
  • Use padded_batch rather than regular batching for tensors whose shapes vary.

Common mistakes

  • Regular batching requires every corresponding component in the input elements to have the same shape; use padded_batch when shapes differ.

Example: Batch a dataset

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.

When to use it

Create mini-batches

Training and evaluation input pipelines

Guarantee static batch size

Models or accelerators requiring a fixed leading dimension

Batch multiple components together

Structured examples containing features and labels

Interview questions

QWhat does 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.

QWhat is the effect of 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.

QWhat shape constraint does batching impose?

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_batch
  • tf.data.Dataset.map
  • tf.stack

Source: tf.data: Build TensorFlow input pipelines. Checked 2026-10-15.