Gradient accumulation lets several smaller microbatches contribute gradients before one optimizer update. If the intended loss is the mean over examples, each example must receive the same weight. Summing per-example gradients and dividing by the total number of examples preserves that objective even when the microbatches have different sizes.
This film uses a scalar linear network with two weights. The input is multiplied by w1 = 2 and then w2 = -1. For input 1 and target 0, the prediction is -2 and half the squared error is 2. The chain rule gives gradients (2, -4). Two more examples, with input and target pairs (2, -3) and (-1, 1), give gradients (2, -4) and (1, -2), evaluated at the same starting weights.
Put the first example in a microbatch of one and the other two in a microbatch of two. Their gradient sums are (2, -4) and (3, -6). The total is (5, -10), so the correct mean is (5/3, -10/3). Averaging the two microbatch means equally would instead give (1.75, -3.5): the lone example would get half of the influence, while each other example gets a quarter. Regrouping the same data as two examples followed by one changes that wrong result to (1.5, -3).
The explorer changes the split and normalization rule while keeping the examples and starting weights fixed. Correct example weighting produces the same gradient and update for every split. Equal microbatch means coincide with the correct answer when the microbatches have equal sizes, which can hide this bug in regular batches and reveal it in a smaller final batch.
At learning rate 0.1, one plain SGD update changes the weights to (11/6, -2/3), about (1.833, -0.667). The underlying calculation retains full floating-point precision. This is an arithmetic demonstration with independent examples, a fixed parameter snapshot and no stateful or stochastic layers. Batch-dependent normalization, random operations, mixed-precision scaling and distributed reductions need their own treatment; accumulating several optimizer steps is a different procedure.
The maths
- A small forward and backward pass
Both derivatives use the same old weights. The film traces every factor on the first example.
- Weight examples equally
If each microbatch already reports a mean, multiply that mean by its example count before the final division by N.
- Why a mean of means can be wrong
With microbatch sizes one and two, the weights become one half, one quarter and one quarter instead of one third each.
- One optimizer step
The toy uses plain SGD. Keep the parameter snapshot fixed while accumulating this effective batch, then update once.
Transcript
The narration, chapter by chapter. A timestamp opens the film at that moment.
- 0:00Every example counts
- Three examples can arrive as one microbatch and then two. If every example should count equally, the split must not decide how far we move the weights. Let us test that.
- 0:11A tiny forward pass
- Our tiny network multiplies an input by two, then by minus one. With input one and target zero, its prediction is minus two, and half the squared error is two.
- 0:22Trace the gradient
- Backpropagation carries the error through both multiplies. This example contributes gradients two and minus four. Compute the other examples at those same weights, so their gradients can be added before any update.
- 0:35Add microbatch sums
- The first microbatch has one example. The second has two, whose gradient sum is three and minus six. Across all three examples, the accumulated sum is five and minus ten.
- 0:47The mean-of-means trap
- Here is the trap: averaging the two microbatch means gives the single example half the influence. It should receive one third. Changing the grouping can now change the update, even though the data is identical.
- 1:01Divide by examples
- Instead, divide the total gradient sum by three examples. The correct mean is five thirds and minus ten thirds. With learning rate zero point one, make exactly one optimizer step after that division.
- 1:16One update, same result
- The new weights are about one point eight three three and minus zero point six six seven. Try another split below: the correct result stays fixed. This scalar example uses plain gradient descent and excludes stateful batch layers.
Also available as captions (WebVTT), the video file and a Markdown copy of this page.
Sources and model assumptions
Follow the original mechanism behind this explainer. The interactive examples identify their toy data and simplifying assumptions above.
Related terms
- TrainingTraining is the process that produces a model: showing it enormous amounts of text and adjusting its parameters until it gets good at predicting what comes next. It happens once, before you ever use the model.
- ParametersParameters are the learned numbers (weights) inside a model that hold everything it appears to know. The count of them is what people mean by model size, and they are fixed once training ends.
- ModelA model is the trained artifact at the centre of every AI coding tool: a large file of numbers (parameters) that, given some text, produces the most likely continuation. When people say "which model are you using," this is the thing they mean.
More visualisations
Embed this film
Paste this into a post, a course page or a newsletter. The film plays in place, with the same controls as here, and credits the source.