Hessians and Multivariate Taylor Series
The gradient says which way is downhill. The Hessian, the matrix of second derivatives, says how the slope itself changes: whether you are in a bowl, on a ridge or at a saddle, and how big a step is safe. It powers Newton's method, explains why some loss surfaces are hard to optimize, and classifies critical points.
The Hessian
For with continuous second derivatives,
It is symmetric, because mixed partial derivatives do not depend on the order (Schwarz's theorem).
Multivariate Taylor expansion
Around , with :
The quadratic term is the curvature. Near a point where (a critical point), it decides everything:
| Eigenvalues of | Shape | Point is |
|---|---|---|
| all | bowl | local minimum |
| all | cap | local maximum |
| mixed signs | saddle | saddle point |
| some | flat direction | undecided at second order |
import numpy as np
def f(v): x, y = v; return x**3 - 3 * x + y**2
def hessian(f, v, h=1e-4):
n = len(v); H = np.zeros((n, n))
for i in range(n):
for j in range(n):
ei, ej = np.eye(n)[i] * h, np.eye(n)[j] * h
H[i, j] = (f(v + ei + ej) - f(v + ei - ej) - f(v - ei + ej) + f(v - ei - ej)) / (4 * h * h)
return H
for p in (np.array([1.0, 0.0]), np.array([-1.0, 0.0])): # both have gradient (3x^2-3, 2y) = 0
H = hessian(f, p)
lam = np.linalg.eigvalsh(H)
kind = "minimum" if np.all(lam > 0) else ("maximum" if np.all(lam < 0) else "saddle")
print(p, "Hessian eigenvalues", np.round(lam, 4), "->", kind)[1. 0.] Hessian eigenvalues [2. 6.] -> minimum
[-1. 0.] Hessian eigenvalues [-6. 2.] -> saddle
The exact Hessian is : at both eigenvalues are positive, at one is negative.
Newton's method
Minimize the quadratic Taylor model instead of following the gradient a little: setting its gradient to zero gives
On a quadratic function Newton's method lands on the minimum in one step, whatever the scaling. Gradient descent, by contrast, zig-zags when the Hessian's eigenvalues differ a lot; the ratio (the condition number of ) measures how badly.
import numpy as np
A = np.array([[10.0, 0.0], [0.0, 1.0]]) # f(x) = 1/2 x^T A x, condition number 10
grad = lambda x: A @ x
x_gd = np.array([1.0, 1.0]); x_nt = np.array([1.0, 1.0])
for _ in range(20):
x_gd = x_gd - 0.1 * grad(x_gd) # step size 1/lambda_max
x_nt = x_nt - np.linalg.solve(A, grad(x_nt)) # one Newton step
print("gradient descent, 20 steps:", np.round(x_gd, 6))
print("Newton, 1 step: ", x_nt)gradient descent, 20 steps: [0. 0.121577]
Newton, 1 step: [0. 0.]
Gradient descent has wiped out the steep direction but still crawls along the flat one. Newton's method rescales each direction by its curvature. For networks with millions of parameters, forming and inverting is impossible, which is why deep learning uses gradient methods with cheap curvature hints such as Adam's per-parameter scaling.
In software
scipy.optimize.minimize(method="Newton-CG")and L-BFGS use Hessian information without forming .- Hessian-vector products cost about as much as two gradients with autodiff (
jax.hvp). - The largest Hessian eigenvalue (sharpness) of a network's loss bounds stable learning rates: gradient descent diverges along a direction if .
Exercises
Exercise 1: Compute a Hessian
Find the Hessian of .
Answer
.
Solution
, . Then , , .
Exercise 2: Classify a critical point
Classify the critical point of at the origin.
Answer
A saddle point.
Solution
with eigenvalues , that is and . Mixed signs give a saddle: along the function is , along it is .
Exercise 3: Newton in one dimension (code)
Minimize starting at with five Newton steps .
Answer
It converges to .
Solution
, . At the limit , so it is a local minimum.
x = 2.0
for k in range(5):
x = x - (4 * x**3 - 6 * x + 1) / (12 * x**2 - 6)
print(k + 1, round(x, 6))1 1.5
2 1.238095
3 1.144277
4 1.131153
5 1.130901
Test yourself
At a critical point, a Hessian with eigenvalues and means…
- a local minimum
- a local maximum
- a saddle point
- a global minimum
The Newton step is…
Why is gradient descent slow on with ?
- The function has no minimum
- The condition number is large: a safe step for the steep direction is tiny for the flat one
- The gradient is zero
- is not symmetric