Momentum-Based Gradient Descent and Nesterov Accelerated Gradient
On this page 9 headings
Compare momentum with a look-ahead gradient
Use L(w) = ½w², current w = 3, previous accumulator u = 2, β = 0.9 and η = 0.1. Here η is outside the accumulator, including in the look-ahead distance.
Ordinary momentum uses the gradient at the current point.
For this NAG convention, first find the look-ahead point.
Use the gradient there, then update from the original current point.
What changed? NAG changes where the gradient is measured. This example names its scaling convention explicitly; do not mix it with code that absorbs η into the accumulator.
Quick check: Why is the NAG accumulator a little smaller here?
The look-ahead point is closer to the bowl’s minimum, so its gradient is 2.82 instead of 3.
Swipe long formulas, tables or code sideways to see the rest.
Start here If someone keeps telling you “go left… go left… go left”, you start walking faster to the left. Momentum does exactly this: it remembers past gradients and builds up speed. It crosses flat regions quickly — but it can overshoot. Nesterov Accelerated Gradient (NAG) fixes the overshooting by looking ahead before stepping.
If I am repeatedly asked to move in the same direction, I should gain confidence and take bigger steps in that direction — just like a ball rolling down a slope gains momentum.
with random, and ( is the momentum factor, often 0.9).
So besides the current gradient, we also look at the history of updates.
- In general:
is an exponentially weighted sum of the current and all past gradients. Recent gradients get weight close to 1; old ones fade .
Feel it: if the gradient is always 1 and :
| t | |
|---|---|
| 0 | 1 |
| 1 | |
| 2 | |
| 3 | |
| … | grows towards 10 |
So on a long gentle slope, momentum can take steps up to 10 times bigger than plain gradient descent.
def do_mgd(max_epochs):
w, b, eta = -2, -2, 1.0
prev_uw, prev_ub, beta = 0, 0, 0.9
for i in range(max_epochs):
dw, db = 0, 0
for x, y in zip(X, Y):
dw += grad_w(w, b, x, y)
db += grad_b(w, b, x, y)
uw = beta * prev_uw + eta * dw
ub = beta * prev_ub + eta * db
w = w - uw # the slide writes "w - vw" — a typo; it must be uw
b = b - ub # likewise "b - vb" should be ub
prev_uw = uw
prev_ub = ubNote: the code puts inside (u = βu + η∇w, then ). The formula above puts outside . With a fixed these are the same thing — the code’s u is just times the formula’s u.
Good: even on gentle slopes, momentum takes large steps because the built-up momentum carries it along.
Bad: it can overshoot the goal. In a narrow valley (error high on both sides of the minimum), momentum oscillates in and out of the valley and takes many U-turns before settling.
Lecture example — the race Start , on a surface with a narrow valley. After 50 iterations, momentum has reached an error of 0.00001, while plain gradient descent is still stuck at 0.16. So, despite the U-turns, momentum still converges much faster than plain gradient descent.
Can we reduce the oscillations? Yes.
From we know we are going to move at least by , and then a bit more by the gradient. So why not first jump to where momentum will take us, measure the gradient there, and then decide?
NAG update:
(In the lecture’s code form, where is inside u, the look-ahead point is prev_v; in the formula form above, it is . Same idea: step to where momentum is taking you, then measure.)
def do_nag(max_epochs):
w, b, eta = -2, -2, 1.0
prev_vw, prev_vb, beta = 0, 0, 0.9
for i in range(max_epochs):
dw, db = 0, 0
# partial update: where momentum alone would take us
v_w = beta * prev_vw
v_b = beta * prev_vb
for x, y in zip(X, Y):
# look ahead: gradient at the look-ahead point
dw += grad_w(w - v_w, b - v_b, x, y)
db += grad_b(w - v_w, b - v_b, x, y)
vw = beta * prev_vw + eta * dw
vb = beta * prev_vb + eta * db
w = w - vw
b = b - vb
prev_vw = vw
prev_vb = vbWhy it helps: if momentum is about to overshoot, the gradient at the look-ahead point already points back, so NAG corrects its course earlier. Its oscillations are smaller, and it is less likely to escape the minimum valley.
| Method | Uses | Strength | Weakness |
|---|---|---|---|
| Gradient descent | current gradient only | stable | very slow on gentle slopes |
| Momentum | current + past gradients | fast on gentle slopes | overshoots, many U-turns |
| NAG | gradient at the look-ahead point + past | fast, shorter U-turns | slightly more bookkeeping |
Worked comparison (, start , so ):
| Step | GD | Momentum | NAG |
|---|---|---|---|
| 1 | 0.8 | 0.8 | 0.8 |
| 2 | 0.64 | 0.46 | 0.496 |
| 3 | 0.512 | 0.062 | 0.178 |
| 4 | 0.410 | −0.309 (overshot past 0) | −0.087 (smaller overshoot) |
(Full arithmetic for these steps is on the How to Solve page.)
In one line: Momentum adds a decaying memory of past gradients to speed through flat regions (but overshoots); NAG measures the gradient at the look-ahead point to cut down the overshooting.
Exam words: momentum, , exponentially weighted average, overshoot, oscillation, U-turn, Nesterov Accelerated Gradient, look-ahead, look before you leap.
