Answers · The mathematics underneath
How does momentum stop the steps from cancelling each other out?
In shortMomentum keeps a running total of past gradients, so directions that keep agreeing build up while directions that keep flipping sign cancel out.
Momentum stops the steps cancelling each other out by not taking each gradient as a separate step. It keeps a running total, so a component that points the same way at every step is added to its own past and grows, while a component that flips sign is subtracted by its own past and shrinks. The parameter update uses that total, not the current gradient alone. The velocity carries the agreement; the gradient only nudges it.
Unroll the recursion and the running total is a weighted list of every gradient so far: . The weight on a gradient from steps ago is . With a gradient ten steps old still carries , which is about , and one fifty steps old carries , about .
Take a direction where the gradient is at every step. Starting from zero velocity, the first four values are , , , and . Every term has the same sign, so every term adds. The sum is , which is . The step length is therefore , not .
Now take the same coefficient and a direction where the gradient is , the pattern produced by overshooting a narrow valley. Starting from zero velocity, , , , and . The positive history is being subtracted by the next negative gradient, so the total never builds. The alternating sum is , which is , about .
| step | gradient (steady) | velocity (steady) | gradient (alternating) | velocity (alternating) |
|---|---|---|---|---|
| 1 | 1 | 1.0 | 1 | 1.0 |
| 2 | 1 | 1.9 | -1 | -0.1 |
| 3 | 1 | 2.71 | 1 | 0.91 |
| 4 | 1 | 3.439 | -1 | -0.181 |
The first sum has all plus signs and the second has alternating signs. That is the whole difference between a direction that agrees with itself and one that does not. At the first sum is and the second is , so the same running total treats the two directions differently by a factor of .
Momentum is therefore not smoothing in the sense of averaging away noise. It is a weighted sum in which age is discounted and sign is preserved. A past gradient is never forgotten outright; it is multiplied by a power of and added.
The natural guess
The natural guess is that momentum simply adds speed, so both directions should get a larger step. That guess fails because the running total is not a multiplier applied equally to every component. It is a sum whose signs matter. A direction that alternates meets its own past with the opposite sign, so the old contribution cancels the new one. A direction that agrees meets its own past with the same sign, so the old contribution reinforces the new one. The cancellation has not been removed; it has been pushed into the history, where it hits the zigzag and leaves the valley direction alone.
What the coefficient decides
The ratio between the two limits is . At that is : the steady direction is nineteen times larger than the alternating one. At the steady multiplier is and the alternating one is , about , so the ratio is about . Raising the coefficient does not make both directions faster by the same amount. It widens the gap, which is why a high coefficient straightens a zigzag but also makes the stable step length much larger.
Where it stops
This picture assumes the gradient repeats, either steadily or with a clean sign flip. When the gradient changes irregularly, the geometric sum is still a filter, but it is filtering a moving signal rather than a fixed one. A high coefficient then keeps stale directions alive for many steps, and a noisy or zero-mean gradient can be accumulated as easily as a real slope. The other limit is the step length. The learning rate really in force is . With and that is ; with it is . The learning rate has not changed, but the step has grown by a factor of ten, and a run that was stable can diverge.
Worth keeping
- With beta = 0.9 a steady gradient builds a velocity ten times the gradient, so the real learning rate is ten times the number set.
- With the same beta an alternating gradient builds to only about 0.526 of a single gradient, a ratio of 19 to the steady case.
- Raising beta from 0.9 to 0.99 raises the steady multiplier from 10 to 100, so a stable learning rate can become unstable without any change to the learning rate.
Where this goes next
This question is one step of The Mathematics of Gradient Descent, a course that works through its subject from the beginning. The page that teaches this part of it is Momentum, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why is the negative gradient the direction of steepest descent?A unit step goes downhill fastest when it points directly opposite the gradient, because the dot product with the gradient is then as negative as it can be.
- Why does computing the gradient cost about the same no matter how many parameters the network has?Because one backward sweep shares intermediate derivatives across all parameters, so the whole gradient costs a few forward passes in total rather than one per parameter.
- Why does Adam need bias correction at the start of training?Adam needs bias correction because its two running averages start at zero, and without correction the early steps are several times larger than the learning rate asks for.
- Why does gradient descent still work when each step uses only one minibatch's gradient?A minibatch gradient is wrong at every step but correct on average, so the errors cancel over many steps while the true downhill pull keeps adding up.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.