Momentum and Nesterov Acceleration
Plain gradient descent zig-zags across steep valleys and crawls along flat ones. Momentum keeps a running velocity that averages recent gradients: oscillating components cancel and consistent ones build up. Nesterov's variant evaluates the gradient at a look-ahead point, which gives provably faster convergence on convex problems and is a common default for SGD in vision.
Heavy-ball momentum
(This is the PyTorch convention; Polyak's original puts inside the velocity, which is equivalent for a constant .) With a constant gradient , the velocity converges to , so multiplies the effective step by 10 along consistent directions. The velocity is an exponentially weighted sum of past gradients with an "memory" of about steps.
Nesterov momentum
Nesterov computes the gradient where momentum is about to take the parameters:
If the velocity is about to overshoot, the look-ahead gradient already points back and brakes earlier. On smooth convex problems it reaches error in steps instead of , and on strongly convex ones in about instead of steps per digit (Conditioning).
import numpy as np
H = np.diag([1.0, 100.0]) # condition number 100
grad = lambda th: H @ th
def run(method, beta, lr=0.009, steps=150):
th, v = np.array([1.0, 1.0]), np.zeros(2)
for _ in range(steps):
if method == "gd":
th = th - lr * grad(th)
elif method == "heavy-ball":
v = beta * v + grad(th); th = th - lr * v
else:
v = beta * v + grad(th - lr * beta * v); th = th - lr * v
return 0.5 * th @ H @ th
print(f"plain GD : loss {run('gd', 0):.2e}")
print(f"heavy-ball, 0.9 : loss {run('heavy-ball', 0.9):.2e}")
print(f"Nesterov, 0.9 : loss {run('nesterov', 0.9):.2e}")plain GD : loss 3.32e-02
heavy-ball, 0.9 : loss 1.62e-06
Nesterov, 0.9 : loss 2.03e-09
Damping the zig-zag
In the steep direction the gradient flips sign every step, and the average of alternating values is small. In the flat direction it keeps its sign and accumulates. Momentum thus acts like a low-pass filter on the gradient sequence.
import numpy as np
H = np.diag([1.0, 100.0])
for beta in (0.0, 0.9):
th, v = np.array([1.0, 1.0]), np.zeros(2)
path = []
for t in range(6):
v = beta * v + H @ th; th = th - 0.019 * v
path.append(th.copy())
print(f"beta {beta}: steep coordinate", np.round([p[1] for p in path], 3))
print(f" flat coordinate ", np.round([p[0] for p in path], 3))beta 0.0: steep coordinate [-0.9 0.81 -0.729 0.656 -0.59 0.531]
flat coordinate [0.981 0.962 0.944 0.926 0.909 0.891]
beta 0.9: steep coordinate [-0.9 -0.9 0.81 0.81 -0.729 -0.729]
flat coordinate [0.981 0.945 0.895 0.833 0.761 0.682]
In six steps momentum has moved the flat coordinate about three times as far (from 1 to 0.68, versus 0.89 without momentum), while the steep coordinate oscillates no worse than before; over more steps the velocity in the steep direction averages out.
Momentum with mini-batches
With stochastic gradients, momentum also averages out noise: the velocity is a smoothed gradient estimate over about batches. Adam's first moment is exactly this kind of average (Adam and AdamW).
In software
torch.optim.SGD(params, lr=0.1, momentum=0.9, nesterov=True, weight_decay=5e-4)is the classic ResNet recipe.- Increasing without lowering increases the effective step and can cause divergence.
- In practice Nesterov's look-ahead is implemented with a change of variables so that only one gradient per step at the stored parameters is needed.
Exercises
Exercise 1: Effective step
With and , what is the steady-state step size along a constant gradient direction?
Answer
times the gradient.
Solution
, so each step moves .
Exercise 2: Velocity by hand
Starting from with and gradients , compute .
Answer
, , .
Solution
; ; .
Exercise 3: Memory of the average (code)
The velocity weights the gradient from steps ago by . Check that for the weights sum to about , and find after how many steps a gradient's weight falls below .
Answer
Sum about 10; about 10 steps.
Solution
import numpy as np
w = 0.9 ** np.arange(1000)
print(round(w.sum(), 4), int(np.argmax(w < np.exp(-1))))10.0 10
Test yourself
With momentum and a constant gradient, the steady-state velocity is…
What distinguishes Nesterov momentum from heavy-ball momentum?
- It uses no velocity
- It evaluates the gradient at the look-ahead point
- It uses second derivatives
- It only works on GPUs
Why does momentum reduce zig-zagging in a steep valley?
- It increases the learning rate in every direction equally
- Gradients that alternate in sign cancel in the running average
- It normalizes the gradient
- It removes the steep direction