TExcelTAD
Home › Math for Machine Learning › Hessians and Multivariate Taylor Series

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 f:Rn→Rf: \mathbb{R}^n \to \mathbb{R} with continuous second derivatives,

H=∇2f=[∂2f∂x12⋯∂2f∂x1∂xn⋮⋱⋮∂2f∂xn∂x1⋯∂2f∂xn2]H = \nabla^2 f = \begin{bmatrix} \frac{\partial^2 f}{\partial x_1^2} & \cdots & \frac{\partial^2 f}{\partial x_1\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial^2 f}{\partial x_n\partial x_1} & \cdots & \frac{\partial^2 f}{\partial x_n^2} \end{bmatrix}

It is symmetric, because mixed partial derivatives do not depend on the order (Schwarz's theorem).

Multivariate Taylor expansion

Around x0\mathbf{x}_0, with δ=x−x0\boldsymbol{\delta} = \mathbf{x} - \mathbf{x}_0:

f(x)≈f(x0)+∇f(x0) δ+12δTH(x0) δf(\mathbf{x}) \approx f(\mathbf{x}_0) + \nabla f(\mathbf{x}_0)\,\boldsymbol{\delta} + \frac{1}{2}\boldsymbol{\delta}^TH(\mathbf{x}_0)\,\boldsymbol{\delta}

The quadratic term is the curvature. Near a point where ∇f=0\nabla f = \mathbf{0} (a critical point), it decides everything:

Eigenvalues of HH Shape Point is
all >0> 0 bowl local minimum
all <0< 0 cap local maximum
mixed signs saddle saddle point
some =0= 0 flat direction undecided at second order
python · try it yourself
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 diag⁡(6x,2)\operatorname{diag}(6x, 2): at (1,0)(1, 0) both eigenvalues are positive, at (−1,0)(-1, 0) one is negative.

Newton's method

Minimize the quadratic Taylor model instead of following the gradient a little: setting its gradient to zero gives

δ=−H−1∇fT\boldsymbol{\delta} = -H^{-1}\nabla f^T

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 λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min} (the condition number of HH) measures how badly.

python · try it yourself
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 HH 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 HH.
  • Hessian-vector products HvH\mathbf{v} 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 η>2/λ\eta > 2/\lambda.

Exercises

Exercise 1: Compute a Hessian

Find the Hessian of f(x,y)=x2y+3y2f(x, y) = x^2y + 3y^2.

Answer

[2y2x2x6]\begin{bmatrix} 2y & 2x \\ 2x & 6 \end{bmatrix}.

Solution

fx=2xyf_x = 2xy, fy=x2+6yf_y = x^2 + 6y. Then fxx=2yf_{xx} = 2y, fxy=fyx=2xf_{xy} = f_{yx} = 2x, fyy=6f_{yy} = 6.

Exercise 2: Classify a critical point

Classify the critical point of f(x,y)=x2−4xy+y2f(x, y) = x^2 - 4xy + y^2 at the origin.

Answer

A saddle point.

Solution

H=[2−4−42]H = \begin{bmatrix} 2 & -4 \\ -4 & 2 \end{bmatrix} with eigenvalues 2±42 \pm 4, that is 66 and −2-2. Mixed signs give a saddle: along (1,1)(1, 1) the function is −2t2-2t^2, along (1,−1)(1, -1) it is 6t26t^2.

Exercise 3: Newton in one dimension (code)

Minimize f(x)=x4−3x2+xf(x) = x^4 - 3x^2 + x starting at x=2x = 2 with five Newton steps x←x−f′(x)/f′′(x)x \leftarrow x - f'(x)/f''(x).

Answer

It converges to x≈1.1309x \approx 1.1309.

Solution

f′(x)=4x3−6x+1f'(x) = 4x^3 - 6x + 1, f′′(x)=12x2−6f''(x) = 12x^2 - 6. At the limit f′′>0f'' > 0, so it is a local minimum.

python · try it yourself
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 33 and −1-1 means…

  1. a local minimum
  2. a local maximum
  3. a saddle point
  4. a global minimum

The Newton step is…

  1. −η∇fT-\eta\nabla f^T
  2. −H−1∇fT-H^{-1}\nabla f^T
  3. −H∇fT-H\nabla f^T
  4. ∇fT\nabla f^T

Why is gradient descent slow on f(x)=12xTAxf(\mathbf{x}) = \frac{1}{2}\mathbf{x}^TA\mathbf{x} with A=diag⁡(1000,1)A = \operatorname{diag}(1000, 1)?

  1. The function has no minimum
  2. The condition number is large: a safe step for the steep direction is tiny for the flat one
  3. The gradient is zero
  4. AA is not symmetric