Jacobian and Hessian
Two matrices organise the derivatives of multivariable functions. The Jacobian holds the first derivatives of a function with several outputs; it is the best linear approximation of that function and the object that backpropagation passes through every layer. The Hessian holds the second derivatives of a function with one output; it describes curvature and powers Newton's method.
The Jacobian matrix
A function has output components , each depending on inputs. Its Jacobian is the matrix of all first partials, one row per output:
It plays the role of the derivative: near a point ,
so locally behaves like the matrix . Row of is the gradient of ; for a single output () the Jacobian is just the gradient written as a row.
The Jacobian determinant
For a map from to (or to ), is square and is the local area (volume) scale factor: a tiny square of area near is mapped to a tiny parallelogram of area . The polar-coordinate map has , which is why in double integrals.
import sympy as sp
r, th = sp.symbols('r theta', positive=True)
F = sp.Matrix([r * sp.cos(th), r * sp.sin(th)])
J = F.jacobian([r, th])
print("J =", J.tolist())
print("det J =", sp.simplify(J.det()))
# spherical coordinates (rho, phi, theta)
rho, phi = sp.symbols('rho phi', positive=True)
S = sp.Matrix([rho*sp.sin(phi)*sp.cos(th), rho*sp.sin(phi)*sp.sin(th), rho*sp.cos(phi)])
print("spherical det J =", sp.simplify(S.jacobian([rho, phi, th]).det()))J = [[cos(theta), -r*sin(theta)], [sin(theta), r*cos(theta)]]
det J = r
spherical det J = rho**2*sin(phi)
The Jacobian in backpropagation
A neural-network layer , with applied element by element, is a function from to . Writing , the chain rule in matrix form gives
Composing layers multiplies their Jacobians, the multivariable chain rule written with matrices. Backpropagation never builds these matrices in full. It starts from the gradient of the scalar loss, a row vector , and computes the vector-Jacobian product , layer by layer from the output back to the input. That costs about as much as one forward pass, whatever the size of the network. See Backpropagation Is the Chain Rule.
import numpy as np
rng = np.random.default_rng(7)
W = rng.normal(size=(3, 4))
b = rng.normal(size=3)
x = rng.normal(size=4)
layer = lambda x: np.tanh(W @ x + b)
u = W @ x + b
J = np.diag(1 - np.tanh(u) ** 2) @ W # tanh'(u) = 1 - tanh(u)^2
h = 1e-6
J_num = np.column_stack([(layer(x + h * e) - layer(x - h * e)) / (2 * h) for e in np.eye(4)])
print("Jacobian shape:", J.shape)
print("max |J - J_num|:", f"{np.max(np.abs(J - J_num)):.1e}")
g = np.array([1.0, -2.0, 0.5]) # dL/dy from the layer above
print("vector-Jacobian product g·J:", np.round(g @ J, 4))Jacobian shape: (3, 4)
max |J - J_num|: 4.3e-11
vector-Jacobian product g·J: [-0.1791 -0.1515 0.1522 -0.0792]
The Hessian matrix
For a scalar function , the Hessian is the matrix of second partials, . By Clairaut's theorem it is symmetric. It is the Jacobian of the gradient, and it gives the second-order (quadratic) approximation
At a critical point () the quadratic term decides everything, and the eigenvalues of say how the surface curves along its principal directions:
| Eigenvalues of | Critical point |
|---|---|
| all positive ( positive definite) | local minimum |
| all negative | local maximum |
| mixed signs | saddle point |
| some zero | inconclusive |
In two variables, is the discriminant of the second-derivative test. A large ratio between the biggest and smallest eigenvalue (the condition number) means a long narrow valley, the shape that makes gradient descent zig-zag.
Newton's method in several variables
Newton's method for minimisation jumps to the minimum of the local quadratic model:
On an exact quadratic this lands on the minimum in one step, whatever the condition number. Near a minimum of a general smooth function it converges quadratically (the number of correct digits roughly doubles each step). The price is computing and solving with an matrix, too much for a network with millions of weights. Quasi-Newton methods (BFGS, L-BFGS) build an approximate Hessian from gradients instead, and Adam's per-weight scaling is a cheap diagonal stand-in.
import numpy as np
f = lambda p: (1 - p[0])**2 + 10 * (p[1] - p[0]**2)**2 # a curved valley, min at (1, 1)
grad = lambda p: np.array([-2*(1 - p[0]) - 40*p[0]*(p[1] - p[0]**2), 20*(p[1] - p[0]**2)])
hess = lambda p: np.array([[2 - 40*p[1] + 120*p[0]**2, -40*p[0]],
[-40*p[0], 20.0]])
p = np.array([-1.0, 2.0])
print("eigenvalues of H at the minimum:", np.round(np.linalg.eigvalsh(hess(np.array([1.0, 1.0]))), 3))
for k in range(6):
p = p - np.linalg.solve(hess(p), grad(p))
print(f"Newton step {k + 1}: p = {np.round(p, 6)}, f = {f(p):.2e}")eigenvalues of H at the minimum: [ 0.394 101.606]
Newton step 1: p = [-1.105263 1.210526], f = 4.43e+00
Newton step 2: p = [ 0.618093 -2.587917], f = 8.84e+01
Newton step 3: p = [0.624416 0.389855], f = 1.41e-01
Newton step 4: p = [0.9997 0.858562], f = 1.98e-01
Newton step 5: p = [0.999779 0.999557], f = 4.90e-08
Newton step 6: p = [1. 1.], f = 2.41e-14
Both eigenvalues at are positive, so it is a minimum, and their ratio of about 250 shows how narrow the valley is. Far from the minimum pure Newton can wander (step 2 even makes worse, which is why practical solvers add a line search or trust region), but once it is close the error collapses: is about 0.2 after step 4, after step 5 and after step 6.
Exercises
Exercise 1: A Jacobian by hand
Find the Jacobian of and evaluate it at .
Answer
at .
Solution
Exercise 2: Area scaling
The map is applied to the unit square. What is the area of the image?
Answer
Solution
everywhere, , and the area scale factor is . The negative sign means the map flips orientation.
Exercise 3: Classify with the Hessian
. Show that the origin is a critical point and classify it using the Hessian's eigenvalues.
Answer
, eigenvalues , and 6, all positive: a minimum.
Solution
at the origin. The Hessian is constant. The z-block gives eigenvalue 6; the block has trace 6 and determinant 4, so , .
import numpy as np
H = np.array([[2.0, 2.0, 0.0], [2.0, 4.0, 0.0], [0.0, 0.0, 6.0]])
print(np.round(np.linalg.eigvalsh(H), 4))[0.7639 5.2361 6. ]
Exercise 4: Newton on a quadratic (code)
For , take a single Newton step from . Where do you land, and is it the minimum?
Answer
, which is the exact minimum (the gradient there is zero).
Solution
, . Setting gives and , so , . Newton's step reaches it immediately because is exactly quadratic.
import numpy as np
grad = lambda p: np.array([6*p[0] + 2*p[1] - 4, 2*p[0] + 2*p[1]])
H = np.array([[6.0, 2.0], [2.0, 2.0]])
p = np.array([10.0, -7.0])
p = p - np.linalg.solve(H, grad(p))
print(np.round(p, 10), "gradient ≈ 0:", bool(np.max(np.abs(grad(p))) < 1e-12))[ 1. -1.] gradient ≈ 0: True
Test yourself
What is the shape of the Jacobian of ?
The Hessian at a critical point has eigenvalues 4 and −1. The point is…
- a minimum
- a maximum
- a saddle point
- impossible to classify
Why is in polar coordinates important?
- It makes always positive
- It is the area scale factor, so
- It shows polar coordinates are linear
- It is the gradient of