Skip to content
BIASED

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 L(θ)L(\theta) 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 f(x1,…,xn)f(x_1, \dots, x_n) and the ii-th basis vector eie_i,

∂f∂xi(x)=lim⁡t→0f(x+tei)−f(x)t.\frac{\partial f}{\partial x_i}(x) = \lim_{t \to 0} \frac{f(x + t e_i) - f(x)}{t}.

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:

q(x)=x12+x1x2+2x22=x⊤Ax,A=(10.50.52).q(x) = x_1^2 + x_1 x_2 + 2x_2^2 = x^\top A x, \qquad A = \begin{pmatrix} 1 & 0.5 \\ 0.5 & 2 \end{pmatrix}.

It is a quadratic form with a positive definite AA, as in the topic Symmetric matrices and quadratic forms. Holding x2x_2 fixed and then x1x_1 fixed gives

∂q∂x1=2x1+x2,∂q∂x2=x1+4x2.\frac{\partial q}{\partial x_1} = 2x_1 + x_2, \qquad \frac{\partial q}{\partial x_2} = x_1 + 4x_2.

In a real loss the roles are worth keeping straight. For a linear model with one feature and the squared error on one example, L(w,b)=(wx+b−y)2L(w, b) = (wx + b - y)^2, the data xx and yy are constants. The parameters ww and bb are the variables.

The gradient: best linear approximation

Partial derivatives only look along the axes. A step hh can point anywhere, and we want one formula for all of them. A function f:Rn→Rf: \mathbb R^n \to \mathbb R is differentiable at xx if

f(x+h)=f(x)+∇f(x)⊤h+o(∥h∥),f(x + h) = f(x) + \nabla f(x)^\top h + o(\lVert h \rVert),

where the gradient collects the partial derivatives into a vector:

∇f(x)=(∂f∂x1(x),…,∂f∂xn(x))⊤.\nabla f(x) = \Big(\frac{\partial f}{\partial x_1}(x), \dots, \frac{\partial f}{\partial x_n}(x)\Big)^\top.

This is the one-variable definition again. The number f′(x)f'(x) becomes a vector, and the product f′(x) hf'(x)\,h becomes the inner product ⟨∇f(x),h⟩\langle \nabla f(x), h \rangle.

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 xx and are continuous at xx. 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 x=(1,1)x = (1, 1) we get q=4q = 4 and ∇q=(3,5)\nabla q = (3, 5). A step h=(0.1,−0.2)h = (0.1, -0.2) gives the linear estimate

q(1.1, 0.8)≈4+3⋅0.1+5⋅(−0.2)=3.3.q(1.1,\, 0.8) \approx 4 + 3 \cdot 0.1 + 5 \cdot (-0.2) = 3.3.

The exact value is 3.373.37. The error 0.070.07 is quadratic in hh: halve the step and it drops to a quarter.

In a network, ∇L(θ)\nabla L(\theta) has exactly as many entries as θ\theta. The gradient of a model with a billion parameters is a billion numbers.

The direction of steepest ascent

How fast does ff change if we move from xx in a unit direction uu? This is the directional derivative. The linear approximation answers at once:

Duf(x)=ddtf(x+tu)∣t=0=∇f(x)⊤u.D_u f(x) = \frac{d}{dt} f(x + t u)\Big|_{t = 0} = \nabla f(x)^\top u.

By Cauchy–Schwarz (see the topic Inner products, norms and orthogonality), ∣∇f(x)⊤u∣≤∥∇f(x)∥\lvert \nabla f(x)^\top u \rvert \le \lVert \nabla f(x) \rVert for every unit uu. Equality holds only when uu is parallel to the gradient. So:

  • ff grows fastest along ∇f(x)/∥∇f(x)∥\nabla f(x) / \lVert \nabla f(x) \rVert, at the rate ∥∇f(x)∥\lVert \nabla f(x) \rVert;
  • ff falls fastest along −∇f(x)-\nabla f(x), and this is the whole idea of gradient descent, θ←θ−η ∇L(θ)\theta \leftarrow \theta - \eta\,\nabla L(\theta);
  • along a level set ff does not change, so Duf=0D_u f = 0 there and the gradient is perpendicular to the level set.

A unit length matters. For u=(1,1)u = (1, 1) the formula gives 2\sqrt 2 times the rate along the diagonal, not the rate itself.

Back to qq at (1,1)(1, 1), where ∇q=(3,5)\nabla q = (3, 5) and ∥∇q∥=34≈5.83\lVert \nabla q \rVert = \sqrt{34} \approx 5.83. Along u=(0.6,0.8)u = (0.6, 0.8) we get Duq=1.8+4.0=5.8D_u q = 1.8 + 4.0 = 5.8, almost the maximum, because uu almost points along the gradient. Along u=(5,−3)/34u = (5, -3)/\sqrt{34} we get 00: this direction is tangent to the ellipse q(x)=4q(x) = 4.

The Jacobian

A layer outputs a vector, not a number. For F:Rn→RmF: \mathbb R^n \to \mathbb R^m each output FiF_i has its own gradient. Stacking these gradients as rows gives the m×nm \times n Jacobian:

JF(x)=(∇F1(x)⊤⋮∇Fm(x)⊤),(JF(x))ij=∂Fi∂xj(x).J_F(x) = \begin{pmatrix} \nabla F_1(x)^\top \\ \vdots \\ \nabla F_m(x)^\top \end{pmatrix}, \qquad \big(J_F(x)\big)_{ij} = \frac{\partial F_i}{\partial x_j}(x).

It plays the same role as the gradient: F(x+h)≈F(x)+JF(x) hF(x + h) \approx F(x) + J_F(x)\,h. The best local approximation of FF 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 z=Wx+bz = Wx + b is its own linear approximation, so J=WJ = W;
  • an elementwise activation a=φ(z)a = \varphi(z) makes aia_i depend on ziz_i alone, so J=diag⁡(φ′(z1),…,φ′(zm))J = \operatorname{diag}\big(\varphi'(z_1), \dots, \varphi'(z_m)\big). For ReLU the diagonal holds zeros and ones.

A geometric example: F(x)=(x12−x22, 2x1x2)F(x) = (x_1^2 - x_2^2,\ 2x_1 x_2). At x=(1,0.5)x = (1, 0.5)

JF=(2x1−2x22x22x1)=(2−112),det⁡JF=5.J_F = \begin{pmatrix} 2x_1 & -2x_2 \\ 2x_2 & 2x_1 \end{pmatrix} = \begin{pmatrix} 2 & -1 \\ 1 & 2 \end{pmatrix}, \qquad \det J_F = 5.

Near this point FF rotates by about 26.6∘26.6^\circ and stretches by 5\sqrt 5, so small areas grow 55 times. This is the determinant as volume scaling from the topic Determinants and eigenvalues. It returns with ∣det⁡J∣\lvert \det J \rvert in the topic Multiple integrals and change of variables.

The chain rule: a product of Jacobians

A network is a composition of maps. Take f:Rn→Rkf: \mathbb R^n \to \mathbb R^k and g:Rk→Rmg: \mathbb R^k \to \mathbb R^m. The linear approximation of g∘fg \circ f 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

Jg∘f(x)=Jg(f(x)) Jf(x).J_{g \circ f}(x) = J_g\big(f(x)\big)\, J_f(x).

The shapes fit: (m×k)(k×n)=m×n(m \times k)(k \times n) = m \times n. Each Jacobian is taken at its own input: JgJ_g at the point f(x)f(x), not at xx.

Training ends with a scalar loss ℓ:Rm→R\ell: \mathbb R^m \to \mathbb R. Its Jacobian is a single row, ∇ℓ⊤\nabla \ell^\top. Transposing the chain rule gives the gradient of the whole composition:

∇(ℓ∘F)(x)=JF(x)⊤ ∇ℓ(F(x)).\nabla (\ell \circ F)(x) = J_F(x)^\top\, \nabla \ell\big(F(x)\big).

Take ℓ(v)=v1+v22\ell(v) = v_1 + v_2^2 and the map FF from the previous block at x=(1,0.5)x = (1, 0.5). There F(x)=(0.75,1)F(x) = (0.75, 1) and ∇ℓ=(1,2v2)=(1,2)\nabla \ell = (1, 2v_2) = (1, 2), so

∇(ℓ∘F)(x)=(21−12)(12)=(43).\nabla (\ell \circ F)(x) = \begin{pmatrix} 2 & 1 \\ -1 & 2 \end{pmatrix} \begin{pmatrix} 1 \\ 2 \end{pmatrix} = \begin{pmatrix} 4 \\ 3 \end{pmatrix}.

Direct differentiation of ℓ(F(x))=x12−x22+4x12x22\ell(F(x)) = x_1^2 - x_2^2 + 4x_1^2 x_2^2 gives the same (4,3)(4, 3).

Backpropagation

Here is a two-layer network with one output. Biases are dropped to keep the numbers small:

z=W1x,a=ReLU⁡(z),y^=w2⊤a,L=12(y^−y)2.z = W_1 x, \qquad a = \operatorname{ReLU}(z), \qquad \hat y = w_2^\top a, \qquad L = \tfrac12 (\hat y - y)^2.

As a function of the input xx, the loss is a chain of four maps. The chain rule multiplies their Jacobians, and transposing gives

∇xL=W1⊤ diag⁡(ReLU⁡′(z)) w2 (y^−y).\nabla_x L = W_1^\top\, \operatorname{diag}\big(\operatorname{ReLU}'(z)\big)\, w_2\, (\hat y - y).

The order of evaluation is the point. Start at the loss with the number δ=y^−y\delta = \hat y - y 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

x=(12),W1=(1−10.51),w2=(21),y=1.5.x = \begin{pmatrix} 1 \\ 2 \end{pmatrix}, \qquad W_1 = \begin{pmatrix} 1 & -1 \\ 0.5 & 1 \end{pmatrix}, \qquad w_2 = \begin{pmatrix} 2 \\ 1 \end{pmatrix}, \qquad y = 1.5.

Forward: z=(−1,2.5)z = (-1, 2.5), a=(0,2.5)a = (0, 2.5), y^=2.5\hat y = 2.5, L=0.5L = 0.5. The pass stores zz for later.

Backward: δ=1\delta = 1, then ∇aL=δ w2=(2,1)\nabla_a L = \delta\, w_2 = (2, 1), then ∇zL=(0⋅2, 1⋅1)=(0,1)\nabla_z L = (0 \cdot 2,\ 1 \cdot 1) = (0, 1), and finally ∇xL=W1⊤(0,1)=(0.5,1)\nabla_x L = W_1^\top (0, 1) = (0.5, 1).

The first hidden unit has z1<0z_1 < 0. 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 n×nn \times n Hessian:

(∇2f(x))ij=∂2f∂xi ∂xj(x).\big(\nabla^2 f(x)\big)_{ij} = \frac{\partial^2 f}{\partial x_i\, \partial x_j}(x).

It is the Jacobian of the gradient map x↦∇f(x)x \mapsto \nabla f(x). 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 ∇q=(2x1+x2, x1+4x2)\nabla q = (2x_1 + x_2,\ x_1 + 4x_2), so ∇2q=(2114)=2A\nabla^2 q = \begin{pmatrix} 2 & 1 \\ 1 & 4 \end{pmatrix} = 2A, the same at every point.

The Hessian gives the second-order Taylor expansion:

f(x+h)=f(x)+∇f(x)⊤h+12 h⊤∇2f(x) h+o(∥h∥2).f(x + h) = f(x) + \nabla f(x)^\top h + \tfrac12\, h^\top \nabla^2 f(x)\, h + o(\lVert h \rVert^2).

Where it comes from: restrict ff to the line through xx along hh, ϕ(t)=f(x+th)\phi(t) = f(x + th). The chain rule gives ϕ′(t)=∇f(x+th)⊤h\phi'(t) = \nabla f(x + th)^\top h, and since the Jacobian of ∇f\nabla f is the Hessian, once more ϕ′′(t)=h⊤∇2f(x+th) h\phi''(t) = h^\top \nabla^2 f(x + th)\, h. The one-variable expansion ϕ(1)≈ϕ(0)+ϕ′(0)+12ϕ′′(0)\phi(1) \approx \phi(0) + \phi'(0) + \tfrac12 \phi''(0) from the topic Derivatives and Taylor series is the formula above; continuity of the second partials makes the remainder o(∥h∥2)o(\lVert h \rVert^2).

The new term is a quadratic form, the curvature along hh. For the quadratic qq the expansion is exact: in the gradient block 12h⊤(2A) h=0.07\tfrac12 h^\top (2A)\, h = 0.07, which is precisely the error of the linear estimate.

For a function that is not quadratic the Hessian changes from point to point. Take

c(x)=x13−3x1+x22,∇c=(3x12−3, 2x2),∇2c=(6x1002).c(x) = x_1^3 - 3x_1 + x_2^2, \qquad \nabla c = (3x_1^2 - 3,\ 2x_2), \qquad \nabla^2 c = \begin{pmatrix} 6x_1 & 0 \\ 0 & 2 \end{pmatrix}.

At (1.2,0.5)(1.2, 0.5) with the step h=(0.1,−0.2)h = (0.1, -0.2) the second-order estimate is −1.614-1.614, and the exact value c(1.3,0.3)=−1.613c(1.3, 0.3) = -1.613.

Critical points and saddles

At a critical point ∇f(x∗)=0\nabla f(x^\ast) = 0 the linear term vanishes, and the curvature decides:

f(x∗+h)≈f(x∗)+12 h⊤∇2f(x∗) h.f(x^\ast + h) \approx f(x^\ast) + \tfrac12\, h^\top \nabla^2 f(x^\ast)\, h.

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, x12+x24x_1^2 + x_2^4 and x12−x24x_1^2 - x_2^4 both have the Hessian diag⁡(2,0)\operatorname{diag}(2, 0) at the origin, yet the first has a minimum there and the second does not.

For cc the gradient vanishes at (±1,0)(\pm 1, 0). At (1,0)(1, 0) the Hessian is diag⁡(6,2)\operatorname{diag}(6, 2), both eigenvalues are positive, and c=−2c = -2 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, L(a,b)=(ab−1)2L(a, b) = (ab - 1)^2. Every point of the hyperbola ab=1ab = 1 is a minimum, and at (1,1)(1, 1) the Hessian is (2222)\begin{pmatrix} 2 & 2 \\ 2 & 2 \end{pmatrix}, with eigenvalues 44 and 00. The zero one points along (1,−1)(1, -1), the direction of the hyperbola, where rescaling a→caa \to ca, b→b/cb \to b/c 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: n=106n = 10^6 parameters give 101210^{12} entries. Practice uses Hessian-vector products ∇2L v\nabla^2 L\, v, 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 ∇xL\nabla_x L, but training needs the gradient with respect to W1W_1, a whole matrix. Writing ∂L/∂(W1)ij\partial L / \partial (W_1)_{ij} 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.