Three words for three different counts
- : the number of training examples.
- : the batch size.
A batch is how many examples one gradient is computed from. An iteration, or step, is one parameter update. An epoch is one full pass over the data.
With 50,000 examples, a batch of 128 and 10 epochs, that is 391 steps per epoch and 3,910 updates in total.
The model learns per step, not per epoch. An epoch is a bookkeeping unit, and it is the wrong unit as soon as the dataset size changes.
Why the distinction bites
Doubling the batch size halves the number of updates in the same number of epochs, so a run that was tuned at batch 64 takes half as many steps at batch 128 and underfits.
The usual correction is to scale the learning rate with the batch, so that fewer, less noisy steps each travel further.
Large language models are not measured in epochs at all. They are measured in tokens seen, because most of the data is visited once and a second pass would be a different decision entirely.
One run, in order
- Shuffle the training set at the start of every epoch.
- Cut it into batches of size .
- For each batch, forward, backward, step, and zero the gradients.
- At the end of the pass, evaluate on the validation set.
- Repeat for epochs, or until early stopping fires.
The reshuffle at step one matters. Without it the model sees the same batches in the same order every epoch, and the gradient noise becomes periodic rather than random.
The last batch, and the step that is not a step
The final batch of an epoch is usually smaller, and a smaller batch has a noisier gradient than the rest.
With batch normalisation a final batch of size 1 fails outright, because the variance of one example is zero. Dropping the remainder is the standard fix.
Gradient accumulation goes the other way: run batches, sum the gradients, and step once, which buys a large effective batch on a small GPU.
The step is then one iteration made of forward and backward passes, and the learning rate should be set for the effective size rather than the physical one.
What to report
Loss against steps, not loss against epochs, because two runs with different batch sizes are not comparable on an epoch axis.
Wall clock time is worth the third axis. A configuration that needs fewer steps but spends longer per step has not saved anything.