Week 08 · Hessians and gradient descent

Read curvature from a Hessian and predict whether a gradient descent step will converge or diverge.

Watch on YouTube

Subject contents · Week 7 · Week 9

Read curvature from a Hessian and predict whether a gradient descent step will converge or diverge.

A correct slope can still lead to a bad step

Imagine trying to descend a narrow valley. One direction drops quickly and the other changes slowly. A single step size must work for both directions. If the step is too large for the steep direction, you can jump across the valley and land higher than where you started.

Here the loss is a simple quadratic, so we can predict exactly when that happens. By the end, you should be able to read curvature from the Hessian, calculate a safe range of learning rates for this example, and diagnose the difference between slow progress and divergence.

The idea

The Hessian collects second partial derivatives of a scalar function. The gradient tells you the slope at the current point. The Hessian tells you how that slope changes as you move. For a quadratic loss L(w) = ½ wᵀHw with symmetric H, the gradient is Hw. One gradient descent update is wₙ₊₁ = (I − ηH)wₙ, where η is the learning rate.

In an eigenvector direction of H with eigenvalue λ, the update multiplies that coordinate by 1 − ηλ. If its absolute value is below one, the coordinate shrinks. A negative multiplier changes the sign on each step. A multiplier with magnitude above one causes growth. For a positive definite quadratic, all directions shrink when 0 < η < 2/λₘₐₓ.

A large ratio between the largest and smallest eigenvalues means the loss has both a steep and a shallow direction. A single step size small enough for the steep direction may still crawl along the shallow one.

Derive the multiplier along one direction

Let w contain the model parameters and let H be the symmetric matrix of second derivatives of the loss. For the quadratic L(w) = ½wᵀHw, differentiating gives ∇L = Hw. Gradient descent subtracts η times that gradient, where η is the learning rate.

w(next) = w − ηHw = (I − ηH)w.

I is the identity matrix, which leaves any vector unchanged. If one component of w points along an eigenvector v with H v = λv, the update changes its coefficient by a factor of 1 − ηλ. A repeated update multiplies by that factor again on every step. It converges along this direction exactly when the factor lies strictly between −1 and 1.

For a positive λ, solving −1 < 1 − ηλ < 1 gives 0 < η < 2/λ. To shrink every direction of a positive definite quadratic, satisfy the strict bound for the largest eigenvalue. At the boundary the steep component can alternate with unchanged magnitude. This is an exact result for this quadratic with a fixed step size.

Work it out

Use L(x, y) = ½(x² + 20y²) and start at (1, 1). The gradient is (x, 20y), so the two multipliers are 1 − η and 1 − 20η. The steep direction requires η < 0.1.

At η = 0.05, the y multiplier is zero. After one step the point is (0.95, 0). The x coordinate still shrinks by only 5% each step. At η = 0.11, y is multiplied by −1.2. It oscillates and grows, even though x shrinks. A correct gradient can give a worse loss when the step is too large.

Side-by-side contours show gradient descent converging at step size 0.05 and zigzagging away from the minimum at 0.11.

The narrow contours mark the steep y direction. A step above the stability limit grows in that direction.

Run the Python lab

import numpy as np

H = np.diag([1., 20.])

def loss(w):
    return 0.5 * w @ H @ w

def trajectory(rate, steps):
    path = [np.ones(2)]
    for _ in range(steps):
        w = path[-1]
        path.append(w - rate * (H @ w))
    return np.array(path)

stable = trajectory(0.05, 5)
unstable = trajectory(0.11, 5)
print(f"initial loss: {loss(stable[0]):.3f}")
print("stable first step:", np.round(stable[1], 2).tolist())
print(f"large step loss: {loss(unstable[1]):.3f}")
assert loss(stable[-1]) < loss(stable[0])
assert loss(unstable[-1]) > loss(unstable[0])

Expected output

initial loss: 10.500
stable first step: [0.95, 0.0]
large step loss: 14.796

Compare the first steps before inspecting the plot

The starting loss is ½(1 + 20) = 10.5. With η = 0.05, the next point (0.95, 0) has loss 0.45125. With η = 0.11, the next point (0.89, −1.2) has loss ½(0.89² + 20 × 1.2²) ≈ 14.796. The steep component dominates the change despite the horizontal component moving closer to zero.

The lab’s stable run also shows why a safe rate can be slow. After the first step, the horizontal coordinate shrinks by only 5% per update. Rescaling features can change the relative curvatures seen by an optimizer. In Week 12, you will compare this effect on the actual classifier loss, where the Hessian can change as the weights move.

Try it yourself

  1. Use η = 0.1 exactly. Predict the y coordinate after any number of steps.
  2. Rescale the second coordinate by defining z = √20 y. What does the Hessian look like in (x, z) coordinates?

Check your work. At the boundary, y alternates between 1 and −1, so it never converges. In the new coordinates the loss is ½(x² + z²), and the Hessian is the identity.

Further reading

Deep Learning · Applied Math and Machine Learning Basics

Subject contents · Week 7 · Week 9