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











