AI Wiki中文/EN

The Hessian

海森矩阵

Read directional curvature, derive a quadratic local model, and test Hessian–vector products without building a large matrix.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

The slope can change as you walk

A gradient tells you how a scalar score changes at your current position. Take a step and that gradient can change too. On a narrow valley floor, the slope changes sharply across the valley and gently along it. The Hessian records this directional difference in curvature.

Consider f(a,b)=a2+2ab+3b2+a3f(a,b)=a^2+2ab+3b^2+a^3. At the origin the gradient is zero. Nearby, the quadratic terms form a bowl, while the cubic term gradually changes the curvature along a. The same function can therefore look bowl-shaped in one neighborhood and have negative curvature elsewhere. MML, Section 5.7

Differentiate the gradient

For a scalar function with continuous second partial derivatives in a neighborhood, the Hessian is the Jacobian of its column gradient:

Hf(x)ij=∂∂xj(∂f∂xi),∇f(x+h)=∇f(x)+Hf(x)h+o(∥h∥).H_f(x)_{ij}=\frac{\partial}{\partial x_j}\left(\frac{\partial f}{\partial x_i}\right),\qquad \nabla f(x+h)=\nabla f(x)+H_f(x)h+o(\|h\|).

It has shape n×nn\times n. Under the stated continuity assumption, mixed partial derivatives agree and H is symmetric. The notation o(∥h∥)o(\|h\|) means that the remainder divided by the displacement norm tends to zero. A vector-valued function has one scalar-output Hessian per component; collecting them requires an additional output index.

For the example,

∇f(a,b)=[2a+2b+3a22a+6b],Hf(a,b)=[2+6a226].\nabla f(a,b)=\begin{bmatrix}2a+2b+3a^2\\2a+6b\end{bmatrix},\qquad H_f(a,b)=\begin{bmatrix}2+6a&2\\2&6\end{bmatrix}.

Restrict the function to the line x+tvx+tv for a fixed direction v. Applying the chain rule twice gives the line’s second derivative v⊤Hf(x+tv)vv^\top H_f(x+tv)v. If v has unit length, this measures curvature per unit displacement. Scaling v by c multiplies the quantity by c2c^2; a reported directional curvature should therefore specify the direction’s normalization.

Integrating the changing gradient along a short segment yields the local quadratic approximation:

f(x+h)=f(x)+∇f(x)⊤h+12h⊤Hf(x)h+o(∥h∥2).f(x+h)=f(x)+\nabla f(x)^\top h+\frac12h^\top H_f(x)h+o(\|h\|^2).

For this particular cubic polynomial, the remainder is exactly ha3h_a^3. For a general twice continuously differentiable function, the displayed little-o term is valid; an O(∥h∥3)O(\|h\|^3) bound needs additional control, such as a locally Lipschitz Hessian. Convex Optimization, Appendix A.4.3–A.4.4

Check curvature by moving the gradient

The complete program uses analytic derivatives of this two-variable polynomial. Its functions assume two finite, moderate-size real inputs so intermediate arithmetic stays finite. It checks a Hessian–vector product against a directional difference of gradients, then verifies the exact cubic remainder.

import math

def model(x):
    a,b = x
    return a*a+2*a*b+3*b*b+a**3

def gradient(x):
    a,b = x
    return [2*a+2*b+3*a*a, 2*a+6*b]

def hessian(x):
    a,b = x
    return [[2+6*a,2.],[2.,6.]]

def hessian_vector(x,v):
    a,b = x
    p,q = v
    return [(2+6*a)*p+2*q, 2*p+6*q]

def quadratic_model(x, displacement):
    g = gradient(x)
    hv = hessian_vector(x,displacement)
    return model(x)+sum(a*b for a,b in zip(g,displacement))+.5*sum(a*b for a,b in zip(displacement,hv))

