Visualized ML
Topics

B · Neural networks

Optimizers: Momentum & Adam

Smarter steps than plain gradient descent: remember past gradients to roll through valleys, and scale each weight's step to its own gradients.

Try it: B6 · Optimizers

Intuition

Plain gradient descent looks only at the slope right where it stands. On real loss surfaces that is a handicap. They are full of long, narrow valleys: steep across, gently sloped along. The gradient points mostly across the valley, so gradient descent bounces from wall to wall and creeps along the bottom. A learning rate small enough to stop the bouncing makes the creeping slower still.

Optimizers fix this with memory. Momentum keeps a running velocity: the steps across the valley alternate in sign and cancel out, the steps along it agree and add up, like a ball rolling downhill that picks up speed. Adaptive methods such as RMSProp and Adam keep, for each weight separately, a running average of how large its gradients have been, and divide by it: steep directions get smaller steps, flat ones larger steps.

Three optimizers start at the same point of a long, narrow valley (contour lines). Gradient descent zig-zags across it. Momentum gathers speed along the valley, overshoots and swings back. Adam rescales each direction and heads almost straight for the minimum.

The math

Each method updates the weights \(\mathbf{w}\) from the gradient \(\mathbf{g}_t = \nabla L(\mathbf{w}_t)\) at step \(t\), with learning rate \(\eta\).

Gradient descent

\[ \mathbf{w}_{t+1} = \mathbf{w}_t - \eta\,\mathbf{g}_t \]

Momentum

\[ \mathbf{v}_{t+1} = \beta\,\mathbf{v}_t + \mathbf{g}_t, \qquad \mathbf{w}_{t+1} = \mathbf{w}_t - \eta\,\mathbf{v}_{t+1} \]

With \(\beta = 0.9\), the velocity is roughly the sum of the last ten gradients. Where gradients agree, the step is up to \(1/(1-\beta) = 10\) times larger than plain gradient descent's.

RMSProp

\[ \mathbf{s}_{t+1} = \rho\,\mathbf{s}_t + (1-\rho)\,\mathbf{g}_t^2, \qquad \mathbf{w}_{t+1} = \mathbf{w}_t - \frac{\eta\,\mathbf{g}_t}{\sqrt{\mathbf{s}_{t+1}} + \epsilon} \]

Squares, roots and divisions are per weight. Each weight's step is its gradient divided by the typical size of its recent gradients.

Adam

Adam combines both: a momentum-like average of gradients \(\mathbf{m}\) and an RMSProp-like average of squared gradients \(\mathbf{s}\), each corrected for starting at 0:

\[ \mathbf{m}_{t} = \beta_1\mathbf{m}_{t-1} + (1-\beta_1)\mathbf{g}_t, \qquad \mathbf{s}_{t} = \beta_2\mathbf{s}_{t-1} + (1-\beta_2)\mathbf{g}_t^2 \] \[ \hat{\mathbf{m}}_t = \frac{\mathbf{m}_t}{1-\beta_1^t}, \quad \hat{\mathbf{s}}_t = \frac{\mathbf{s}_t}{1-\beta_2^t}, \qquad \mathbf{w}_{t+1} = \mathbf{w}_t - \frac{\eta\,\hat{\mathbf{m}}_t}{\sqrt{\hat{\mathbf{s}}_t} + \epsilon} \]

The usual settings are \(\beta_1 = 0.9\), \(\beta_2 = 0.999\), \(\epsilon = 10^{-8}\), and \(\eta\) around \(10^{-3}\).

Worked example. The valley in the animation is \(L = \tfrac12(x^2 + 20y^2)\). At the start, \((-4, 1.2)\), the gradient is \((-4, 24)\): six times steeper across than along.

Gradient descent with \(\eta = 0.09\) moves by \((0.36, -2.16)\), to \((-3.64, -0.96)\): it jumps right over the valley floor and barely moves along it.

Adam's first step: \(\hat{\mathbf{m}} = (-4, 24)\), \(\hat{\mathbf{s}} = (16, 576)\), so \(\hat{\mathbf{m}}/\sqrt{\hat{\mathbf{s}}} = (-1, 1)\). Both coordinates move by exactly \(\eta\), whatever their gradients' size: the steep direction is tamed and the flat one is boosted.

Variants and practice

  • Nesterov momentum computes the gradient at the point momentum is about to carry you to, a small look-ahead that damps overshooting.
  • AdamW applies weight decay directly to the weights instead of adding it to the gradient, where Adam's per-weight scaling would distort it. It is the default for training transformers. See Regularization.
  • Learning-rate schedules matter as much as the optimizer: a short warm-up from a tiny rate, then a slow decay (cosine or linear) is the common recipe.
  • SGD with momentum is still often the choice for image models, where it can generalize slightly better than Adam once well tuned. Adam is easier to get working on a new problem.
  • Memory. Adam keeps two extra numbers per weight, tripling the optimizer's memory. Variants such as Adafactor and 8-bit Adam reduce this for very large models.

Pitfalls

  • Comparing at one learning rate. Every optimizer has its own good range. Compare each at its best rate, or the comparison says more about the rate than the method.
  • Too much momentum. \(\beta\) close to 1 with a large \(\eta\) overshoots and oscillates, as the animation shows.
  • Adaptive is not tuning-free. Adam still needs a learning rate, and a bad one still fails.
  • Resetting state. When you resume training from a checkpoint, restore the optimizer's state too; a fresh Adam starts with large, poorly scaled steps.

History

Boris Polyak introduced momentum ("the heavy ball method") in 1964, and Yurii Nesterov his accelerated variant in 1983. AdaGrad (Duchi, Hazan and Singer, 2011) introduced per-weight step sizes; Geoffrey Hinton proposed RMSProp in a 2012 lecture, never in a paper. Diederik Kingma and Jimmy Ba published Adam in 2014, and Ilya Loshchilov and Frank Hutter AdamW in 2017.

Connections

Builds on

Further reading