Minibatch gradients
The gradient of a minibatch-average loss is the average of its per-example gradients.
On this page 8 sections
01A minibatch combines several selected examples into one training update#
A minibatch combines several selected examples into one training update. Averaging their gradients balances their contributions and can reduce sampling noise while remaining cheaper than a full-dataset calculation. Every gradient must be evaluated at the same current parameter.
A minibatch gradient is the average of its per-example gradients. Add matching coordinates, then divide by the number of selected examples.
Gradients (2,−1), (5,2) and (2,5) sum to (9,6). Their batch average is (3,2).
For gradients , , and , sum first coordinates to get 9 and second coordinates to get 6. Divide both sums by the batch size 3, giving . There are three examples, not six: the number of vector entries is not the averaging denominator.
Example gradients: , . Find their average.
Show answer and explanation
Sum each coordinate and divide by 2: .
02If a batch selects only some rows, average exactly those rows#
If a batch selects only some rows, average exactly those rows. A repeated selected row contributes once per occurrence.
The result is a vector with the same shape as each gradient; do not collapse coordinates into one scalar.
Gradients: A=, B=, C=. Average selected rows A,B,B.
Show answer and explanation
Count B twice: average .
03A gradient sum differs from a gradient average by the batch count#
A gradient sum differs from a gradient average by the batch count. The selected objective’s normalization determines which is required.
Using only the last example discards the other selected contributions.
Example gradients: , . A draft reports their sum. Give the correct average.
Show answer and explanation
Sum each coordinate and divide by 2: .
04A mean loss uses a mean gradient#
A mean loss uses a mean gradient. If your objective instead sums losses, summing gradients is appropriate and changes the effective step scale. Keep the normalization consistent when comparing updates across batch sizes.
Sum matching coordinates, retain selected multiplicity and divide by the actual batch size.
- Average a minibatch's per-example gradients.
Sources & further reading
- [1]D2L §12.5.2, equations12.5.1–12.5.2 ↗D2L §12.5.2, equations12.5.1–12.5.2 · Article