No one computes the exact gradient of a language model's loss over its full training set: one gradient step would require a pass over trillions of tokens. Training instead uses cheap, noisy gradient estimates and adds two refinements, momentum and per-parameter step sizes. This lesson explains each piece, why it works, and the optimizer settings large models actually use.
Noisy gradients are enough
The training loss is an average over examples, . Pick a random minibatch of examples and use
in place of . This estimate is unbiased: its expectation is the true gradient. Its variance falls as , so its standard deviation falls as . Quadrupling the batch only halves the noise, while it quadruples the compute per step.
That the method converges at all goes back to Robbins and Monro (1951). They studied finding the root of a function that can only be observed through noisy measurements. They showed that iterating with step sizes satisfying
converges, for example with .[1] The first condition lets the iterates travel arbitrarily far; the second makes the accumulated noise finite. Stochastic gradient descent (SGD) is this scheme applied to . In practice, deep-learning schedules do not follow . They use a roughly constant rate followed by a decay, but the logic is the same: large steps to make progress, small steps to average out the noise.
The noise is not only a cost. Keskar et al. found that very large batches tended to converge to sharper minima and to generalize worse than small batches, and attributed the difference to the noise in small-batch gradients.[2] The interpretation is debated (see Generalization). Practitioners have also found ways to use large batches without that loss. Goyal et al. trained ResNet-50 on ImageNet with batches of 8,192 images without losing accuracy. They scaled the learning rate linearly with batch size and added a warmup phase.[3]
Momentum: averaging out the zig-zag
Gradient descent in a long, narrow valley zig-zags across the steep walls and creeps along the floor. Polyak's heavy-ball method (1964) adds a velocity term:[4]
The velocity is an exponentially weighted sum of past gradients. Components that keep the same sign, along the valley floor, add up, so with a constant gradient the effective step approaches . That is 10 times larger for the common choice . Components that flip sign, across the walls, largely cancel.
Worked example: the same valley, with momentum
Return to , with condition number . Plain gradient descent with its best step contracts the error by per step, so a 100-fold reduction takes about 230 steps. On quadratics, heavy-ball momentum with tuned and achieves the rate , a standard result for quadratics. The same 100-fold reduction then takes
a tenfold speed-up. The dependence on conditioning has gone from to . With stochastic gradients the gain is smaller, because momentum also averages noise, but the qualitative benefit remains.
With momentum and learning rate , roughly what effective step size does a parameter receive if its gradient has been constant for a long time? What risk does this create?
Show answer +Hide answer −
. The risk is overshoot: the velocity takes about steps to respond to a change, so when the gradient changes direction the parameter keeps moving the old way for many steps. High-momentum settings usually need a smaller .
Adam: a step size per parameter
Parameters in a deep network see gradients on very different scales. Embedding rows for rare tokens are updated seldom, and some layers have far larger gradients than others. A single learning rate suits none of them well. Adam (Kingma and Ba, 2014) keeps two running averages per parameter, the mean and the uncentred variance of its gradient:[5]
corrects them for their zero initialisation,
and updates
The paper's suggested defaults are , and .[5]
Why bias correction? At the first step, , which is far too small. Dividing by restores . Likewise , so the first update is per coordinate.
That last observation is the key property. The ratio is roughly unit-scale whatever the raw size of the gradient, so each parameter moves by about per step, scaled down when its gradient is noisy (large relative to ). Rescaling the loss by 1,000 leaves Adam's updates almost unchanged. Adam behaves like momentum combined with a per-coordinate correction for conditioning.
Parameter A has gradients that are always . Parameter B has gradients that alternate between and . After many steps of Adam, which moves faster, and why?
Show answer +Hide answer −
A. Its and , so it moves by about per step. For B, (the signs cancel) while , so it barely moves. Adam's step size depends on the gradient's consistency, not its magnitude.
AdamW: decoupling weight decay
Weight decay shrinks parameters toward zero every step. For plain SGD this is the same as adding an L2 penalty to the loss. Loshchilov and Hutter pointed out that for Adam the two are not equivalent.[6] If the penalty's gradient is added to , it gets divided by along with everything else. Parameters with large gradient variance are then barely regularised. AdamW applies the decay directly to the weights, outside the adaptive scaling:
They reported that decoupling made the best decay value largely independent of the learning rate. It also substantially improved Adam's generalization, enough to match SGD with momentum on image-classification benchmarks where Adam had lagged.[6]
AdamW is widely used to train large transformers. In one documented example, Llama 2 was pretrained with AdamW using , , , weight decay 0.1, gradient clipping at 1.0, 2,000 warmup steps and cosine decay to 10% of the peak learning rate.[7] The lower shortens the memory of the variance estimate from about 1,000 steps to about 20, so it responds faster to changes in gradient scale.
Weight decay matters beyond final accuracy. In this lab's small-network continual-learning experiments, loss of plasticity over long training streams behaved like an Adam-related weight-norm pathology. Plain weight decay fixed it better than several specialised methods at that scale. The authors note that other work finds L2 insufficient for deeper networks and longer streams.[8] The full results are in Escaping the saturated game.
Key takeaways
- Minibatch gradients are unbiased and have noise that falls as . Robbins–Monro step-size conditions explain why noisy steps still converge.
- Momentum accumulates consistent gradient directions and cancels oscillating ones. On quadratics it improves the dependence on conditioning from to , from about 230 to about 23 steps in the worked example.
- Adam normalises each parameter's step by a running estimate of its gradient scale, so updates are roughly in size and insensitive to gradient magnitude. Bias correction fixes the early steps.
- With Adam, L2 regularisation and weight decay differ. AdamW decouples them and is the standard optimizer for large transformers.
Sources
-
H. Robbins and S. Monro (1951). "A Stochastic Approximation Method." The Annals of Mathematical Statistics 22(3), 400–407. https://doi.org/10.1214/aoms/1177729586 ↩
-
N. S. Keskar, D. Mudigere, J. Nocedal, M. Smelyanskiy and P. T. P. Tang (2016). "On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima." ICLR 2017. arXiv:1609.04836. https://arxiv.org/abs/1609.04836 ↩
-
P. Goyal, P. Dollár, R. Girshick et al. (2017). "Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour." arXiv:1706.02677. https://arxiv.org/abs/1706.02677 ↩
-
B. T. Polyak (1964). "Some methods of speeding up the convergence of iteration methods." USSR Computational Mathematics and Mathematical Physics 4(5), 1–17. https://doi.org/10.1016/0041-5553(64)90137-5 ↩
-
D. P. Kingma and J. Ba (2014). "Adam: A Method for Stochastic Optimization." ICLR 2015. arXiv:1412.6980. https://arxiv.org/abs/1412.6980 ↩ ↩2
-
I. Loshchilov and F. Hutter (2017). "Decoupled Weight Decay Regularization." ICLR 2019. arXiv:1711.05101. https://arxiv.org/abs/1711.05101 ↩ ↩2
-
H. Touvron et al. (2023). "Llama 2: Open Foundation and Fine-Tuned Chat Models." arXiv:2307.09288. https://arxiv.org/abs/2307.09288 ↩
-
NeuroGenesys (2026). "Escaping the saturated game: plasticity, harder benchmarks, and retrieval in continual learning." /research/three-frontiers-continual-learning ↩
