Momentum and Adam
Momentum gives gradient descent a memory: it steps along a running average of recent gradients, so pushes that keep pointing the same way add up while zig-zags cancel out. Adam also keeps a running average of the squared gradients, so each weight gets a step size scaled to its own typical gradient.
What the symbols mean
- Velocity
A running sum of recent gradients, older ones weighted less.
- Momentum coefficient
How much of the old velocity is kept each step, typically 0.9.
What the symbols mean
- Moving average
The average after step t, weighting recent values most.
- Newest value
The gradient at step t.
What the symbols mean
- Corrected average gradient
The moving average of g, divided by 1 − β₁ᵗ to undo its start at 0.
- Corrected average squared gradient
The moving average of g², corrected the same way. Its square root is the gradient's typical size.
- Safety term
A tiny number like 10⁻⁸ that prevents division by zero.
Explanation
Momentum is simply giving gradient descent a memory, the way a ball rolling down a hill keeps some of its speed instead of starting fresh at every spot. The update has two pieces: (most of the old velocity, kept because is a number like ) and (today's slope, added in). Then the weights move along instead of along the raw gradient. Adam, the default optimizer for most networks today, uses the same running-average trick twice: once for the direction and once for the size. Each piece is a short line of arithmetic; it's the combination that works so well.
Why plain gradient descent zig-zags. Go back to the long, narrow valley from The Learning Rate and Convergence. The steep walls force a small , and even then the steep coordinate overshoots, flipping sign every step, while the coordinate along the valley floor crawls. Look at the gradients that produces: across the valley they alternate , and along the floor they point the same way every time. What we'd like is to cancel the first kind and amplify the second. Averaging does exactly that.
What momentum does. Unroll the velocity, starting from :
If the gradient stays the same, , this is a geometric series: . With , that's ten times the plain step along a consistent direction. If the gradient alternates, , the series alternates too, and the velocity settles to a size of only , about half of when . Momentum speeds up the directions that agree from step to step and damps the ones that flip. There's an animation further down of momentum straightening out a zig-zag.
Exponential moving averages. The same unrolling explains the moving average , which momentum and Adam are both built on. Starting from ,
The weights shrink exponentially as you go back in time, hence the name. Add them up with the geometric sum formula:
As grows that heads to , so in the long run is a true weighted average. Its memory is about steps: roughly the last values for , the last for . (Momentum's is this average without the in front, which just makes it times bigger; the learning rate absorbs the difference.)
Bias correction. Here's the catch with starting from . Early on the weights add up to only , not , so the average comes out too small. After the first step with , : just a tenth of the actual gradient. The fix is to divide by the sum of the weights, , which makes them add up to again. If every gradient equals , then and exactly. The correction matters only at the start, since .
Adam. Adam (short for adaptive moment estimation: the mean is the first moment, the mean square the second) keeps two moving averages for every weight:
usually with and . It bias-corrects both and steps by . Think about what that ratio means. is the typical size of this weight's gradient (its root mean square), so is "the average direction, measured in units of the gradient's usual size". When the gradient is consistent, it's about , and the step is about , whether the raw gradient is or . Weights with huge gradients get calmed down and weights with tiny gradients get boosted, so every weight effectively gets its own learning rate. That undoes much of the stretched-bowl problem from the learning-rate lesson. A neat check: on the very first step and , so the step is exactly.
A common mistake. It's tempting to skip bias correction as a minor detail. For Adam it isn't. With the uncorrected has square root , while the uncorrected , so the first steps would come out about times too large, right when the weights are at their most random. Too much momentum is the other trap: with close to the ball rolls past the bottom and circles around it before settling.
See it
Worked examples
Gradient descent with momentum uses and , starting with . The gradient is at each of three steps in a row. Find the velocity and the weight change at each step, and what the steps grow to in the long run.
- 1
Step 1: , so the weight moves by . The first step is exactly plain gradient descent.
Run Adam for two steps on one weight starting at , with , , and . The gradients are and .
- 1
Step 1, the first average: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Momentum updates its velocity by , with . Starting from , it sees the gradient three times in a row. What is the velocity after the third step?
Momentum updates its velocity by , with . It has been seeing the same gradient, , for a very long time. What value does the velocity settle to?
An exponential moving average with starts from . After steps, is a weighted sum of . What do those weights add up to?
An exponential moving average with starts from and sees , then . What is the bias-corrected average ?
On its very first step, Adam (, ) sees the gradient . By how much does the weight change? (Give the change with its sign.)
About how many recent steps does a moving average with remember? (Use the rule of thumb .)
An optimizer uses momentum, and , with and . In one direction the gradient has been a steady for many steps. How far does the weight move per step in that direction now?
In a narrow valley, the gradient across the valley alternates forever. Momentum updates its velocity by with . What size does the velocity's swing settle to?
The hardest one here. Get it, and you've got this lesson.
Run Adam on one weight starting at , with , , and . The first gradient is and the second is . Find .
Where it's used
Adam and its close cousin AdamW, which adds weight decay, train nearly every large language model and image generator today, usually with , between and , and a learning rate around . Momentum with is still the standard choice for many image classifiers. The same exponential moving average shows up all over engineering: smoothing a noisy sensor reading, tracking a stock's trend line, or estimating network round-trip times so a computer knows how long to wait before resending a packet.