x = [.5,-.2]; v = [.3,-.4]; step = 1e-5
plus = [a+step*b for a,b in zip(x,v)]
minus = [a-step*b for a,b in zip(x,v)]
numeric = [(a-b)/(2*step) for a,b in zip(gradient(plus),gradient(minus))]
assert max(abs(a-b) for a,b in zip(numeric,hessian_vector(x,v))) < 1e-9
for h in ([.1,-.2],[-.2,.3],[0.,.4]):
    actual = model([a+b for a,b in zip(x,h)])
    assert abs(actual-quadratic_model(x,h)-h[0]**3) < 1e-12
assert hessian([0.,0.]) == [[2.,2.],[2.,6.]]
assert hessian_vector([-.5,0.],[1.,0.])[0] == -1.

The fixed two-dimensional example has constant cost. In a dense n-variable model, storing H needs O(n2)O(n^2) numbers and multiplying an already stored H by v costs O(n2)O(n^2). Constructing H has its own derivative cost. A direct product can avoid that storage; here the expression is written by hand. It is not an implementation of general second-order automatic differentiation.

Curvature alone does not locate a minimum

At an interior stationary point of a twice continuously differentiable scalar function, positive-definite H is sufficient for a strict local minimum; negative-definite H gives a strict local maximum; an indefinite H gives a saddle. These follow because the quadratic term dominates the smaller remainder in the relevant directions.

A positive-semidefinite Hessian is necessary at such a local minimum but is not sufficient. At zero, x4x^4, −x4-x^4, and x3x^3 all have zero gradient and zero Hessian, yet give a minimum, a maximum, and neither. Higher-order behavior matters. Also, f(x)=x+x2f(x)=x+x^2 has positive second derivative at zero but is not stationary there, so zero is not a minimum. Constraints require separate feasible-direction reasoning.

For a Newton step, solve Hp=−∇fHp=-\nabla f rather than explicitly forming an inverse. If H is indefinite, that solution need not be a descent direction; if H is singular, it may not be uniquely defined. The local quadratic model is not automatically reliable after a large step. Line searches, trust regions, or regularized curvature address different parts of these difficulties.

Try to fool the curvature test

  1. What is the Hessian at the origin in the example? Answer: [2226]\begin{bmatrix}2&2\\2&6\end{bmatrix}. Its leading minors are two and eight, so it is positive definite; with zero gradient, the origin is a strict local minimum.
  2. What is the curvature along (1,0)(1,0) at (−0.5,0)(-0.5,0)? Answer: −1. The function is not convex on all of R2\mathbb R^2.
  3. Why does a zero Hessian not prove a flat neighborhood? Answer: x4x^4 has zero Hessian at zero but positive values at every nonzero point.
  4. For h=(0.1,−0.2)h=(0.1,-0.2), how far is the quadratic model from the true example? Answer: the true value exceeds the model by 0.13=0.0010.1^3=0.001.

A matrix too large to store

Neural-network curvature connects optimization, sensitivity, and uncertainty approximations. But a Hessian with a billion parameter coordinates would contain a quintillion entries if stored densely. This motivates products, low-rank approximations, and structured substitutes. Their usefulness depends on which curvature information a task needs. Read quadratic forms for directional geometry and Taylor expansions for the error term that makes local curvature reasoning precise.

Make a prediction, then check

At a stationary point the Hessian is zero. What can be concluded directly?

Choose one answer

Learning objectives

  • Derive the Hessian and directional curvature from a column gradient.
  • Distinguish sufficient, necessary, and inconclusive stationary-point conditions.

Prerequisites & connections

Explore related knowledge →

References

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. Convex Optimization
    Stephen Boyd; Lieven Vandenberghe · 2004

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: specified-source comparison, derivation checks, independent numerical checks, and paragraph-level bilingual comparison; no independent human peer review.

  • Checked gradient differences, Hessian products, and exact cubic remainders for 80 inputs/directions; verified interior-stationary-point and C² assumptions.
  • Used x⁴, −x⁴, and x³ to distinguish inconclusive second-order information; did not claim semidefiniteness is sufficient, and checked the 10¹⁸ entry count.
  • Compared bilingual assumptions, claims, numbers, exercise answers and citations; structural checks additionally compare formulas, code, main anchors and source IDs.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.