Jacobian, Hessian and convexity
When there are several inputs, the derivative is no longer one number. It becomes a table. The Jacobian tells you how a function stretches and turns the plane at one spot. The Hessian tells you how a landscape bends at one spot. It also tells you whether a place where the gradient is 0 is a bottom, a top or a saddle point. Move the points and see.
Short answer. The Jacobian is the table (matrix) of all partial derivatives of a function with several inputs and several outputs. Very close up, the function stretches and turns the plane just like this matrix does. The Hessian is the table of second derivatives. It tells you which way the landscape bends at that spot, and by how much. Where the gradient is 0, the eigenvalues of the Hessian tell a bottom (both +), a top (both −) and a saddle point (different signs) apart.
Point P (r, θ) = ( , )
Jacobian J at P
Drag point P in the top left picture, or type numbers. Top left is the input plane. Top right is where F moves its grid. The orange square is a small square with side h around P, and the shape it moves to. The bottom picture puts F(P) in the middle and zooms that shape in by 1/h. The black dashed shape is the parallelogram the Jacobian J makes from the same square. The green and blue arrows are the first and second columns of J. Move the "Square side h" slider to the right to make h smaller, and the two shapes come together.
Definitions
The text below uses functions with two inputs and two outputs, but the definitions are the same for any number. A point is written , and we assume the partial derivatives we need exist.
- Jacobian matrix
- For a function , the m×n matrix with the partial derivative in row i, column j. When m = n, its determinant det JF is called the Jacobian determinant.
- Hessian matrix
- For a function , the n×n matrix with the second partial derivative in row i, column j. If all second partial derivatives are continuous, the order of differentiation does not change the value, so the matrix is symmetric.
- critical point
- If f is differentiable at a point x₀ and its gradient there is the zero vector, x₀ is called a critical point (stationary point) of f. Some books also count points where a partial derivative does not exist.
- saddle point
- A critical point x₀ that is neither a local maximum nor a local minimum. That is, every neighborhood of x₀ has both points with and points with .
- second derivative test
- At a critical point x₀ of a function f with continuous second partial derivatives: if the eigenvalues of the Hessian Hf(x₀) are all positive, it is a local minimum; if they are all negative, a local maximum; if there are both positive and negative ones, a saddle point. In the other cases (an eigenvalue is 0 and there are not both positive and negative ones) this test cannot tell. With two variables you can use D below instead. D > 0 and fxx > 0 means a local minimum, D > 0 and fxx < 0 a local maximum, D < 0 a saddle point, and D = 0 cannot tell.
- convex function
- A function f defined on a convex set is convex if the inequality below holds for every two points x, y and every 0 ≤ t ≤ 1. If the domain is an open convex set and f is twice differentiable, f is convex exactly when the Hessian is positive semidefinite (all eigenvalues 0 or more) at every point.
1. The Jacobian: a table of all the partial derivatives
A derivative was the slope of a function with one input and one output. Partial derivatives and the gradient were the slopes of a function with several inputs and one output (a spot on a map → the height of the ground). Now the function has several outputs too. The first function in the experiment takes a distance r and an angle θ and gives back a spot (x, y) on the plane.
For every pair of one output and one input there is one partial derivative. 2 outputs × 2 inputs = 4 of them. Laid out as a table, they form the Jacobian.
| move only r | move only θ | |
|---|---|---|
| output x | ∂x/∂r | ∂x/∂θ |
| output y | ∂y/∂r | ∂y/∂θ |
- One row = one output, one column = one input. With m outputs and n inputs, it has m rows and n columns.
- Read a column top to bottom, and it says "if I move only this one input a little, which way does the output go, and how fast?" In the experiment, the green arrow is the first column and the blue arrow is the second column.
- With only one output, the Jacobian has one row, and that row is the gradient. The Jacobian of
f(x, y) = x² + y²is the single row[2x 2y]. The gradient is a special case of the Jacobian (some books write the gradient standing up, as a column).
2. Work it out by hand: the Jacobian of polar coordinates
- Write down the outputs.
x = r cos θ,y = r sin θ. - Take the partial derivative by r (keep θ fixed). Treat cos θ and sin θ as fixed numbers.
∂x/∂r = cos θ,∂y/∂r = sin θ. This is the first column. - Take the partial derivative by θ (keep r fixed).
∂x/∂θ = −r sin θ,∂y/∂θ = r cos θ. This is the second column. - Put them in a table. Top row
cos θ, −r sin θ, bottom rowsin θ, r cos θ. - Find the determinant.
cos θ × r cos θ − (−r sin θ) × sin θ = r × (cos²θ + sin²θ) = r.cos²θ + sin²θis always 1. - Put in numbers. For r = 2 and θ = 0.5 (radians), cos 0.5 ≈ 0.88 and sin 0.5 ≈ 0.48, so the top row is
0.88, −0.96and the bottom row is0.48, 1.76. The determinant is exactly 2. This is the first screen of the experiment.
Here is the same work written as formulas.
What it means: a tiny square near P moves to something very close to a parallelogram with twice the area. Far from the center (large r), the same angle covers a longer arc, so things spread out more. At r = 0 the determinant is 0 too, because every angle lands on the same single point, the center.
Try this. With Polar selected: ① At the start (P = (2, 0.5), h = 1), the determinant is 2, and the area after F ÷ h² is also 2. ② Move P to r = 1 and you get 1. At r = 3 you get 3. Changing θ does not change the determinant. ③ Move to r = 0 and the determinant is 0. The parallelogram in the bottom picture is squashed onto a line.
3. Up close, it is a matrix
Even a curved function does almost the same thing as a single matrix when you look very close up. If you move from P by a small arrow d, the output moves by about J × d.
This is f(x + h) ≈ f(x) + f′(x) × h from the derivative page, made two-dimensional. There you multiplied by the number f′(x). Here you multiply by the matrix J, the same way as in matrix multiplication.
The bottom picture of the experiment shows this. It takes a square with side h, moves it with F (orange) and with J (black dashes), zooms both in by the same amount and puts them on top of each other. When h is large, the orange shape is curved and they do not match. When h gets small, they almost match. How far apart they are shrinks in step with h.
So what you learned about the determinant works at that spot too.
- The determinant of the Jacobian = how many times larger areas get at that spot. For polar coordinates at (2, 0.5), areas get twice as large.
- Negative means flipped. The Jacobian of the fold (x, y) → (x², y) has top row
2x, 0and bottom row0, 1, so the determinant is 2x. At x = −1 it is −2, so the left half lands on the right half as a mirror image. - 0 means squashed. At x = 0 on the fold, the determinant is 0. This is the fold line, so a small square gets squashed almost onto a line.
- It changes from spot to spot. One matrix changes the whole plane in the same way, but a curved function is a different matrix at every spot. The determinant of the wave function is
1 − 0.25 × cos x × cos y, so it is 0.75 at (0, 0) and 1.2 at (2.5, 0).
Try this. ① With Polar and P = (2, 0.5), make "Square side h" go 1 → 0.1 → 0.01. The largest gap goes 0.365 → 0.035 → 0.004. Make h ten times smaller and the gap gets about ten times smaller too. ② Pick Wave and put P at (0, 0). The determinant is 0.75. The area after F ÷ h² is 0.82 at h = 2, 0.77 at h = 1 and 0.75 at h = 0.2, getting closer to the determinant. ③ Pick Fold and set the x of P to 1, 0 and −1. The determinant is 2, 0 and −2. At −2 the green arrow in the bottom picture points left.
Integrals use this area factor too. When you add up the area of a round piece of land by cutting it into small pieces in (r, θ) instead of (x, y), one small (r, θ) square is really r times as large. That is why you write r dr dθ in place of dx dy. When you change variables in an integral, you multiply by the size of the Jacobian determinant (integrals).
4. The chain rule multiplies Jacobians
For functions with one input, you multiplied the slopes of the steps (the chain rule). With several inputs and outputs, you multiply Jacobians instead. If you do G first and F second:
The later step goes on the left. You measure the outer step's Jacobian at G(p), the spot that comes out of the inner step, not at p. This is the same trap as on the derivative page.
Example: G is the polar change of coordinates, and F is F(x, y) = x² + y², which has one output. The Jacobian of F is the single row [2x 2y].
- Multiply. Row × table:
[2x cos θ + 2y sin θ, −2x r sin θ + 2y r cos θ]. - Put in x = r cos θ and y = r sin θ. First entry
2r cos²θ + 2r sin²θ = 2r, second entry−2r² cos θ sin θ + 2r² sin θ cos θ = 0. - Check. The result is
[2r 0]. Combining first givesF(G(r, θ)) = r² cos²θ + r² sin²θ = r², and taking the derivative directly gives 2r by r and 0 by θ. They match. For r = 2 it is[4 0].
Backpropagation multiplies Jacobians starting from the back. A neural network is a chain of functions, one for each layer, and the loss at the very end is a single number. So the last Jacobian is a single row. If you multiply from the back, every step is "one row × matrix = one row", which is cheap. If you multiply from the front, you have to do "big matrix × big matrix". Real programs never even build the full Jacobian matrices. They compute "one row × Jacobian" directly (a vector-Jacobian product). See backpropagation for more.
5. The Hessian: a table of second derivatives
Now back to a function with one output, the height of the ground f(x, y). The gradient (∂f/∂x, ∂f/∂y) is the slope at that spot. The slope also changes from spot to spot, so you can take partial derivatives of it by x and by y once more. Put the four results in a table and you get the Hessian.
| once more by x | once more by y | |
|---|---|---|
| ∂f/∂x | f_xx | f_xy |
| ∂f/∂y | f_yx | f_yy |
The Hessian is the Jacobian of the gradient. The gradient is itself a function with two inputs (x, y) and two outputs (the two partial derivatives).
- The diagonal, f_xx and f_yy: how fast the slope changes as you walk in the x direction (or the y direction), in other words how much the ground bends that way. + means it bends up (a bowl), − means it bends down (an upside-down bowl).
- Off the diagonal, f_xy: how much the x slope changes when you move in y. This is how much the ground is twisted.
- It is always symmetric (f_xy = f_yx). For a smooth function, taking the derivative by x first and then by y gives the same value as the other order.
The Hessian also tells you how much the ground bends in any one direction. For a direction u of length 1, the bending is uᵀ H u (the dot product of u and H u). The directions where this is largest and smallest are the eigen directions of the Hessian, and the bending in those directions is the eigenvalues. Because the Hessian is symmetric, its eigenvalues are always real numbers and its two eigen directions are at right angles.
Example: f(x, y) = x³ − 3x + y². ∂f/∂x = 3x² − 3 and ∂f/∂y = 2y. Taking derivatives once more gives f_xx = 6x, f_xy = f_yx = 0 and f_yy = 2. At P = (0.5, 1) the Hessian is [3 0 / 0 2] (top row before the /, bottom row after it), and the eigenvalues are 3 (x direction) and 2 (y direction).
The ground can bend even when the diagonal is 0. The Hessian of f = xy is [0 1 / 1 0]. It does not bend in the x or y direction, but its eigenvalues are 1 and −1, with eigen directions (1, 1) and (1, −1). It bends up along one diagonal and down along the other.
Point P = ( , )
Hessian H at P
Drag point P on the map on the right, or type numbers. The lines on the map are contour lines that join points of the same height. Darker means higher. The green line is the direction that bends up the most (λ₁), and the blue line is the direction that bends the least (λ₂). A direction with a negative eigenvalue (bending down) is drawn dashed. The green and blue curves in the 3D picture on the left are slices of the ground along those directions. The black arrow is the gradient (drawn at 1/4 length). Turn on "Show the quadratic approximation" to lay the paraboloid made from the Hessian over the ground as an orange net. An orange ring on the map shows where that paraboloid is flat (where Newton's method goes).
6. The second derivative test: bottom, top or saddle point
Where the gradient is (0, 0), the ground does not slope in any direction. This is called a stopping point (a critical point). It might be a bottom, a top, or a saddle point, like the middle of a horse saddle: uphill one way and downhill another way. The slope is 0, so the slope alone cannot tell them apart. You have to look at the bending (the Hessian).
At a stopping point P the gradient is 0, so the height nearby is f(P + d) ≈ f(P) + ½ dᵀ H d. If ½ dᵀ H d is + in every direction, every way goes up. The signs of the eigenvalues decide this. To see, picture by picture, how the table below comes out of this formula, go to How the formula is built.
| Eigenvalues of the Hessian | Shape | Examples (at the origin) |
|---|---|---|
| both + | Bottom (local minimum). Every way goes up | x² + y², x² + 4y² |
| both − | Top (local maximum). Every way goes down | −x² − y² |
| one +, one − | Saddle point. Up along λ₁, down along λ₂ | x² − y², xy |
| one is 0 | Cannot tell. You need third order or higher in that direction | x², x² + y⁴, x² − y⁴ |
A shortcut without finding the eigenvalues. The determinant of the Hessian is the product of the two eigenvalues, and the trace (f_xx + f_yy) is their sum (see eigenvalues). So:
- determinant > 0 and f_xx > 0 → bottom. A + product means both eigenvalues have the same sign, and f_xx tells you which sign.
- determinant > 0 and f_xx < 0 → top.
- determinant < 0 → saddle point. A − product means the signs are different.
- determinant = 0 → cannot tell.
7. How the formula is built: the second derivative test
The table above only gives the results. Here you build it step by step, by cutting the ground around a stopping point one direction at a time. The starting example is the stopping point (−1, 0) of "Hill and saddle x³ − 3x + y²" from the lab above.
Press "Next" to go one step at a time. Each step changes the two pictures by that much, adds one line to the formula under the pictures (the orange background marks the line just added), and explains why at the bottom. You can move the "Cutting direction θ" slider at any step. The orange bar on the map is the cutting direction, and the arrow points to where t is +. The top of the second picture is the slice along that direction (across: the distance t from P, up: how much higher than f(P)). The thick gray line is the real ground and the orange line is the quadratic approximation. The bottom is the bend k(θ) for each direction θ. Green and blue are the eigenvalues λ₁ and λ₂ and their directions. The purple dotted line is the middle of the wave.
Written only as formulas, the steps above are:
Here is the same thing in words. The numbers are for the starting example (−1, 0) and θ = 30°.
- The slope term drops out. In the quadratic approximation
f(P + d) ≈ f(P) + ∇f · d + ½ dᵀ H d(the quadratic approximation and Newton's method), a stopping point has ∇f = (0, 0), so the middle term is 0. Written out, the½ dᵀ H dthat is left is½(f_xx·dx² + 2f_xy·dx·dy + f_yy·dy²). At (−1, 0), f = 2 and the Hessian is[−6 0 / 0 2], sof ≈ 2 − 3dx² + dy². The real height is2 − 3dx² + dx³ + dy², so the only difference is dx³, and it shrinks faster than the bending terms as you get closer to P. - Cut along one direction and you get one parabola. Go a distance t along a direction of length 1,
u = (cos θ, sin θ). Thendx = t cos θanddy = t sin θ. Put these in, and the three terms collapse into one:½ · k(θ) · t². Herek(θ) = f_xx cos²θ + 2f_xy cos θ sin θ + f_yy sin²θ, which is the same asuᵀ H ufrom section 5. For θ = 30°,k = −6 × 0.75 + 2 × 0.25 = −4, and the slice is the parabola−2t², which opens down. - Turn all the way around and you get a wave whose ends are the eigenvalues. Use
cos²θ = (1 + cos 2θ) ÷ 2,sin²θ = (1 − cos 2θ) ÷ 2and2 sin θ cos θ = sin 2θ. Thenk(θ) = m + (f_xx − f_yy) ÷ 2 × cos 2θ + f_xy × sin 2θwithm = (f_xx + f_yy) ÷ 2. The part that mixes cos and sin is a wave that goes up and down by the radiusR = √(((f_xx − f_yy) ÷ 2)² + f_xy²). So k stays between m − R and m + R and reaches both ends. The two roots of the eigenvalue equationλ² − (f_xx + f_yy)λ + (f_xx f_yy − f_xy²) = 0are alsom ± R. So the most bending is λ₁ and the least bending is λ₂. At (−1, 0), m = −2 and R = 4, so λ₁ = 2 (at 90°) and λ₂ = −6 (at 0°). - The signs decide the shape. The bend in any direction lies between λ₂ and λ₁. If λ₂ > 0, every slice is a parabola that opens up: a bottom. If λ₁ < 0, every slice opens down: a top. If λ₁ > 0 > λ₂, some directions open up and some open down: a saddle point. At (−1, 0), k is 0 at 60° and 120°. Cut between them (around 90°) and the slice opens up. Cut anywhere else and it opens down. The difference between the real ground and the quadratic approximation shrinks faster than t² as you get closer to P, so very close to P the sign of the bending term wins (we do not prove this here). If an eigenvalue is 0, the bending term is 0 in that direction, so the leftover difference (third order and beyond) decides the shape. That is why you cannot tell.
- The shortcut with the determinant and the trace. The product and the sum of the two roots of a quadratic equation come straight from its coefficients:
λ₁ × λ₂ = f_xx f_yy − f_xy²(the determinant of the Hessian) andλ₁ + λ₂ = f_xx + f_yy(the trace). If the product is −, the signs differ: a saddle point. If the product is +, the signs are the same, and that sign is the sign of the sum. When the product is +,f_xx f_yy > f_xy² ≥ 0, so f_xx and f_yy also have the same sign. That is why you can look at f_xx instead of the trace, as in the list above. At (−1, 0) the determinant is−6 × 2 − 0² = −12< 0 → saddle point.
Try this. ① With the starting example, go to step 2 and turn the bar to 90°. Then k = 2 and the slice opens up. At 60°, k = 0, so the quadratic approximation is a flat line (the real ground still bends a little because of dx³). ② Pick "Diagonal saddle xy". From step 1 the real ground and the quadratic approximation are the same, because the ground is already a quadratic formula. At step 3, λ₁ = 1 (at 45°) and λ₂ = −1 (at 135°). It bends along the diagonals even though f_xx and f_yy are 0. ③ For "Upside-down bowl", the wave at step 3 is a flat line stuck at −2. ④ Look at "Flat valley x²" at step 4. The wave touches 0 at 90°, and it says "cannot tell". ⑤ Look at (1, 0) of "Hill and saddle" at step 5. The determinant is 12 and the trace is 8 → bottom.
8. Work it out by hand: the stopping points of x³ − 3x + y²
- Find the gradient.
∇f = (3x² − 3, 2y). - Set it to 0.
3x² − 3 = 0gives x = 1 or −1, and2y = 0gives y = 0. There are two stopping points, (1, 0) and (−1, 0). - Find the Hessian.
[6x 0 / 0 2]. - (1, 0): Hessian
[6 0 / 0 2]. Eigenvalues 6 and 2, both + → bottom. Height1 − 3 + 0 = −2. With the shortcut: determinant 12 > 0, f_xx = 6 > 0. - (−1, 0): Hessian
[−6 0 / 0 2]. Eigenvalues 2 and −6, different signs → saddle point. Height−1 + 3 + 0 = 2. With the shortcut: determinant −12 < 0.
Here is the same work written as formulas.
On the derivative page, x = −1 was a top of x³ − 3x. Adding y² makes the ground bend up in the y direction, so the same spot becomes a saddle point instead of a top. Looking in only one direction can miss things.
Try this. With "Hill and saddle": ① At the start, P = (0.5, 1) has gradient (−2.25, 2), so "This is not a stopping point". ② Press "Go to a stopping point". P moves to the nearest one, (1, 0), and the eigenvalues 6 and 2 make it a bottom. ③ Move P to (−1, 0). The eigenvalues 2 and −6 make it a saddle point. In the 3D picture the green slice bends up and the blue slice bends down. ④ At the origin of "Diagonal saddle xy" the eigenvalues are 1 and −1, and the two lines lie along the diagonals. ⑤ In "Flat valley x²", put P at (0, 0.7). The eigenvalues are 2 and 0, so it says "Cannot tell". On this ground, the whole line x = 0 is the bottom.
9. The quadratic approximation and Newton's method
Even away from a stopping point, the Hessian lets you approximate the ground near that spot with a paraboloid (a curved surface shaped like a bowl or a saddle). It is the flat plane you get from the slope, with the bending added.
This is the Taylor series up to second order (Taylor series). Example: at P = (0.5, 1), f = −0.375, ∇f = (−2.25, 2) and the Hessian is [3 0 / 0 2]. For d = (0.5, 0) the quadratic approximation is −0.375 + (−2.25 × 0.5) + ½ × 3 × 0.5² = −1.125, and the real height f(1, 1) is −1. For d = (0.1, 0) you get −0.585 and −0.584, almost the same. The closer you are, the better it fits.
Newton's method jumps straight to the place where this paraboloid is flat. Setting the paraboloid's gradient ∇f + H d to 0 gives d = −H⁻¹ ∇f (H⁻¹ is the inverse matrix). With one input it is x ← x − f′(x) ÷ f″(x).
- From P = (0.5, 1), one step goes to (1.25, 0). y² is exactly a parabola, so y reaches 0 in one step. Repeating for x gives 0.5 → 1.25 → 1.025 → 1.0003, so it sticks to the bottom (1, 0) within a few steps.
- If the ground is exactly a bowl given by a quadratic formula, one step reaches the bottom from anywhere.
- Be careful: Newton's method only goes to "a flat place". It does not choose bottoms. At P = (−0.5, 1) the Hessian is
[−3 0 / 0 2], so one step goes to (−1.25, 0), toward the saddle point.
Try this. Turn on "Show the quadratic approximation". ① At P = (0.5, 1), the orange net hugs the ground near P and pulls away further out. The orange ring on the map (one Newton step) is at (1.25, 0). ② Move P to (−0.5, 1). The net becomes saddle-shaped, and the ring is at (−1.25, 0). ③ On "Bowl", the ring is at (0, 0) wherever you put P. ④ On "Flat valley", the determinant of the Hessian is 0, so no ring appears.
10. Convex functions: bowl-shaped everywhere
Pick two points on the ground and lay a straight stick between them. If, for any two points you pick, the stick never cuts into the ground (it stays above it or touches it), the function is convex. For a function you can take the derivative of twice, you can say the same thing with the Hessian: it is convex when the eigenvalues of the Hessian are 0 or more at every spot. It bends up or stays flat everywhere, and never bends down.
x² + y²,x² + 4y²: eigenvalues + everywhere → convex.x²: eigenvalues 2 and 0 → convex (0 is fine). But the bottom is not a single point. It is the whole line x = 0.x² − y²,xy: one eigenvalue is − everywhere → not convex.x³ − 3x + y²: f_xx = 6x is − when x < 0 → not convex.
Why convex is nice. Any place where the gradient is 0 is the lowest place of all. You do not have to worry "is this bottom only low nearby?", and as long as the step size is right, gradient descent reaches the lowest height from any starting point. If the eigenvalues are above 0 everywhere, there is only one lowest point. The losses of least squares and logistic regression are convex, so where gradient descent stops is the lowest place.
Press "Roll the balls" and balls roll down from twelve starting points (hollow circles) all at once, using gradient descent. The starting points come from a fixed seed (the starting value for the random numbers), so they are the same every time. The strip under the slider shows the range of step sizes η. The green part is the range where balls do not fly away from the bottom (up to 2 ÷ λ), and the orange part is beyond it.
Try this. ① On "Bowl" with η = 0.3, all 12 balls stop at (0, 0) (the slowest takes 9 steps). The ground is convex, so every start ends at the same bottom. ② "Four wells" is not convex. Roll with η = 0.05 and the balls split up: 4 at (1, 1), 3 at (1, −1), 3 at (−1, 1) and 2 at (−1, −1). Which well a ball starts near decides where it ends.
11. The step size limit: 2 ÷ λ
On bowl-shaped ground, the Hessian decides how large the step size can be. For a bowl f = ½ dᵀ H d with its bottom at the origin, the gradient is H d. Split one gradient descent step d ← d − η H d along the eigen directions, and the distance along each direction gets multiplied by (1 − η λ) in one step.
- It only shrinks if
|1 − ηλ| < 1, in other words0 < η < 2 ÷ λ. - With several directions, every direction has to shrink, so the largest eigenvalue sets the limit:
η < 2 ÷ λ_max. - At
η = 1 ÷ λ, that direction reaches the bottom in one step. Between1 ÷ λand2 ÷ λ, the ball jumps past the bottom to the other side each time (a zigzag) but still gets closer. At2 ÷ λit just bounces back and forth, and beyond it the ball gets further away every step.
The bowl x² + y² has the Hessian [2 0 / 0 2], so λ = 2 and the limit is 1. With η = 0.5, the ball reaches the bottom in one step.
Long bowls are slow. x² + 10y² has eigenvalues 20 (the y direction) and 2 (the x direction). The steep y direction sets the limit at 2 ÷ 20 = 0.1. But with such a small step, the gentle x direction only shrinks to about 1 − 0.1 × 2 = 0.8 times per step. The largest eigenvalue ÷ the smallest eigenvalue is called the condition number. Here it is 10. The larger the condition number, the more the steep direction zigzags and the more the gentle direction crawls, so it takes a long time to reach the bottom.
Try this. ① On "Bowl", set η to 0.5 and every ball stops in 1 step. At 0.9 they zigzag and take 37 steps. At 1, none of the 12 balls stops within 1000 steps (they bounce back and forth). From 1.01 up, they all fly off. ② On "Very long bowl", the starting η = 0.09 zigzags and takes 46 steps. Wherever you move the slider below 0.1, it never gets faster than 44 steps. At η = 0.105, they all fly off. ③ The bottoms of "Four wells" have the Hessian [8 0 / 0 8], so the limit is 0.25. Roll with η = 0.25 and the balls bounce back and forth around the bottoms without stopping.
Methods like momentum and Adam, which reduce this zigzag, can be compared directly on the gradient descent page.
12. The loss landscape of a neural network
The loss of a neural network is a landscape with millions of weights or more. Here is what is known about it, stated carefully.
- It is not convex. For example, if you swap two neurons in a hidden layer, together with their incoming and outgoing weights, you get the same function. So there are bottoms of the same height in several places, and the loss can go up on the straight line between them. That cannot happen on convex ground.
- There are thought to be very many saddle points. To be a bottom, all of millions of eigenvalues must be +. There are theoretical arguments and experiments showing that in high dimensions, many places where the gradient is 0 are saddle points rather than bottoms, and that learning slows down on the flat areas around them (Dauphin et al., 2014).
- Stopping exactly on a saddle point is rare. There is a proof that, under some conditions, gradient descent from a randomly chosen start does not end up at a saddle point (Lee et al., 2016). It can still spend a long time near one.
The Hessian is too big. With n weights, the Hessian has n × n entries. With a million weights that is a trillion (10¹²) entries, and even at 4 bytes per number that is 4 TB. Solving with it the way Newton's method does takes even more work. So large models do not use the Hessian directly.
- There is a way to compute only "the Hessian times one arrow" instead of the whole Hessian. It gives the exact value in a few times the work of computing the gradient (Pearlmutter, 1994).
- Methods like Adam remember the average of past squared slopes for each weight and divide the step by its square root (Kingma and Ba). Because it sets the step separately for each direction, it is sometimes explained as a very rough stand-in for the diagonal of the Hessian (the bending seen one weight at a time). But squared slopes are not the bending itself, so they are not the same thing. See gradient descent for more.
- Quasi-Newton methods such as BFGS and L-BFGS estimate the inverse of the Hessian from how the gradient changed over the last few steps (Goodfellow et al., section 8.6).
13. Properties to know
| Property | In plain words |
|---|---|
| Jacobian: rows = outputs, columns = inputs | One column is "the arrow the output follows when you move only that input a little". With one output it has one row, and that row is the gradient |
F(P + d) ≈ F(P) + J × d | Very close up, even a curved function moves things like a single matrix |
| Determinant of the Jacobian = area factor at that spot | Negative means flipped, 0 means squashed. For polar coordinates it is r |
| Chain rule = product of Jacobians | The later step goes on the left. Backpropagation multiplies from the back |
| Hessian = Jacobian of the gradient, always symmetric | Real eigenvalues, eigen directions at right angles. Eigenvalue = how much it bends in that direction |
| Gradient 0 + signs of the eigenvalues → shape | Both + bottom, both − top, different signs saddle point, a 0 means cannot tell |
| Convex = eigenvalues ≥ 0 everywhere | Where it stops is the lowest place |
| Step size < 2 ÷ λ_max | Beyond it, balls fly off. A large condition number means slow zigzags |
Where is it used?
- Backpropagation and automatic differentiation. Multiply the Jacobian of each layer, starting from the back. Programs never build the full Jacobian. They only compute "one row × Jacobian", which keeps it cheap (backpropagation).
- Finding out why training goes wrong. If the loss jumps around or keeps growing, the step size (learning rate) may be past the limit 2 ÷ λ_max set by the steepest bending. In long, narrow valleys (large condition number), training slows down.
- Changing probability distributions. Generative models such as normalizing flows move the points of a simple distribution with a function that can be undone, and fix up the probability density by the Jacobian determinant, because the area (volume) changed by that much. So the functions are designed so that the determinant is quick to compute (Dinh et al., Real NVP).
- Changing variables in an integral. When you add up areas or volumes in other coordinates such as (r, θ), multiply by the size of the Jacobian determinant (integrals).
- Optimization. Newton's method and quasi-Newton methods use the Hessian (or an estimate of it) to choose the direction and size of each step. The second derivative test checks whether an answer you found is a bottom.
- Convex problems. When the loss is convex, as in least squares and logistic regression, where it stops is the best answer, so you can trust the result.
Frequently asked questions
What is the difference between the Jacobian and the gradient?
The gradient collects the partial derivatives of a function with one output. The Jacobian is the table you get when there are several outputs, with one row for each output. If there is only one output, the Jacobian has one row, and that row is the gradient.
Why is the Hessian always symmetric?
For a smooth function (one whose second derivatives do not jump), taking the derivative by x first and then by y gives the same result as doing it in the other order. Because of this, the eigenvalues of the Hessian are always real numbers, and its two eigen directions are at right angles.
When does the test fail even though the gradient is 0?
When one of the eigenvalues of the Hessian is 0. The bending (second order) alone cannot tell you the shape in that direction. For example, x² + y⁴ and x² − y⁴ have the same Hessian at the origin, [2 0 / 0 0], but the first one is a bottom and the second one is a saddle point.
Why is convexity nice?
For a convex function, any place where the gradient is 0 is the lowest place of all. You never get stuck in a bottom that is only low nearby, and as long as the step size is right, gradient descent reaches the lowest height from any starting point.
Why don't people use the Hessian directly to train neural networks?
With n weights, the Hessian has n × n entries, so a million weights give a trillion entries. That is hard to store and hard to compute. So people use only the gradient, compute only the Hessian times one arrow, or use methods like Adam that remember how large the slopes are for each weight and adjust the step for each weight.
Words
| What this page says | What textbooks call it |
|---|---|
| table of all the partial derivatives | Jacobian matrix. J, ∂(x, y)/∂(r, θ) |
| determinant of the Jacobian, area factor at that spot | Jacobian determinant. Some books call only this "the Jacobian" |
| up close, it is a matrix | linear approximation, linearization |
| one row × Jacobian | vector-Jacobian product (VJP) |
| table of second derivatives | Hessian matrix. H, ∇²f |
| derivative by x, then by y | mixed partial derivative. f_xy, ∂²f/∂x∂y |
| the order of the derivatives does not matter | Schwarz's theorem, Clairaut's theorem |
| stopping point (where the gradient is 0) | critical point, stationary point |
| bottom / top | local minimum / local maximum |
| uphill one way, downhill another | saddle point |
| telling them apart by the signs of the eigenvalues | second derivative test |
| all eigenvalues + / all 0 or more | positive definite / positive semidefinite |
| bowl-shaped everywhere | convex function |
| lowest place of all | global minimum |
| paraboloid estimate at that spot | quadratic approximation, second-order Taylor expansion |
| jumping to where the paraboloid is flat | Newton's method |
| methods that use an estimate of the Hessian | quasi-Newton methods. BFGS, L-BFGS |
| Hessian × one arrow | Hessian-vector product |
| largest eigenvalue ÷ smallest eigenvalue | condition number |
| step size η | learning rate, step size |
Sources
- OpenStax, Calculus Volume 3. 4.3 Partial Derivatives (mixed partial derivatives and their order), 4.5 The Chain Rule, 4.7 Maxima/Minima Problems (the second derivative test), 5.7 Change of Variables in Multiple Integrals (the Jacobian as an area factor).
- Goodfellow, Bengio, Courville, Deep Learning. Chapter 4, Numerical Computation (Jacobian and Hessian, the second derivative test, condition number and step size, Newton's method, convex functions), Chapter 8, Optimization for Training Deep Models (weight space symmetry, saddle points, quasi-Newton methods).
- Dauphin, Pascanu, Gulcehre, Cho, Ganguli, Bengio (2014). Identifying and attacking the saddle point problem in high-dimensional non-convex optimization.
- Lee, Simchowitz, Jordan, Recht (2016). Gradient Descent Converges to Minimizers.
- Pearlmutter (1994). Fast Exact Multiplication by the Hessian. Neural Computation 6(1).
- Kingma, Ba (2014). Adam: A Method for Stochastic Optimization.
- Dinh, Sohl-Dickstein, Bengio (2016). Density estimation using Real NVP.
- Baydin, Pearlmutter, Radul, Siskind (2018). Automatic Differentiation in Machine Learning: a Survey. JMLR 18(153). Reverse mode and vector-Jacobian products.