Gradient accumulation 2026-11-05
Gradient accumulation combines gradient contributions from several processed mini-batches before one optimizer update. Parameters stay fixed while those contributions are evaluated. With consistent normalization, an additive per-example loss function can therefore use an effective batch size larger than the group processed at once.[ref]
Automatic Mixed Precision examples in the PyTorch documentation. Gradient accumulation subsection and example loop
For an original example, let per-example scalar gradients be at the same parameter value. Process the first two together and the last separately. Their means are 2 and 8. The combined per-example mean isThe unweighted mean of group means is 5 and gives the smaller group too much weight. One implementation accumulates each group's summed loss divided by the total example count, then performs one update.
Accumulation is also different from consecutive updates. Let , with , starting at and using step size . One accumulated mean-gradient step gives . Updating first on the two-example mean gives ; the last example then has gradient , and a second step gives .
The distinction is where the gradients are evaluated. Agreement with one larger group requires fixed parameters and compatible model state, additive losses and consistent weighting. A method whose calculations depend on the whole group can violate those assumptions. State whether batch size denotes a processed group or the complete set contributing to an update.
Mini-batch 2026-11-05
A mini-batch is a group of training examples processed together in one iteration of a learning algorithm. Its batch size counts examples, while a training epoch usually counts one pass over the available training data. In stochastic gradient descent, a mini-batch gradient combines the component gradients from the selected examples; the group of examples and the resulting numerical estimate are distinct objects.[ref]
Optimizing Model Parameters in the PyTorch tutorials. Hyperparameters and Optimization Loop: batch size, epochs and updates.
For an original counting example, partition eight examples into mini-batches with nominal batch size three and retain the final partial group. The sizes are , giving three updates in one training epoch if each group produces one update. Discarding the final partial group changes which observations participate.
Unequal group sizes also matter when reporting a loss function. At fixed model parameters, suppose the three mini-batch mean losses are . Their unweighted mean is , but the mean over all eight examples isThe smaller last group must not receive the same weight as a larger group when the intended statistic is a per-example sample mean. This bookkeeping issue is separate from the randomness used to select a mini-batch.