liveBook · Manning

Batch size

As explained in Grokking AI Algorithms, Second Edition

Batch size is the number of training examples processed together in one model update.

Batch size is the amount of training data processed together before the model updates its weights. In Transformer language-model training, that amount is often described as a fixed number of tokens, such as 32. Batching turns a long token stream into manageable pieces that fit within the model’s context window. section 11.10 section 11.32

A batch moves through training as a short, repeatable cycle:

  • Select the next batch from the dataset. section 11.32
  • Run the batch through the model, calculate its loss, and backpropagate the resulting gradients. section 11.32
  • Update the model’s weights, then repeat with another batch. section 11.32
  • Handle an incomplete final batch by dropping it, padding it, or using its variable length when the architecture allows. section 11.13

A 32-token batch

Suppose a token stream is divided using a context window of 32. The first batch contains 32 token IDs and passes through the Transformer as one input. After its loss produces gradients and the weights are updated, training selects the next batch. If the final batch contains only three tokens, it can be dropped, padded with special tokens that the model ignores, or processed directly by an architecture supporting variable-length input. section 11.13 section 11.32

Figure 11.20 Tokenized training data as training batches
11_20

Visualizes how a tokenized stream becomes separate training batches. section 11.10

def batch_tokens(token_stream, context_window_size):
   batches = []

   start_index = 0
   while True:
       end_index = start_index + context_window_size     #A

       if end_index > len(token_stream):      #B
           break

       batch = token_stream[start_index:end_index]      #C
       batches.append(batch)

       start_index += 1      #D

   return batches

Shows a Python-style implementation that slices a token stream into fixed-length batches. section 11.10

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Explains batching, incomplete-batch handling, and the update cycle, while stopping short of deeper optimization theory.
Examples
Uses 32-token batches, a six-token-batch illustration, a three-token remainder, padding, and token-slicing code.
What it assumes
Assumes tokenized input and introduces how token streams and context windows determine batch construction.
What you can do after
Operate/configure training batches, process incomplete final batches, and include batch size among tunable training hyperparameters.
How it connects
Connects batch size with token streams, context windows, Transformers, epochs, backpropagation, padding, and hyperparameter tuning.

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 Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.