The Hessian
海森矩阵
Read directional curvature, derive a quadratic local model, and test Hessian–vector products without building a large matrix.
hessianThe 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 . 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:
It has shape . Under the stated continuity assumption, mixed partial derivatives agree and H is symmetric. The notation 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,
Restrict the function to the line for a fixed direction v. Applying the chain rule twice gives the line’s second derivative . If v has unit length, this measures curvature per unit displacement. Scaling v by c multiplies the quantity by ; a reported directional curvature should therefore specify the direction’s normalization.
Integrating the changing gradient along a short segment yields the local quadratic approximation:
For this particular cubic polynomial, the remainder is exactly . For a general twice continuously differentiable function, the displayed little-o term is valid; an 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 numbers and multiplying an already stored H by v costs . 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, , , and all have zero gradient and zero Hessian, yet give a minimum, a maximum, and neither. Higher-order behavior matters. Also, 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 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
- What is the Hessian at the origin in the example? Answer: . Its leading minors are two and eight, so it is positive definite; with zero gradient, the origin is a strict local minimum.
- What is the curvature along at ? Answer: −1. The function is not convex on all of .
- Why does a zero Hessian not prove a flat neighborhood? Answer: has zero Hessian at zero but positive values at every nonzero point.
- For , how far is the quadratic model from the true example? Answer: the true value exceeds the model by .
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?
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
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - 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:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §5.7, printed pp.164–165 / PDF pp.170–171
- Convex Optimization · Appendix A.4.3–A.4.4, printed pp.643–645 / PDF pp.657–659
Related topics