TExcelTAD
Home › Math for Deep Learning & LLMs › Gradient-Based Optimization and Curvature

Curvature, the Hessian and Saddle Points

The gradient says which way is downhill; the Hessian says how the slope itself changes. Curvature explains why learning rates have a maximum, why some directions train slowly, and why high-dimensional loss surfaces are full of saddle points rather than bad local minima. The calculus is in Hessians and Second-Order Taylor; here we look at what it means for training networks.

The second-order picture

Near a point w\mathbf{w}, the loss is approximately

L(w+δ)≈L(w)+gTδ+12δTHδ\mathcal{L}(\mathbf{w} + \boldsymbol{\delta}) \approx \mathcal{L}(\mathbf{w}) + \mathbf{g}^T\boldsymbol{\delta} + \tfrac{1}{2}\boldsymbol{\delta}^T H\boldsymbol{\delta}

For a gradient step δ=−ηg\boldsymbol{\delta} = -\eta\mathbf{g} the predicted change is −η∥g∥2+12η2gTHg-\eta\lVert\mathbf{g}\rVert^2 + \frac{1}{2}\eta^2\mathbf{g}^TH\mathbf{g}. If the curvature along g\mathbf{g} is large, the second term wins and the step increases the loss. Along an eigenvector with eigenvalue λ\lambda, gradient descent multiplies the error by 1−ηλ1 - \eta\lambda, which diverges once η>2/λ\eta > 2/\lambda:

η<2λmax⁡(H)\eta < \frac{2}{\lambda_{\max}(H)}

Critical points

Where g=0\mathbf{g} = \mathbf{0}, the eigenvalues of HH classify the point: all positive is a local minimum, all negative a local maximum, mixed signs a saddle point. In a network with millions of parameters, having every one of millions of eigenvalues positive is a strong condition; most critical points with high loss turn out to be saddles. Gradient noise and momentum help escape them, because any small push along a negative-curvature direction grows.

python · try it yourself
import numpy as np

f = lambda w: w[0]**2 - w[1]**2                          # a saddle at the origin
grad = lambda w: np.array([2 * w[0], -2 * w[1]])
H = np.array([[2.0, 0.0], [0.0, -2.0]])
print("eigenvalues of H:", np.linalg.eigvalsh(H), "-> saddle")
for start in ([1.0, 0.0], [1.0, 1e-6]):
    w = np.array(start)
    for _ in range(80):
        w = w - 0.1 * grad(w)
    print(f"start {start}: after 80 steps w = {np.round(w, 4)}, f = {f(w):.4f}")
eigenvalues of H: [-2.  2.] -> saddle
start [1.0, 0.0]: after 80 steps w = [0. 0.], f = 0.0000
start [1.0, 1e-06]: after 80 steps w = [0.     2.1602], f = -4.6666

Started exactly on the ridge, gradient descent converges to the saddle; a perturbation of 10−610^{-6} is enough to escape.

The edge of stability

The largest Hessian eigenvalue λmax⁡\lambda_{\max} (the sharpness) can be estimated without forming HH by power iteration on Hessian-vector products, which autodiff computes for the cost of about two gradients. Observed in practice: during full-batch training the sharpness rises until it hovers around 2/η2/\eta, the "edge of stability".

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
A = rng.normal(size=(50, 50)); H = A @ A.T / 50           # a positive semidefinite "Hessian"
hvp = lambda v: H @ v                                      # in practice computed by autodiff
v = rng.normal(size=50)
for _ in range(1000):
    v = hvp(v); v /= np.linalg.norm(v)
lam = v @ hvp(v)
print(f"power iteration: {lam:.4f}, exact: {np.linalg.eigvalsh(H).max():.4f}")
print(f"largest stable learning rate: {2 / lam:.4f}")
power iteration: 3.5069, exact: 3.5069
largest stable learning rate: 0.5703

Flat and sharp minima

Minima with small Hessian eigenvalues are flat: the loss changes little if the weights are perturbed. Flat minima tend to generalize better, an observation behind the success of small-batch SGD and methods such as sharpness-aware minimization (SAM), which minimizes the worst loss in a small ball around the weights.

In software

  • torch.autograd.functional.hvp and jax.jvp(jax.grad(f), …) give Hessian-vector products.
  • Learning-rate warmup (Schedules) keeps early steps below 2/λmax⁡2/\lambda_{\max} while the sharpness is high.
  • Newton's method uses H−1gH^{-1}\mathbf{g}; it is too expensive for networks, but Adam's per-coordinate scaling is a cheap diagonal stand-in.

Exercises

Exercise 1: Maximum learning rate

The Hessian at a minimum has eigenvalues 0.5, 4 and 20. What is the largest stable step size?

Answer

η<0.1\eta < 0.1.

Solution

Stability requires ∣1−ηλ∣<1\lvert 1 - \eta\lambda \rvert < 1 for every eigenvalue, so η<2/20=0.1\eta < 2/20 = 0.1.

Exercise 2: Classify the point

f(x,y)=x2+3xy+y2f(x, y) = x^2 + 3xy + y^2 has a critical point at the origin. What kind?

Answer

A saddle point.

Solution

H=(2332)H = \begin{pmatrix} 2 & 3 \\ 3 & 2 \end{pmatrix} has eigenvalues 55 and −1-1: mixed signs.

Exercise 3: Divergence (code)

Run gradient descent on f(w)=5w2f(w) = 5w^2 from w=1w = 1 with η=0.19\eta = 0.19 and η=0.21\eta = 0.21 for 20 steps.

Answer

0.190.19 converges (factor −0.9-0.9 per step); 0.210.21 diverges (factor −1.1-1.1).

Solution

f′′=10f'' = 10, so the threshold is 2/10=0.22/10 = 0.2; each step multiplies ww by 1−10η1 - 10\eta.

python · try it yourself
for lr in (0.19, 0.21):
    w = 1.0
    for _ in range(20):
        w -= lr * 10 * w
    print(lr, round(w, 4))
0.19 0.1216
0.21 6.7275

Test yourself

Gradient descent on a quadratic is stable only if the step size is below…

  1. 1/λmin⁡1/\lambda_{\min}
  2. 2/λmax⁡2/\lambda_{\max}
  3. λmax⁡\lambda_{\max}
  4. 1

A critical point whose Hessian has both positive and negative eigenvalues is…

  1. a minimum
  2. a maximum
  3. a saddle point
  4. impossible

How is the largest Hessian eigenvalue of a big network usually estimated?

  1. Form the full Hessian and call eig
  2. Power iteration with Hessian-vector products
  3. It cannot be estimated
  4. From the learning rate alone