Contents
07Momentum and Adam
7.1The trouble with narrow valleys
Section 3 showed that a large condition number hurts gradient descent: the steep directions force a small learning rate, and the flat directions are then slow. Consider the valley
with \kappa=100, starting at (-9,1) where J=90.5. With \eta=0.019, just under the limit 2/100, the steep coordinate is multiplied by 1-1.9=-0.9 per step: gradient descent zigzags across the walls. The flat coordinate is multiplied by 0.981: it crawls along the floor. After 80 steps the loss is still 1.881 (Figure 5); no constant learning rate does much better (the best one, \eta=0.0195, reaches 1.747). Two ideas fix this, and every modern optimizer uses one or both.
7.2Momentum: a heavy ball
The first idea is to remember the past. Polyak (1964) replaced the gradient by a running sum of past gradients, the velocity:
with 0\le\beta<1, typically 0.9 (Murphy, 2022, §8.2.4). Unrolling, v_{t+1}=\sum_{k=0}^{t}\;\beta^k\,\nabla J(\theta_{t-k}), an exponentially weighted sum of past gradients. Its effect depends on whether the gradients agree:
- a steady slope (gradients equal to g): v\to g/(1-\beta), so steps grow up to 1/(1-\beta) times longer, ten times for \beta=0.9;
- a zigzag (gradients alternating +g,-g): the terms cancel, |v|\to g/(1+\beta), and the oscillation is damped.
Like a heavy ball rolling down the valley, it builds up speed along the floor while its inertia smooths out the side-to-side motion. In the valley, momentum with \eta=0.004 and \beta=0.9 reaches J=0.016 after 80 steps, a hundred times lower than gradient descent (Table 2).
Why it is faster. Subtracting consecutive updates gives the heavy-ball form \theta_{t+1}=\theta_t-\eta\nabla J(\theta_t)+\beta(\theta_t-\theta_{t-1}). On a coordinate with curvature \lambda the error obeys e_{t+1}=(1+\beta-\eta\lambda)e_t-\beta e_{t-1}, whose rate is set by the roots z of z^2-(1+\beta-\eta\lambda)z+\beta=0. Their product is \beta, so whenever they are complex, |z|=\sqrt\beta. Tuning \beta and \eta for \lambda_{\min} and \lambda_{\max}, Polyak (1964) obtained the rate
with \beta=\big((\sqrt\kappa-1)/(\sqrt\kappa+1)\big)^2. For \kappa=100 that is 9/11=0.818 per step instead of 0.980: one decimal digit every 11.5 steps instead of every 115. Momentum replaces \kappa by \sqrt\kappa.
Nesterov momentum evaluates the gradient where the ball is about to be, v_{t+1}=\beta v_t+\nabla J(\theta_t-\eta\beta v_t): it looks one step ahead and corrects before overshooting. On smooth convex problems it guarantees an error that falls like 1/t^2 (Nesterov, 1983), and Sutskever et al. (2013) showed that it trains deep networks well.
7.3A step size for every parameter
The second idea is to give every parameter its own learning rate, scaled by how large its gradients have been. With g_t=\nabla J(\theta_t) and all operations taken coordinate by coordinate,
A parameter with large gradients (a steep wall) gets small steps; a parameter with small gradients (a flat floor, or a rare feature) gets large ones.
- AdaGrad (Duchi et al., 2011): s_t=s_{t-1}+g_t^2, the sum of all past squared gradients. Excellent for sparse features such as rare words, but s only grows, so the steps shrink toward zero and training can stall.
- RMSProp (Tieleman and Hinton, 2012): s_t=\rho\,s_{t-1}+(1-\rho)\,g_t^2, an exponential moving average (typically \rho=0.9), so the step size tracks the recent gradient scale instead of shrinking forever.
7.4Adam
Adam (Kingma and Ba, 2015) combines the two ideas: a momentum average m_t of the gradients and an RMSProp average s_t of their squares,
with the bias-corrected averages \hat m_t=m_t/(1-\beta_1^t) and \hat s_t=s_t/(1-\beta_2^t). The defaults are \eta=0.001, \beta_1=0.9, \beta_2=0.999 and \epsilon=10^{-8} (Kingma and Ba, 2015); Murphy (2022, §8.4.6) gives the same values with \epsilon=10^{-6}.
Why the bias correction. Both averages start at zero. Unrolling, m_t=(1-\beta_1)\sum_{k=1}^{t}\;\beta_1^{t-k}g_k, and if the gradients have a constant mean \E[g],
Early on the average is biased toward zero: at t=1, m_1=0.1\,g_1 and s_1=0.001\,g_1^2, and after ten steps m still holds only 1-0.9^{10}=65\% of the mean gradient. Dividing by 1-\beta^t removes the bias. Without it, the first step would be 0.1/\sqrt{0.001}=3.16 times too large in every coordinate.
Worked example 3 (Adam’s first step). At t=1 the corrected averages are \hat m_1=g_1 and \hat s_1=g_1^2, so (ignoring \epsilon) the first step is -\eta\,g_1/|g_1|=-\eta\,\mathrm{sign}(g_1) in every coordinate, whatever the size of the gradient. In the valley the gradient at (-9,1) is (-9,100), eleven times larger in \theta_2, yet Adam’s first step with \eta=0.3 is exactly (+0.3,-0.3). Adam’s steps are invariant to rescaling the gradient, and their size is set by \eta: that is why its learning rate is easy to choose and why 0.001 works across many problems.
| method | settings | J after 40 steps | J after 80 steps |
|---|---|---|---|
| gradient descent | \eta=0.019 | 8.74 | 1.881 |
| momentum | \eta=0.004, \beta=0.9 | 1.153 | 0.016 |
| Adam | \eta=0.3, \beta_1=0.9, \beta_2=0.999 | 0.348 | 0.005 |
7.5AdamW and weight decay
L2 regularization adds \lambda\theta to the gradient. In plain SGD this is the same as shrinking the weights by a factor 1-\eta\lambda every step, which is why the two names are used interchangeably. In Adam they are not the same: the added \lambda\theta is divided by \sqrt{\hat s} like the rest of the gradient, so parameters with large gradients are regularized less. AdamW (Loshchilov and Hutter, 2019) decouples the decay from the adaptive step,
and with this change Adam competes with SGD and momentum on image classification, where it used to fall behind (Loshchilov and Hutter, 2019). AdamW is now the standard optimizer for transformers. LLaMA, for example, was trained with AdamW (\beta_1=0.9, \beta_2=0.95, weight decay 0.1), 2{,}000 warmup steps and a cosine learning-rate schedule (Touvron et al., 2023).
Which optimizer? A reasonable default today is AdamW with warmup and decay for transformers and most new models, and SGD with momentum 0.9 and a step or cosine schedule for convolutional image models, where it often generalizes slightly better. Whatever the optimizer, the learning rate is the first hyperparameter to tune.
Pause and think. Why does momentum need a smaller learning rate than plain gradient descent in Table 2?
Show the answer
On a steady slope momentum multiplies the step by up to 1/(1-\beta)=10, so \eta=0.004 behaves like 0.04 along the floor. With \eta=0.019 the steep coordinate would rotate by 90^\circ per step (\eta\lambda=1.9 makes the roots \pm0.949i), a slowly decaying square wave that looks worse than plain gradient descent.