liveBook · Manning

Batch size

As explained in Deep Learning with Python, Third Edition

The number of sequence examples processed together in one forward or training step.

A model processes data in small groups rather than taking in an entire dataset at once. Batch size is the number of samples in one group. In a data tensor, axis 0 is the samples axis; after samples are grouped, it is also called the batch axis. section 2.2.7

128 handwritten digits at a time

With batch size 128, `train_images[:128]` is the first batch and `train_images[128:256]` is the next. In Keras, `model.fit(train_images, train_labels, epochs=5, batch_size=128)` trains on successive mini-batches. For each batch, the model computes a loss gradient and updates its weights; an epoch is one pass through the training data. section 2.2.7 section 2.5

A batch is often 8 to 128 samples. Powers of two are common because they facilitate GPU memory allocation. Batch size also affects training behavior: a larger batch produces gradients that are more informative and less noisy. If loss stalls, try adjusting batch size alongside learning rate while keeping the other settings fixed. section 4.0 section 5.3.1

Listing 8.12 Batching a dataset
>>> batched_dataset = dataset.batch(32)
>>> for i, element in enumerate(batched_dataset):
>>>     print(element.shape)
>>>     if i >= 2:
>>>         break
(32, 16)
(32, 16)
(32, 16)

Compare the shapes before and after `dataset.batch(32)`: individual vectors of shape `(16,)` become batches of shape `(32, 16)`. The new leading dimension counts samples. section 8.2.4

In a data pipeline, set the size when creating batches—for example, with `Dataset.batch(32)` or an image-directory loader’s `batch_size` argument. Check a yielded batch’s shape to confirm the setting. Batch size does not solve variable sample lengths: sequences of differing lengths must first be converted into tensors the model can process, such as padded or multi-hot-encoded inputs. section 8.2.4 section 4.1.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines the batch axis, traces batches through Keras training, and gives practical tuning advice without deriving gradient statistics.
Examples
Uses 128-sample MNIST slices and Keras training, plus 32-vector Dataset batches whose shape is `(32, 16)`.
What it assumes
Assumes basic familiarity with samples and arrays while defining the batch axis and demonstrating batching in code.
What you can do after
Operate/configure it with Keras `fit(batch_size=...)` or `Dataset.batch()`, then adjust it when training stalls.
How it connects
Links batch size to tensor shapes, epochs, gradient descent, learning rate, and dataset preprocessing.

Also explained in

Compare how 12 books explain batch size →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.