JA EN
LearnCalculus & Optimization
·★ MEMBER·PAPER·12 min read

Beyond SGD — Adam, Second-Order Methods, and Constrained Optimization

What exactly is momentum accumulating? What does each of Adam's four lines do? What did AdamW fix? And why does nobody train an LLM with second-order methods that are supposedly faster? Metaphor, equations, live figures, code, and production practice — no prerequisites assumed.

ModalitytextTaskmath

Adam: A Method for Stochastic Optimization


A metaphor: descending a narrow canyon by feel

You are in fog, finding the valley floor using nothing but the slope under your feet — the picture from Calculus for AI. Let's push it one step further.

This time the terrain is a narrow canyon: steep walls on both sides, nearly flat along the canyon floor. You dutifully walk in whatever direction is steepest downhill. But that direction is not along the floor — it is straight across the canyon. One step lands you on the opposite wall, where the steepest direction again points across. You bounce from wall to wall and creep only inches toward the exit.

This is not a fanciful picture; it is everyday life on a loss surface. If curvature differs by a factor of 100 between directions, your step size has to accommodate the steepest one, so the flat direction advances a hundred times slower. SGD is slow not because the gradient is wrong, but because "steepest" and "where you want to go" are different directions. There are four ways out: accumulate (momentum), give each direction its own step size (Adam), measure the curvature itself (second-order methods), and fence off where you're allowed to move (constrained optimization).

Recap: what SGD actually does

θt+1=θtηgt,gt=θLBt(θt)\theta_{t+1} = \theta_t - \eta\, g_t, \qquad g_t = \nabla_\theta \mathcal{L}_{B_t}(\theta_t)
(1)

In plain words: from the current parameters θt\theta_t, move a distance set by the learning rate η\eta in the direction opposite to gtg_t, the "gets worse" direction measured on the data you happen to be holding. That is all. BtB_t is the minibatch you drew this step — a bundle of tens to hundreds of examples. Put in words: measure the slope under your feet using a few hundred examples, take one fixed-length step the other way, and repeat forever.

Two problems live inside that line. One is the direction problem from the canyon. The other is noise: gtg_t is not the true gradient over the full dataset but an estimate from a few hundred samples, so standing in the same spot on different batches gives you different arrows. Conveniently, one tool relieves both at once.

Momentum: keep it as velocity instead of discarding it

SGD has no memory of previous steps. Give it velocity.

vt=βvt1+gt,θt+1=θtηvtv_t = \beta\, v_{t-1} + g_t, \qquad \theta_{t+1} = \theta_t - \eta\, v_t
(2)

Read aloud: velocity vtv_t is "the previous velocity decayed by a factor β\beta" plus "this step's gradient," and the parameters move by that velocity. β\beta is a decay rate between 0 and 1; 0.9 is the conventional choice. Put in words: rather than measuring a fresh step from scratch each time, you carry over nine tenths of the push you already had and add today's slope on top of it.

What 0.9 means falls out of 1/(1β)=101/(1-\beta) = 10: you are roughly summing the last ten steps' gradients. That sum kills both problems at once.

If SGD is a hiker who stops and re-measures the ground at every step, momentum is a heavy ball rolling down the slope.

FIG 1A descent path moving across the contours of a narrow valley. Set momentum to 0 and it ricochets between the walls; raise it to 0.9 and the sideways swings cancel while progress along the floor stretches out. Push it too high and it overshoots into oscillation.

The side effect is overshoot. Velocity survives arrival at the bottom, so the ball sails past it. Raising β\beta buys acceleration at the cost of stopping power — that tug-of-war sits behind everything that follows.

A step size per direction: AdaGrad to RMSProp

Momentum fixed the direction, but the step size is still shared by every parameter. So give each parameter its own. The raw material is how large that direction's gradient has been in the past: shrink the step where big gradients arrive often, stretch it where they rarely do. That is AdaGrad's idea. The catch is that AdaGrad sums all past squared gradients, so the denominator grows monotonically and the step size dies toward zero. RMSProp replaces "all" with "recent."

vt=β2vt1+(1β2)gt2,θt+1=θtηvt+εgtv_t = \beta_2 v_{t-1} + (1-\beta_2)\, g_t^2, \qquad \theta_{t+1} = \theta_t - \frac{\eta}{\sqrt{v_t}+\varepsilon}\, g_t
(3)

In plain words: keep a running average vtv_t of recent squared gradients for each parameter, and divide the learning rate by its square root. Here gt2g_t^2 is elementwise squaring and ε\varepsilon is a small constant (around 10810^{-8}) that prevents division by zero. Since vt\sqrt{v_t} is "the typical magnitude of the gradient in that direction," dividing by it means rescaling every gradient to roughly unit size before you step. Think of it in words: the directions that always shout get their volume turned down and the ones that rarely speak get turned up, so every direction reaches you at the same loudness before you decide where to walk.

That gives us two ingredients. Momentum answers which way to go; RMSProp answers how far to go. They track different statistics, so they can coexist without interfering.

Do both — that is Adam (Adaptive Moment Estimation).

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Adam: A Method for Stochastic Optimization. arXiv:1412.6980Paper page·PDF
  2. Decoupled Weight Decay Regularization. arXiv:1711.05101Paper page·PDF
  3. On the difficulty of training Recurrent Neural Networks. arXiv:1211.5063Paper page·PDF
  4. Optimizing Neural Networks with Kronecker-factored Approximate Curvature. arXiv:1503.05671Paper page·PDF
  5. Shampoo: Preconditioned Stochastic Tensor Optimization. arXiv:1802.09568Paper page·PDF
  6. Adafactor: Adaptive Learning Rates with Sublinear Memory Cost. arXiv:1804.04235Paper page·PDF
  7. ZeRO: Memory Optimizations Toward Training Trillion Parameter Models. arXiv:1910.02054Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment