Gradients, Jacobians and Hessians
Derivatives in many variables: partial derivatives, the gradient as the direction of steepest ascent, the Jacobian of a layer, the chain rule as a product of Jacobians (backpropagation), and the Hessian as curvature.
Partial derivatives
The topic Derivatives and Taylor series gave us the derivative of a function of one variable: the best linear approximation near a point. A model's loss depends on all of its parameters at once, and a network has millions of them. We need derivatives in many variables.
The simplest idea is to freeze every variable but one and differentiate along that one. This is the partial derivative. For and the -th basis vector ,
The other coordinates act as constants, so every rule from one-variable calculus applies unchanged.
The running example of this topic is a small loss surface, a bowl with two parameters:
It is a quadratic form with a positive definite , as in the topic Symmetric matrices and quadratic forms. Holding fixed and then fixed gives
In a real loss the roles are worth keeping straight. For a linear model with one feature and the squared error on one example, , the data and are constants. The parameters and are the variables.
The gradient: best linear approximation
Partial derivatives only look along the axes. A step can point anywhere, and we want one formula for all of them. A function is differentiable at if
where the gradient collects the partial derivatives into a vector:
This is the one-variable definition again. The number becomes a vector, and the product becomes the inner product .
One caution. Partial derivatives can exist at a point where no linear approximation does: there are functions with both partials equal to zero at the origin that still jump there. Differentiability is the formula above, not just the partials. A convenient sufficient condition: all partial derivatives exist near and are continuous at . Every polynomial meets it, and so do the losses in this course, except at isolated kinks like that of ReLU at zero.
For the running example at we get and . A step gives the linear estimate
The exact value is . The error is quadratic in : halve the step and it drops to a quarter.
In a network, has exactly as many entries as . The gradient of a model with a billion parameters is a billion numbers.
The direction of steepest ascent
How fast does change if we move from in a unit direction ? This is the directional derivative. The linear approximation answers at once:
By Cauchy–Schwarz (see the topic Inner products, norms and orthogonality), for every unit . Equality holds only when is parallel to the gradient. So:
- grows fastest along , at the rate ;
- falls fastest along , and this is the whole idea of gradient descent, ;
- along a level set does not change, so there and the gradient is perpendicular to the level set.
A unit length matters. For the formula gives times the rate along the diagonal, not the rate itself.
Back to at , where and . Along we get , almost the maximum, because almost points along the gradient. Along we get : this direction is tangent to the ellipse .
The Jacobian
A layer outputs a vector, not a number. For each output has its own gradient. Stacking these gradients as rows gives the Jacobian:
It plays the same role as the gradient: . The best local approximation of is a linear map, and a linear map is a matrix (see the topic Vectors, spaces and linear maps).
Every network is built from two kinds of Jacobians:
- a linear layer is its own linear approximation, so ;
- an elementwise activation makes depend on alone, so . For ReLU the diagonal holds zeros and ones.
A geometric example: . At
Near this point rotates by about and stretches by , so small areas grow times. This is the determinant as volume scaling from the topic Determinants and eigenvalues. It returns with in the topic Multiple integrals and change of variables.
The chain rule: a product of Jacobians
A network is a composition of maps. Take and . The linear approximation of is the composition of the two linear approximations. A composition of linear maps is a matrix product (see the topic Matrices and linear systems), so the chain rule reads
The shapes fit: . Each Jacobian is taken at its own input: at the point , not at .
Training ends with a scalar loss . Its Jacobian is a single row, . Transposing the chain rule gives the gradient of the whole composition:
Take and the map from the previous block at . There and , so
Direct differentiation of gives the same .
Backpropagation
Here is a two-layer network with one output. Biases are dropped to keep the numbers small:
As a function of the input , the loss is a chain of four maps. The chain rule multiplies their Jacobians, and transposing gives
The order of evaluation is the point. Start at the loss with the number and move toward the input. Every step multiplies a vector by one layer's Jacobian. Nobody ever multiplies two Jacobians together. This is backpropagation, or reverse-mode differentiation. One backward pass costs a few forward passes, however many inputs there are. Starting from the input side would mean multiplying whole matrices.
A worked example with
Forward: , , , . The pass stores for later.
Backward: , then , then , and finally .
The first hidden unit has . ReLU switched it off, and no gradient flows through it. The gradient with respect to the input is used in practice too: it shows which pixels matter for a prediction and is the starting point of adversarial examples.
The Hessian and curvature
The gradient gives the slope. It does not say whether the surface bends up or down. That takes second derivatives, collected in the Hessian:
It is the Jacobian of the gradient map . When the second partials are continuous, the order of differentiation does not matter, so the Hessian is symmetric. Everything from the topic Symmetric matrices and quadratic forms now applies: real eigenvalues and orthogonal eigenvectors.
For the running example , so , the same at every point.
The Hessian gives the second-order Taylor expansion:
Where it comes from: restrict to the line through along , . The chain rule gives , and since the Jacobian of is the Hessian, once more . The one-variable expansion from the topic Derivatives and Taylor series is the formula above; continuity of the second partials makes the remainder .
The new term is a quadratic form, the curvature along . For the quadratic the expansion is exact: in the gradient block , which is precisely the error of the linear estimate.
For a function that is not quadratic the Hessian changes from point to point. Take
At with the step the second-order estimate is , and the exact value .
Critical points and saddles
At a critical point the linear term vanishes, and the curvature decides:
The sign of this quadratic form is read from the eigenvalues of the Hessian:
- all eigenvalues positive: a strict local minimum;
- all negative: a strict local maximum;
- both signs: a saddle point, up in some directions and down in others;
- a zero eigenvalue and no opposite signs: the test is silent. For example, and both have the Hessian at the origin, yet the first has a minimum there and the second does not.
For the gradient vanishes at . At the Hessian is , both eigenvalues are positive, and is a strict local minimum.
In a large network the Hessian has millions of eigenvalues, and mixed signs are the rule. Dauphin et al. (2014) argued that most high-loss critical points of such losses are saddles, not bad minima.
Zero eigenvalues are common too. The smallest example is a product of two weights fitted to one target, . Every point of the hyperbola is a minimum, and at the Hessian is , with eigenvalues and . The zero one points along , the direction of the hyperbola, where rescaling , leaves the loss unchanged. ReLU networks have the same symmetry between the weights into a unit and out of it, so their minima come in flat families, and the second-order test is silent at every one of them.
The Hessian is also too big to store: parameters give entries. Practice uses Hessian-vector products , which cost about as much as a gradient. The eigenvalues still matter: they bound the step size of gradient descent in the topic Unconstrained optimization, and a Hessian that is positive semidefinite everywhere means a convex loss, as the topic Convex sets and functions shows.
One gap is left. In the backpropagation block we found , but training needs the gradient with respect to , a whole matrix. Writing entry by entry works, and it gets painful fast. The topic Matrix calculus does such gradients in a few lines.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Gradient vs finite differencesoptional
Match analytic ∇f to central differences on R^3; tol 1e-5.
~20 min · easier on a computer · The matching course topic has been read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.