Step Size and Momentum
Plain gradient descent has two weaknesses: a fixed step size is either too timid or too reckless, and on ill-conditioned problems it zig-zags. Line search and adaptive step sizes address the first; momentum addresses the second by letting the iterate build up speed in consistent directions while oscillations cancel out.
Choosing the step size
- Fixed: simple; must satisfy on quadratics.
- Backtracking line search: start with a large and halve it until the loss decreases enough. The Armijo condition asks for with a small such as .
- Schedules: in deep learning, is decayed over training (step, cosine or inverse-square-root schedules) instead of searched (Learning-Rate Schedules).
Momentum
Gradient descent with momentum keeps a running velocity:
with , typically . The velocity is an exponentially weighted sum of past gradients. Components that keep the same sign accumulate (up to a factor , so 10 times for ); components that flip sign every step cancel. Physically, it is a heavy ball rolling downhill with friction.
import numpy as np
A = np.diag([1.0, 50.0]) # ill-conditioned bowl, kappa = 50
f = lambda x: 0.5 * x @ A @ x
x0 = np.array([10.0, 1.0])
def run(lr, alpha, steps=100):
x, v = x0.copy(), np.zeros(2)
for _ in range(steps):
v = alpha * v - lr * (A @ x)
x = x + v
return f(x)
print(f"plain GD (lr=0.035): f = {run(0.035, 0.0):.3e}")
# heavy-ball tuning for a quadratic: lr = 4/(sqrt(L)+sqrt(mu))^2, alpha = ((sqrt(k)-1)/(sqrt(k)+1))^2
L, mu = 50.0, 1.0
lr = 4 / (np.sqrt(L) + np.sqrt(mu)) ** 2
alpha = ((np.sqrt(L / mu) - 1) / (np.sqrt(L / mu) + 1)) ** 2
print(f"momentum (lr={lr:.4f}, alpha={alpha:.3f}): f = {run(lr, alpha):.3e}")
print(f"momentum (lr=0.035, alpha=0.9): f = {run(0.035, 0.9):.3e}")plain GD (lr=0.035): f = 4.022e-02
momentum (lr=0.0614, alpha=0.566): f = 1.496e-19
momentum (lr=0.035, alpha=0.9): f = 1.452e-03
With well-chosen parameters, momentum improves the rate from about steps per digit to about , a huge gain when is in the thousands.
Nesterov's variant
Nesterov accelerated gradient evaluates the gradient at the look-ahead point instead of . It corrects the course before overshooting and has the optimal worst-case rate among first-order methods on smooth convex functions.
Backtracking line search in code
import numpy as np
def f(x): return (x[0] - 1) ** 2 + 10 * (x[1] - x[0] ** 2) ** 2 # a curved valley
def grad(x): return np.array([2 * (x[0] - 1) - 40 * x[0] * (x[1] - x[0] ** 2), 20 * (x[1] - x[0] ** 2)])
x = np.array([-1.0, 1.0])
evals = 0
for it in range(300):
g = grad(x); t = 1.0
while f(x - t * g) > f(x) - 1e-4 * t * (g @ g):
t *= 0.5; evals += 1
x = x - t * g
print("after 300 steps:", np.round(x, 4), " f =", f"{f(x):.2e}", " step halvings:", evals)after 300 steps: [1. 1.] f = 0.00e+00 step halvings: 1
Backtracking needs no tuning, but it costs extra function evaluations and is unusual in deep learning, where the loss is only seen through noisy mini-batches.
In software
torch.optim.SGD(params, lr, momentum=0.9, nesterov=True).scipy.optimize.minimizemethods such as BFGS and L-BFGS use line searches satisfying Wolfe conditions.- Adam combines momentum with per-parameter step sizes (Adam and AdamW).
Go further: Momentum and Nesterov shows where momentum shines, on a curved valley, and the same update in PyTorch.
Exercises
Exercise 1: Effective step
With momentum and a constant gradient , what is the steady-state velocity?
Answer
.
Solution
At steady state , so and : momentum multiplies the effective step by along consistent directions.
Exercise 2: Armijo check
At for , the gradient is . Do the steps and satisfy ?
Answer
: yes. : no.
Solution
Starting from , backtracking halves once and lands exactly on the minimum.
Exercise 3: Momentum on a 1D quadratic (code)
Minimize from with , comparing and after 20 steps.
Answer
Plain GD reaches about ; with the iterate is still far larger. Momentum does not help on this easy problem.
Solution
for alpha in (0.0, 0.5):
x, v = 1.0, 0.0
for _ in range(20):
v = alpha * v - 0.05 * 10 * x
x = x + v
print(alpha, f"{abs(x):.2e}")0.0 9.54e-07
0.5 9.77e-04
With and , plain GD multiplies by each step, which is already fast on this perfectly conditioned 1D problem. Momentum helps on ill-conditioned problems, not on easy ones.
Test yourself
Momentum mainly helps by…
- computing exact second derivatives
- accumulating consistent gradient directions while oscillating components cancel
- making the loss convex
- removing the need for a learning rate
Nesterov momentum evaluates the gradient…
- at the current point
- at a look-ahead point
- at the origin
- only every other step
Backtracking line search shrinks the step until…
- the gradient is zero
- the loss decreases by a sufficient amount (Armijo condition)
- the step is exactly 1
- the Hessian is positive definite