TExcelTAD
Home › Math for Machine Learning › Step Size and Momentum

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 γ<2/λmax⁡\gamma < 2/\lambda_{\max} on quadratics.
  • Backtracking line search: start with a large γ\gamma and halve it until the loss decreases enough. The Armijo condition asks for f(x−γg)≤f(x)−cγ∥g∥2f(\mathbf{x} - \gamma\mathbf{g}) \le f(\mathbf{x}) - c\gamma\lVert \mathbf{g} \rVert^2 with a small cc such as 10−410^{-4}.
  • Schedules: in deep learning, γ\gamma 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:

vi+1=α vi−γ ∇f(xi)Txi+1=xi+vi+1\begin{aligned} \mathbf{v}_{i+1} &= \alpha\,\mathbf{v}_i - \gamma\,\nabla f(\mathbf{x}_i)^T \\ \mathbf{x}_{i+1} &= \mathbf{x}_i + \mathbf{v}_{i+1} \end{aligned}

with α∈[0,1)\alpha \in [0, 1), typically 0.90.9. The velocity is an exponentially weighted sum of past gradients. Components that keep the same sign accumulate (up to a factor 1/(1−α)1/(1 - \alpha), so 10 times for α=0.9\alpha = 0.9); components that flip sign every step cancel. Physically, it is a heavy ball rolling downhill with friction.

python · try it yourself
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 κ\kappa steps per digit to about κ\sqrt{\kappa}, a huge gain when κ\kappa is in the thousands.

Nesterov's variant

Nesterov accelerated gradient evaluates the gradient at the look-ahead point xi+αvi\mathbf{x}_i + \alpha\mathbf{v}_i instead of xi\mathbf{x}_i. 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

python · try it yourself
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.minimize methods 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 α=0.9\alpha = 0.9 and a constant gradient gg, what is the steady-state velocity?

Answer

−10γg-10\gamma g.

Solution

At steady state v=0.9v−γgv = 0.9v - \gamma g, so 0.1v=−γg0.1v = -\gamma g and v=−10γgv = -10\gamma g: momentum multiplies the effective step by 1/(1−α)=101/(1 - \alpha) = 10 along consistent directions.

Exercise 2: Armijo check

At x=2x = 2 for f(x)=x2f(x) = x^2, the gradient is g=4g = 4. Do the steps t=0.5t = 0.5 and t=1t = 1 satisfy f(x−tg)≤f(x)−10−4 t g2f(x - tg) \le f(x) - 10^{-4}\,t\,g^2?

Answer

t=0.5t = 0.5: yes. t=1t = 1: no.

Solution
t=0.5:f(2−2)=0≤4−10−4⋅0.5⋅16=3.9992✓t=1:f(2−4)=4≤4−10−4⋅16=3.9984fails\begin{aligned} t = 0.5: &\quad f(2 - 2) = 0 \le 4 - 10^{-4} \cdot 0.5 \cdot 16 = 3.9992 && \checkmark \\ t = 1: &\quad f(2 - 4) = 4 \le 4 - 10^{-4} \cdot 16 = 3.9984 && \text{fails} \end{aligned}

Starting from t=1t = 1, backtracking halves once and lands exactly on the minimum.

Exercise 3: Momentum on a 1D quadratic (code)

Minimize f(x)=5x2f(x) = 5x^2 from x=1x = 1 with γ=0.05\gamma = 0.05, comparing α=0\alpha = 0 and α=0.5\alpha = 0.5 after 20 steps.

Answer

Plain GD reaches about 10−610^{-6}; with α=0.5\alpha = 0.5 the iterate is still far larger. Momentum does not help on this easy problem.

Solution
python · try it yourself
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 γ=0.05\gamma = 0.05 and f′′=10f'' = 10, plain GD multiplies xx by 1−0.5=0.51 - 0.5 = 0.5 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…

  1. computing exact second derivatives
  2. accumulating consistent gradient directions while oscillating components cancel
  3. making the loss convex
  4. removing the need for a learning rate

Nesterov momentum evaluates the gradient…

  1. at the current point
  2. at a look-ahead point x+αv\mathbf{x} + \alpha\mathbf{v}
  3. at the origin
  4. only every other step

Backtracking line search shrinks the step until…

  1. the gradient is zero
  2. the loss decreases by a sufficient amount (Armijo condition)
  3. the step is exactly 1
  4. the Hessian is positive definite