Adaptive Optimizers: RMSProp, Adam & AdamW
Per-parameter step sizes, bias correction and decoupled weight decay.
One learning rate for a billion parameters is a compromise: some parameters see large, consistent gradients and others tiny, rare ones. Adaptive optimizers give every parameter its own step size, read off from the history of its gradients. Adam, the most widely used of them, adds momentum and a small statistical correction, and AdamW fixes how it interacts with weight decay. This lesson derives all three and the practical lore around them.
What you’ll learn
- Write the AdaGrad, RMSProp and Adam updates, and explain what per-coordinate scaling does on ill-conditioned or poorly scaled problems.
- Derive Adam's bias correction from , and predict the size of its first steps.
- Show why an L2 penalty inside Adam is not weight decay, and implement decoupled weight decay (AdamW) with the usual parameter groups.
- Choose between AdamW and SGD with momentum, weigh the evidence on generalization, and count the memory cost of optimizer states.
One step size per parameter
Gradient descent's single must suit the most sharply curved direction, so directions with small gradients crawl. Worse, gradient sizes differ systematically between parameters: an embedding row for a rare word gets a gradient only when that word appears, and layers deep in a network can see gradients orders of magnitude smaller than others. The adaptive idea is to divide each coordinate's step by a running measure of that coordinate's gradient size.
AdaGrad (Duchi, Hazan & Singer, 2011) divides by the root of the sum of all past squared gradients. Rare features keep large steps, which is why it excels on sparse problems, but every step size shrinks like , often too fast for long nonconvex training runs. RMSProp (Tieleman & Hinton, 2012, in lecture notes for a course rather than a paper) replaces the sum by an exponential moving average, so the scale tracks recent gradients:
A consequence worth absorbing: the step is nearly scale-free. Multiply the loss by 100 and every gradient grows by 100, but so does ; the update barely changes, up to . Plain SGD's step would grow by 100. It also means a coordinate with a tiny but consistent gradient moves about as far per step as one with a large gradient, which is exactly what helps near a saddle point, where the escape direction has almost no slope.
Adam and its bias correction
Adam (Kingma & Ba, 2015) combines RMSProp's scaling with momentum, keeping exponential moving averages of the gradient and of its square, then correcting both for their start at zero:
ADAM(θ, η = 10⁻³, β₁ = 0.9, β₂ = 0.999, ε = 10⁻⁸)
m ← 0; v ← 0 // first and second moments, one per parameter
for t = 1, 2, …
g ← ∇ℒ_B(θ) // mini-batch gradient
m ← β₁·m + (1 − β₁)·g // momentum: EMA of g
v ← β₂·v + (1 − β₂)·g ⊙ g // EMA of g², elementwise
m̂ ← m / (1 − β₁ᵗ); v̂ ← v / (1 − β₂ᵗ) // bias correction
θ ← θ − η · m̂ / (√v̂ + ε) // elementwiseThe defaults are those of the original paper. Large language models commonly lower to , which forgets old gradient sizes faster; GPT-3 used , , and weight decay (Brown et al., 2020).
Unroll the recursion from : the second moment is a weighted sum of past squared gradients whose weights do not sum to one.
Suppose the squared gradients are stationary, . A geometric sum gives the expectation, which falls short by exactly :
So is unbiased, and likewise . The correction matters early: with , is a thousand times too small, and only after a few thousand steps is negligible. If the squared gradients drift slowly the bias is small but nonzero, which Kingma & Ba absorb into an error term.
Adam with , and default betas takes its first step. One parameter's first gradient is . How far does that parameter move, in units of ?
Show the answer
1 × 10⁻³
and , so the step is . A parameter with gradient would move by the same amount, while SGD with the same would move this one by only . Without bias correction the first step would be times larger.
How big are Adam's steps? Kingma & Ba show that the per-coordinate step is at most about in typical cases, and at most in the worst case, when a gradient has been zero for a long time and suddenly appears. The learning rate therefore sets a trust region in parameter space, regardless of the scale of the loss. Read as roughly per coordinate: close to when the gradient's sign is consistent, and small when noise dominates. Adam takes confident steps where the signal is clear and cautious ones where it is not, an automatic annealing as the signal fades near a minimum.
Decoupled weight decay: AdamW
Two regularizers that are the same thing for SGD come apart for Adam. An L2 penalty adds to the loss, so its gradient adds . Weight decay shrinks the weights directly, , at every step. For plain SGD the two are identical, since .
Put the penalty's gradient into Adam. Take for clarity; the penalty then travels through the same per-coordinate division as the data gradient:
Each weight now decays at its own rate, inversely proportional to its typical gradient size: weights that receive large gradients are barely regularized, those with small gradients are shrunk hard. (And also leaks into .) The regularization is coupled to the preconditioner in a way nobody chose.
AdamW (Loshchilov & Hutter, 2019) decouples the decay from the gradient-based update, so every weight shrinks by the same factor, exactly as in SGD:
Loshchilov & Hutter found that decoupling makes the best weight decay far less dependent on the learning rate and substantially improves Adam's generalization, closing much of its gap with SGD on image classification. Conventions differ slightly: their paper multiplies the decay by a schedule factor but not by the base rate, while PyTorch's AdamW multiplies by the current learning rate, as written above. It is also standard not to decay biases or the gains of normalization layers, which only rescale activations. Why weight decay matters even when it barely regularizes, through its interaction with normalization and the effective learning rate, is the subject of What Weight Decay Really Does in the Regularization topic.
import torch
decay, no_decay = [], []
for name, p in model.named_parameters():
# matrices and embeddings get weight decay; biases and norm gains do not
(decay if p.ndim >= 2 else no_decay).append(p)
opt = torch.optim.AdamW(
[{'params': decay, 'weight_decay': 0.1},
{'params': no_decay, 'weight_decay': 0.0}],
lr=3e-4, betas=(0.9, 0.95), eps=1e-8)A typical setup for a transformer, as of the mid-2020s. PyTorch's default weight_decay for AdamW is ; language-model recipes often use together with , warmup and a decaying schedule.
Adam or SGD?
The generalization debate. Wilson, Roelofs, Stern, Srebro & Recht (2017) built a linearly separable problem on which gradient descent reaches zero test error while AdaGrad, RMSProp and Adam reach test error close to one half, and found that on several deep-learning benchmarks adaptive methods generalized worse than SGD, sometimes by a large margin, even when they trained faster. Choi et al. (2019) countered that such comparisons hinge on the tuning protocol. Adam includes momentum SGD as a limit (with a large , its update approaches momentum SGD with learning rate ), so with enough tuning it should never lose, and in their experiments adaptive methods never underperformed momentum or plain gradient descent.
Practice, as of the mid-2020s. AdamW is the standard for transformers in language and vision, and for diffusion models. SGD with momentum remains common in convolutional image-classification recipes, where it is well understood and tuned. Why SGD struggles on transformers in particular has several competing explanations, the subject of the first Emergence lesson below.
| Optimizer | State per parameter | Notes |
|---|---|---|
| SGD | none | the baseline |
| SGD with momentum | (velocity) | common for CNNs |
| Adam, AdamW | (, ) | standard for transformers |
| Adafactor | optional; factored into row and column statistics | sublinear memory for the second moment (Shazeer & Stern, 2018) |
| 8-bit Adam | , stored in 8 bits with block-wise scales | about a quarter of the state memory (Dettmers, Lewis, Shleifer & Zettlemoyer, 2022) |
You accidentally multiply your loss by 100 (say, by summing instead of averaging over a batch of 100) and keep every hyperparameter. What happens?
- SGD's steps become 100 times larger and may diverge, while Adam's steps barely change
- Both optimizers take 100 times larger steps
- Adam's steps become 100 times larger, while SGD's barely change
- Neither changes, because the minimizer is the same
Show the answer
A. SGD's steps become 100 times larger and may diverge, while Adam's steps barely change
SGD's step is , proportional to the loss scale. Adam divides by , and both scale by 100, so the ratio is unchanged up to the effect of . A plain SGD recipe must rescale by to compensate.
Key takeaways
- Adaptive methods divide each coordinate's step by a running root-mean-square of its gradients: AdaGrad sums squares (good for sparse features, but steps shrink like ), RMSProp and Adam use moving averages. The result is a diagonal preconditioner and a nearly scale-free step.
- Adam keeps moving averages and with , so it divides by to remove the bias from starting at zero. Its first step is about , and its steps are roughly bounded by per coordinate.
- An L2 penalty inside Adam is divided by and decays each weight at a different rate; AdamW applies outside the preconditioner, usually skipping biases and normalization gains.
- AdamW is the standard for transformers and SGD with momentum is still common for CNNs. Claims that adaptive methods generalize worse depend heavily on tuning, since Adam contains momentum SGD as a limit. Adam's two states cost 8 bytes per parameter, 16 in all with mixed precision.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Adam runs with but without bias correction. Right after the first step, how does compare with ?
- It is unbiased, since it is an average of squared gradients
- It is about times too large
- It is too small by a factor , which would make the first steps too large
- It is too small by a factor , a negligible difference
Show the answer
C. It is too small by a factor , which would make the first steps too large
, because the average starts at zero. Dividing by would then inflate the step. The correction divides by , exactly the missing weight.
Why is adding an L2 penalty to the loss not the same as weight decay when training with Adam?
- Because Adam ignores the penalty's gradient
- Because the penalty's gradient is divided by , so weights with large gradient histories are decayed less than others
- Because L2 penalties only work with convex losses
- They are the same; AdamW only changes the default value of
Show the answer
B. Because the penalty's gradient is divided by , so weights with large gradient histories are decayed less than others
Inside Adam the decay rate of becomes , different for every coordinate. AdamW applies outside the preconditioner, so all weights shrink by the same factor, as with SGD.
About how much memory do the weights, gradients and optimizer states take when a 1-billion-parameter model is trained with mixed-precision Adam, by the 16-bytes-per-parameter accounting?
- 4 GB
- 8 GB
- 32 GB
- 16 GB
Show the answer
D. 16 GB
bytes for 16-bit weights and gradients, plus bytes for 32-bit master weights, and : bytes. Activations come on top. This is why optimizer states are sharded across devices in large-scale training.
Choi et al. (2019) argued that, with enough tuning, Adam should never underperform momentum SGD. What is the argument?
- Adam always converges faster on convex problems
- Adam's bias correction removes the noise that slows SGD
- Adam generalizes better because its steps are bounded
- With a large , Adam's update approaches momentum SGD with learning rate , so a more general optimizer contains its special case
Show the answer
D. With a large , Adam's update approaches momentum SGD with learning rate , so a more general optimizer contains its special case
When , the update is momentum SGD. Comparisons in which Adam loses often tuned its and betas less than they tuned SGD's learning rate.
Go deeper
- Why Adam Beats SGD on Transformers · Competing explanations: heavy-tailed noise, heterogeneous curvature and sign descent.
- Curvature-Aware Optimizers: K-FAC & Shampoo · Kronecker-factored preconditioners that make second-order ideas scale.
- Beyond Adam: Lion, Muon & Steepest Descent · Sign updates, orthogonalized momentum and descent under different norms.
Further reading
- J. Duchi, E. Hazan & Y. Singer, "Adaptive subgradient methods for online learning and stochastic optimization," Journal of Machine Learning Research 12, 2121–2159 (2011).
- D. P. Kingma & J. Ba, "Adam: A method for stochastic optimization," ICLR (2015).
- I. Loshchilov & F. Hutter, "Decoupled weight decay regularization," ICLR (2019).
- A. C. Wilson, R. Roelofs, M. Stern, N. Srebro & B. Recht, "The marginal value of adaptive gradient methods in machine learning," NeurIPS (2017).