A layer of a neural network turns many numbers into many numbers, so its slopes are a whole matrix, the Jacobian: it says how every output moves when you nudge every input. Training sends the gradient backward through each layer by multiplying with this matrix, and models that reshape probability distributions track how it stretches volume.
A normalizing flow chains invertible maps, and the log of the Jacobian determinant of each map tells how it stretches probability density. (P8.6: Autoregressive image models and flows, coming soon)
Gradients that shrink or blow up in a deep network come from a product of many layer Jacobians. (P4.16: Vanishing and exploding gradients, coming soon)
Write the Jacobian of a function f:Rn→Rm as the m×n matrix of partial derivatives ∂xj∂fi, compute it for polar coordinates, a dense layer and softmax, and say what its rows and columns mean
Use f(x+Δ)≈f(x)+JΔ, check that its error shrinks like ∥Δ∥2, and explain with a picture why a tiny square becomes a parallelogram whose sides are the columns of J
Recognise the special cases (one output: the gradient as a row; one input: a velocity; a linear map: J=W; an elementwise function: a diagonal matrix) and apply the chain rule Jf∘g=JfJg
Explain ∣detJ∣ as local area scaling, and why the backward pass of training multiplies by J⊤ (a vector-Jacobian product) instead of building J
A neural-network layer takes a vector in and gives a vector out. P1.C4 measured how one number responds to one nudge, and P1.C5 collected those slopes for a function with one output into the gradient. A layer has several outputs, and each one depends on several inputs. With 3 inputs and 2 outputs there are 2×3=6 slopes. Where should they go? This lesson puts them in a matrix, the Jacobian, and shows that the matrix does a job you know from P1.B5: close to a point, a curved function acts like a matrix.
Functions with several outputs
A function f:Rn→Rm takes n numbers and returns m numbers. Write the outputs as a column, f(x)=(f1(x),…,fm(x)).. Each fi is an ordinary function of n inputs, so the partial derivatives of P1.C4 apply to it. Four examples:
Polar to Cartesian(n=m=2):f(r,θ)=(rcosθ,rsinθ) turns a radius and an angle into the coordinates of a point.
A dense layer(n inputs, m outputs): f(x)=σ(Wx+b), where W is an m×n matrix (P1.B5) and the sigmoid σ acts on each entry separately.
Softmax(n=m=k): turns k scores into k probabilities.
A loss(m=1): one output. Everything in P1.C5 was this case.
The Jacobian: a matrix of partial derivatives
The Jacobian of f at a point x is the matrix of all m×n partial derivatives, with output i in row i and input j in column j:
Entry Jij=∂xj∂fi is the partial derivative of output i with respect to input j.
It is written Jf(x) when the function or the point must be named, and it is named after the mathematician Carl Jacobi. Like the gradient, it is a different matrix at each point. The matrix has two readings.
Row i holds all the partials of output fi. It is the gradient of fi from P1.C5, written as a row.
Column j says how all the outputs respond when only input xj is nudged by one unit.
The shape is m×n: (outputs, inputs). That is the same rule as for a matrix W that maps n numbers to m numbers (P1.B5), and it is what makes products of Jacobians line up later.
Example: polar coordinates. The four partials of f(r,θ)=(rcosθ,rsinθ) are ∂r∂x=cosθ,∂θ∂x=−rsinθ,∂r∂y=sinθ and ∂θ∂y=rcosθ. At r=2 and θ=30∘=6π the point is (1.732,1) and
J=(cosθsinθ−rsinθrcosθ)=(0.8660.5−11.732).
Read the columns. Nudge r by 0.01: the point moves by (0.00866,0.005), which is 0.01× column 1, the unit step straight outward. Nudge θ by 0.01 radians: the point moves by about (−0.0101,0.0173), close to 0.01× column 2 =(−0.01,0.0173). Column 2 has length r=2: one radian of angle carries the point a distance r along its circle.
Example: a dense layer. Take the dense layer of P1.B15, y=Wx+b with W=(142536),b=(10,20) and x=(1,0,−1), so y=(8,18). Since y1=x1+2x2+3x3+10 and y2=4x1+5x2+6x3+20,, the six partials are just the entries of W, and J=W, a 2×3 matrix. Check column 1 by nudging x1 from 1 to 1.001:y becomes (8.001,18.004), a change of (0.001,0.004)=0.001×(1,4)..
Zooming in: a curved map looks like a matrix
P1.C1 zoomed into a curve until it looked like a straight line, and P1.C5 zoomed into a surface until it looked like a tilted plane. Here we zoom into a map from the plane to the plane. Move from x by a small step Δ. For a function that is differentiable at x (continuous partial derivatives are enough),
f(x+Δ)≈f(x)+JΔ.
This is the local linear approximation. The effects of nudging each input add up (P1.C4), and the matrix product JΔ is that sum: entry i is ∑jJijΔj. With one output it is exactly the first-order approximation ∇f⋅Δ of P1.C5.
Test it on polar coordinates at (2,30∘), where f=(1.7321,1) and JΔ uses the matrix above. Take Δ=s(0.2,0.2) and shrink s:
s=1,Δ=(0.2,0.2): true f(x+Δ)=(1.6487,1.4566),, approximation (1.7053,1.4464), error (length of the difference) 0.05742.
Halving the step divides the error by about 4(0.0574→0.0143→0.0035). For smooth functions the error typically shrinks like ∥Δ∥2, the same behaviour as in P1.C5, and P1.C8 explains it with second derivatives. The approximation is only local: a big step leaves the straight-line picture far behind.
The picture: a tiny square becomes a parallelogram
Take a small square with corner x and side h. Its bottom side is the step Δ=(h,0), which the map sends to about h×column 1 of J. Its left side is (0,h), which goes to about h×column 2. A linear map sends a square to the parallelogram spanned by its columns (P1.B5), so a smooth map sends a tiny square to a shape that is nearly that parallelogram. Its sides are h times the columns of J, and its area is about ∣detJ∣h2 (P1.B8).
The widget shows both. On the left is the input plane with a square whose lower-left corner is the point p. On the right is the true image of the square (blue, curved) next to the parallelogram of J (orange, straight sides). The right panel is magnified by 1/h, so the shapes stay the same size on the screen while you shrink the square.
area of the true image ÷ area of the square (h²) = 2.15; |det J| = 2
largest gap (largest distance between a point of the blue image and the Jacobian's prediction for the same point) = 0.13; gap ÷ h² = 1.445
At this size the blue image is visibly curved and differs from the orange parallelogram: the largest gap is 0.13. Use “Halve the size” and watch the blue shape settle onto the parallelogram.
Left: the square, with its bottom side in blue and its left side in pink. Right: the same square after the map, magnified by 1 ÷ h, and measured from f(p) (so the axis numbers are changes in the output per unit of h). The blue shape is the true image, with its bottom and left sides in the same colours. The orange dashed parallelogram is what J predicts: its sides are the columns of J, and the arrows are labelled col 1 and col 2. Drag the point p, or focus it and use the arrow keys.
Pick a map, drag the point p (or focus it and use the arrow keys), and change the side h of the square. Left: the input plane with the square (bottom side blue, left side pink). Right: the output near f(p), magnified by 1 divided by h. The blue shape is the true image of the square. The orange dashed parallelogram is what the Jacobian predicts, with sides equal to its columns, which are drawn as arrows. The readouts give J, det J, the area of the true image divided by the area h² of the square, and the largest gap: the largest distance between a point of the square's image and the Jacobian's prediction for the same point.
Things to try with the tiny square:
Start with polar coordinates at (r,θ)=(2,0.524) and h=0.3. The blue image is a patch of a ring, and the orange parallelogram is its straight-edged stand-in. Press “Halve the size” twice, to h=0.15 and h=0.075: the largest gap goes 0.130→0.0322→0.0080,, about four times smaller each time, while the gap divided by h2 stays near 1.4.
Watch the area readout on the same map: 2.15,2.075 and about 2.04 for those three sizes, closing in on ∣detJ∣=2. Drag p to the right, where r is larger: column 2 grows, and detJ equals r.
Switch to the swirl, which rotates each point by an angle that grows with its squared distance from the origin. The shape is sheared and turned, yet the area readout stays at about 1 and detJ=1 at each point and size: the determinant records area, not shape.
On the wobble, detJ=0.772 at the start. Make the square small, then drag p to about (1.5,1.5), where cosx and cosy are close to 0:J is almost the identity, and detJ≈0.999.
The layer is σ(Wx+b) with W=(21−12), the matrix of P1.B8. At (0.5,0.5),detJ=0.231. Drag toward the upper right: the sigmoid flattens out there, and at (1.5,1) the determinant is down to about 0.025. The layer barely responds to its input.
The linear map is the contrast case. The gap is 0 at every size, the area readout is 5=detA (the matrix of P1.B8), and J does not change when you move p.
Special cases
Four cases come up often, and each simplifies the Jacobian.
One output. With m=1 the matrix is 1×n, a single row: the gradient, laid on its side. For f(x,y)=x2+3xy at (2,1) the gradient of P1.C5 is (7,6), and the Jacobian is (76). This site writes the gradient as a column, in the shape of the input, and the Jacobian of a scalar function as a row, so ∇f=J⊤. The first-order formulas agree: ∇f⋅Δ and JΔ are the same number.
One input. With n=1 the matrix is m×1, a single column: the velocity c′(t) of the curve. The curve c(t)=(cost,sint) has J=(−sint,cost)⊤, which at t=6π is (−0.5,0.866), a vector of length 1 tangent to the circle.
A linear map.f(x)=Wx+b has J=W (shape m×n) at every point, and the bias b does not appear. The local linear approximation is exact here, because the function is already linear apart from the shift. This is the contrast preset of the widget.
An elementwise function. If output i depends only on input i (so m=n), the off-diagonal partials are 0 and J=diag(s′(x1),…,s′(xn)) is diagonal. For the sigmoid at z=(1,−1,2) the diagonal is σ′(z)=(0.1966,0.1966,0.1050) (P1.C3 gave σ′=σ(1−σ)). For ReLU the diagonal holds 1 where zi is positive and 0 where it is negative, so at z=(2,−1,3) it is diag(1,0,1) (at the single point zi=0 the slope does not exist, and libraries pick a convention, as in P1.C5).
A whole layer. The layer y=σ(Wx+b) is a linear map followed by an elementwise one. Write z=Wx+b. The next section shows that Jacobians multiply, which gives
J=diag(σ′(z))W.
Multiplying by a diagonal matrix on the left scales each row of W by the slope of its own sigmoid. For the layer of the widget (with b=(0.5,−1)) at x=(0.5,0.5),z=Wx+b=(1,0.5) and σ′(z)=(0.1966,0.2350),, so
That is the matrix the widget shows for the layer preset.
The chain rule for vector functions
When one map feeds another, f∘g means “apply g, then f”. Near x, the map g acts like Jg, and near the point g(x) the map f acts like Jf. Two linear maps in a row are the product of their matrices (P1.B6), so
Jf∘g(x)=Jf(g(x))Jg(x).
Two things to watch. The outer Jacobian is evaluated at the output of g, not at x. And the order is fixed by the shapes: if g:Rn→Rm and f:Rm→Rp, then Jf is p×m,Jg is m×n, and the product is p×n. With all matrices 1×1 this is the rule of P1.C3, f′(g(x))g′(x).
Check it. Let g be polar coordinates at (r,θ)=(2,30∘), so g=(1.732,1) and Jg is the matrix above. Let f(x,y)=(xy,x2+y2),, with Jf=(y2xx2y). At (1.732,1):
Directly, f(g(r,θ))=(r2sinθcosθ,r2),, whose partials at (2,30∘) are (rsin2θ2rr2cos2θ0)=(1.732420).. The two agree, and a central-difference nudge gives the same four numbers.
A network is a long composition, one layer after another, so its Jacobian is a long product JL⋯J2J1. Each factor can shrink or stretch a nudge, which is the origin of vanishing and exploding gradients (P4.16). The order in which a computer multiplies these factors changes the cost but not the answer, as P1.C10 explains.
How much does the map stretch area?
When m=n the Jacobian is a square matrix, and its determinant has the meaning from P1.B8: a tiny square of area h2 becomes a shape of area about ∣detJ∣h2. For a linear map this factor is the same everywhere. For a curved map it changes from point to point, and detJ(x) records it at each point. A negative sign means the map flips the plane over at that point, and detJ=0 means it squashes the square onto a line.
Polar coordinates. Here detJ=cosθ⋅rcosθ−(−rsinθ)sinθ=r.. The widget agrees, and the polar square can be measured exactly: the image of the square [r,r+h]×[θ,θ+h] is a piece of a ring with area 21h((r+h)2−r2)=h2(r+2h).. Dividing by h2 gives r+2h, which tends to r=detJ as h shrinks (2.15,2.075,2.0375 for r=2 and h=0.3,0.15,0.075). A step in θ covers more ground far from the origin, so the factor grows with r.
Shape is another matter. The swirl has detJ=1 at each point, so it keeps areas, yet it shears shapes. The determinant is one number about area, and the full matrix says how shapes change (the Go-deeper box on stretch factors below says how).
Densities and flows. Spreading probability over a larger area makes it thinner. In P1.B8 a map z=f(x) with a constant determinant gave pX(x)=pZ(f(x))∣detA∣.. For a curved map the same rule holds with the local factor,
pX(x)=pZ(f(x))detJf(x),
and a normalizing flow (P8.6) builds a model by chaining many invertible maps and adding up the log∣detJ∣ of each. A small, concrete case: pick r uniformly from [0,1], pick the angle θ in the same way from [0,2π), and plot the point (rcosθ,rsinθ). The density in the (r,θ) plane is 2π1, and the density in the plane of the point at distance ρ from the centre is 2πρ1. Here the input of f is (r,θ) and the output is the point, so the rule above reads p(r,θ)=ppoint⋅ρ, that is, ppoint=p(r,θ)/ρ (divide by detJ=ρ). Points crowd near the centre: half of them fall within ρ<0.5, though that disc has only a quarter of the area. To spread points evenly over a disc, take r=u with u uniform on [0,1].
Products with the Jacobian: forward and backward
A layer with 4096 inputs and 4096 outputs has a Jacobian with 40962=16,777,216 entries, and a whole network has far more. Libraries almost never build it. They compute two kinds of product with it, and each costs a small multiple of one pass through the function (P1.C10).
Jacobian-vector product (JVP).Jv answers: if the input moves in the direction v, how do the outputs move? By the local linear approximation, f(x+tv)≈f(x)+tJv. For polar coordinates at (2,30∘) and v=(0.1,0.2),
and a nudge of size t=0.001 along v moves the point by (−0.000113,0.000396),, which matches. Pushing a nudge forward through the graph is forward mode (P1.C10). With v=ej the product is column j of J, so n products give the whole matrix.
Vector-Jacobian product (VJP). Suppose a layer’s output y=f(x) feeds a loss L, and the gradient g=∇yL is already known. The loss as a function of x is a composition, so by the chain rule its one-row Jacobian is g⊤J, and as a column,
∇xL=J⊤g.
This is a vector-Jacobian product, and it is what the backward pass of a layer computes: gradients are pulled back through a layer with the transpose of its Jacobian. It is reverse mode (P1.C10). With v=ei the product ei⊤J is row i, so m products give the whole matrix. For a loss, m=1, and one VJP gives the entire gradient. That is the reason backpropagation runs backward.
Check it with the dense layer above, J=W, and the made-up loss L=2y1−y2, whose gradient at y is g=(2,−1):
∇xL=W⊤g=123456(2−1)=−2−10.
Raising x1 by 0.001 moved y by (0.001,0.004), so L changed by 2(0.001)−0.004=−0.002,, which is −2×0.001: the first entry. This is the W⊤ that P1.B10 and P1.B15 used for the backward pass. The shapes also fit: g has m=2 entries, and J⊤g has n=3, the shape of the input.
For the sigmoid layer, J=diag(σ′)W gives J⊤g=W⊤(σ′⊙g), where ⊙ multiplies entry by entry: first scale the incoming gradient by the slopes, then multiply by W⊤. At x=(0.5,0.5) with g=(1,−1), the scaled gradient is (0.1966,−0.2350) and
J⊤g=(2−112)(0.1966−0.2350)=(0.158−0.667).
The diagonal matrix need not be stored: it costs one entry-by-entry product. The full treatment of the shapes and gradients for Wx and softmax is P1.C11, and P4.8 runs backpropagation on a whole network with numbers.
A worked example: the softmax Jacobian
Softmax maps scores z to probabilities pi=ezi/∑jezj. Its Jacobian has a compact form, derived in the box below:
JJij=diag(p)−pp⊤,=pi(δij−pj),
where δij is 1 when i=j and 0 otherwise. For z=(1,2,3) the probabilities are p=(0.0900,0.2447,0.6652) and
Four facts to read off it. The diagonal entries pi(1−pi) are positive: raising a score raises its own probability. The off-diagonal entries are negative: raising zj lowers every other probability. The matrix is symmetric. And each row and column sums to 0, because the probabilities add to 1 whatever the scores are, so J1=0: adding the same number to every score changes nothing. A matrix that sends a nonzero vector to 0 is singular, so detJ=0 and its rank is 2 (a central-difference Jacobian gives the same numbers).
A VJP with this matrix gives a well-known result. For the cross-entropy loss L=−lnp3 (the third class is correct) the gradient at the probabilities is g=(0,0,−p31), and
J⊤g=p−e3=(0.0900,0.2447,−0.3348),
the predicted probabilities minus the one-hot target. P3.A8 uses this simple gradient, and P1.C11 derives the matrix-calculus rules behind it.
Go deeper Why the softmax Jacobian has this form
Write S=∑kezk, so pi=ezi/S and ∂zj∂S=ezj. The quotient rule (P1.C2) gives, for i=j,
∂zi∂pi=S2eziS−eziezi=pi(1−pi),
and for i=j the numerator of the top term is 0, so
∂zj∂pi=S20⋅S−eziezj=−pipj.
Together, Jij=pi(δij−pj), which in matrix form is diag(p)−pp⊤.
Computing a Jacobian numerically
P1.C4 and P1.C5 checked a gradient by nudging each input in both directions. The same check builds a Jacobian column by column: column j is
2hf(x+hej)−f(x−hej),
two evaluations of f per input, 2n in all, whatever m is. With h=10−6 the result usually agrees with a correct formula to many digits, which makes it a good test for hand-derived Jacobians and for the layers of a network you wrote yourself. A cheap extra test is a JVP: for a random direction v, compare Jv with the nudge 2tf(x+tv)−f(x−tv),, which needs only two evaluations. Agreement is evidence, not proof.
Go deeper When the Jacobian exists, and what it really claims
The honest definition of “differentiable at x” is that a matrix J exists with
f(x+Δ)=f(x)+JΔ+e(Δ),
where the leftover error e(Δ) is small compared with the step: ∥e∥/∥Δ∥→0 as Δ→0. Then J must be the matrix of partials. The reverse is not true: partial derivatives can exist at a point without this holding (the example x2+y2xy of P1.C4 and P1.C5 is one), so the zoom-in picture can fail. Continuous partial derivatives near the point are enough for the picture to hold. A ReLU layer has kinks where a pre-activation is exactly 0, so its Jacobian does not exist at those points, and software picks a convention there. At other points the Jacobian of a ReLU layer is a diagonal matrix of 0 and 1 times W.
Go deeper Running a map backward: the inverse Jacobian
If f has a differentiable inverse near x, the Jacobian of the inverse at y=f(x) is the inverse matrix, Jf−1(y)=Jf(x)−1 (P1.B9), which needs detJf=0. A standard theorem, the inverse function theorem, says that for continuous partial derivatives and detJ=0 such an inverse exists near the point. For polar coordinates at (2,30∘) the inverse map sends (x,y) back to (r,θ), and
J−1detJ−1=(0.866−0.250.50.433),=0.5=r1.
Going from the plane back to (r,θ) divides areas by r, the opposite of going forward. Flows rely on this: an invertible layer must have a nonzero determinant, and the determinant of the inverse is the reciprocal.
Go deeper Stretch factors: the singular values of the Jacobian
The local linear map J has a singular value decomposition (P1.B13): it turns the tiny square into a parallelogram by rotating, stretching along perpendicular directions, and rotating again. The singular values are the local stretch factors, and their product is ∣detJ∣. For polar coordinates at (2,30∘) the two columns are perpendicular (their dot product is −0.866+0.866=0) with lengths 1 and 2, so the singular values are 2 and 1, and their product is 2=r. A step in r is carried at the same length, a step in θ is stretched by r. When the largest stretch is much bigger than the smallest, the layer treats directions very unequally, which matters for gradients that pass through many layers (P4.16).
Go deeper The Hessian is a Jacobian
The gradient is itself a function from Rn to Rn: it takes a point and returns a vector. Its Jacobian is the n×n matrix of second partial derivatives, the Hessian of P1.C8. For f(x,y)=x2+3xy the gradient is (2x+3y,3x), so the Jacobian of the gradient is (2330). Its off-diagonal entries are equal, which holds whenever the second partials are continuous. P1.C8 uses this matrix to tell bowls from saddles.
The Jacobian in three depths
Intuition
Zoom far enough into a smooth map of the plane, and the curved grid lines look straight, evenly spaced and parallel: a tiny square has become a tiny parallelogram. The Jacobian is the recipe for that parallelogram. Its two columns say where the two sides of the square go, and its determinant says how the area changes.
For a layer with many inputs and outputs the picture has more dimensions, but the story is the same. The Jacobian says how a nudge to the inputs turns into a nudge to the outputs, and the transpose says how a demand on the outputs turns into a demand on the inputs, which is what training needs.
import numpy as npdef num_jac(f, x, h=1e-6): """Central-difference Jacobian: one column per input, 2 evaluations per column.""" x = np.asarray(x, dtype=float) cols = [] for j in range(x.size): e = np.zeros_like(x) e[j] = h cols.append((f(x + e) - f(x - e)) / (2 * h)) return np.array(cols).T # shape (m, n)
PyTorch builds the Jacobian with automatic differentiation (P1.C10), and also offers the two products. This runs in Colab or on your machine, not in the browser here:
import torchfrom torch.autograd.functional import jacobian, vjpdef polar(p): return torch.stack([p[0] * torch.cos(p[1]), p[0] * torch.sin(p[1])])p = torch.tensor([2.0, 0.5235988])print(jacobian(polar, p)) # [[0.8660, -1.0000], [0.5000, 1.7321]]g = torch.tensor([1.0, 0.0]) # a gradient arriving at the two outputsprint(vjp(polar, p, g)[1]) # J^T g, here the first row of J: [0.8660, -1.0000]
Try it: Jacobians in code
The first cell builds a Jacobian by nudging, compares it with the formula, tests the local approximation as the step shrinks, and checks the chain rule. The second cell computes the softmax Jacobian, its facts, and two vector-Jacobian products. Predict the output before you run.
Try it: the Jacobian by nudging, zooming in, and the chain rule
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Challenge: change the point to (r,θ)=(1,2π) and predict J and detJ before you run it.
Try it: the softmax Jacobian and vector-Jacobian products
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Challenge: change the scores to (0,0,0) and print J. Which entries are equal, and why?
Common mistakes
Check your understanding
Let f(x,y)=(x2y,x+y). What is the Jacobian at the point (2,3)?
Row 1 is the gradient of f1=x2y, namely (2xy,x2)=(12,4) at (2,3). Row 2 is the gradient of f2=x+y, namely (1,1). The matrix with 12,1 in its first row has the rows and columns swapped, which would put each output in a column. The matrix starting 6,4 forgets the factor 2 in ∂x∂f1=2xy (it gives xy), and the matrix ending 1,0 drops the derivative of y in the second output.
A function takes 5 inputs and returns 3 outputs. What is the shape of its Jacobian, and what does column 2 describe?
The shape is (outputs, inputs) =3×5. A column belongs to one input, so column 2 holds the three partials of the outputs with respect to x2. The gradient of one output is a row, not a column. A shape of 5×3 reverses the rule, and outputs do not respond to each other, only to inputs.
Two maps have Jacobians Jf=(1023) (at the point where f is evaluated) and Jg=(2101), and f is applied after g. What is the Jacobian of f∘g?
The outer Jacobian goes on the left: JfJg has first row (1⋅2+2⋅1,1⋅0+2⋅1)=(4,2) and second row (0⋅2+3⋅1,0⋅0+3⋅1)=(3,3). The product in the other order is the Jacobian of applying f first. The entry-by-entry product of the two matrices is not a matrix product, and adding them is not the rule for composing maps.
At a point, a map of the plane has detJ=−3. A tiny square of area 0.01 there becomes a shape with about what area, and what happened to its orientation?
The area is multiplied by ∣detJ∣=3, so it is about 0.03, and the negative sign says the plane is flipped over. An area is never negative, so the sign belongs to the orientation, not the size. Dividing by 3 instead of multiplying gives the wrong size, and a negative determinant does not keep the orientation.
A layer y=Wx has W=120013, and the gradient arriving at y is g=(1,−1,2). What gradient does the backward pass send to x?
The backward pass computes J⊤g=W⊤g, with W⊤=(102103): the first entry is 1−2+0=−1 and the second is 0−1+6=5. The result must have 2 entries, the shape of x, so a vector of three entries cannot be right. The pair (5,−1) swaps the entries, and (−1,−5) has the wrong sign on its last entry.
Exercises
Find the Jacobian of f(x,y,z)=(xy,y+z2) at (1,2,3). What is its shape? Show answerThe partials give (y0x102z)=(201106).. There are 2 outputs and 3 inputs, so the shape is 2×3.
Evaluate the polar Jacobian at (r,θ)=(1,2π) and find its determinant. Show answer(cosθsinθ−rsinθrcosθ)=(01−10),, with determinant 0⋅0−(−1)(1)=1=r. A unit step in r moves the point straight up, and a unit step in θ moves it left.
For f(x,y)=(x2,xy) at (1,2), use f(x+Δ)≈f(x)+JΔ to estimate f(1.1,1.9), then find the true value. Show answerJ=(2xy0x)=(2201) and Δ=(0.1,−0.1), so JΔ=(0.2,0.1) and the estimate is (1.2,2.1). The true value is (1.21,2.09), off by (0.01,−0.01).
A ReLU layer has pre-activations z=(2,−1,3). Write its Jacobian, and find what the backward pass sends to the pre-activations when the gradient arriving at the outputs is (5,6,7).Show answerJ=diag(1,0,1). The backward pass multiplies by J⊤=J, giving (5,0,7): the unit with a negative input passes no gradient.
Let g(x)=(x2,3x) and f(u,v)=(u+v,uv). Use the chain rule to find the derivative of f(g(x)) at x=1, and check it by writing f(g(x)) out. Show answerAt x=1,g=(1,3),Jg=(2x3)=(23) and Jf=(1v1u)=(1311). The product is (5,9)⊤. Directly, f(g(x))=(x2+3x,3x3) has derivative (2x+3,9x2)=(5,9).
At a point, J=(3112). By about what factor is the area of a tiny square multiplied, and what area does a tiny square of area 0.01 get? Show answerdetJ=3⋅2−1⋅1=5, so areas are multiplied by 5 and the square gets area about 0.05.
In polar coordinates at r=3, a tiny rectangle has Δr=0.01 and Δθ=0.02. What is the area of its image? Show answerAbout ∣detJ∣ΔrΔθ=3⋅0.01⋅0.02=0.0006..
Compute the softmax Jacobian for the scores z=(0,0). Why is it singular? Show answerp=(0.5,0.5) and diag(p)−pp⊤=(0.25−0.25−0.250.25).. Its determinant is 0: the rows are negatives of each other, and J1=0 because adding the same number to both scores changes nothing.
A layer maps 4 inputs to 2 outputs with a weight matrix W of shape 2×4. What shape does J⊤g have for a gradient g at the outputs, and why is that the right shape? Show answerJ=W is 2×4, so J⊤ is 4×2 and J⊤g has 4 entries. A gradient has the shape of the thing it belongs to, and here that is the input, which has 4 entries.
Find the Hessian of f(x,y)=x2y at (1,2) as the Jacobian of the gradient. Show answerThe gradient is (2xy,x2), and its Jacobian is (2y2x2x0), which at (1,2) is (4220).
Next: P1.C8 takes second derivatives, the Hessian, which is the Jacobian of the gradient, and uses them to tell bowls from saddles and to explain why the error of the straight-line picture shrinks like the square of the step. P1.C10 shows how automatic differentiation computes the products of this lesson without writing the Jacobian out, and P1.C11 collects the matrix-calculus rules for Wx and softmax.
Key takeaways
A function f:Rn→Rm has m×n partial derivatives. The JacobianJ stores them with Jij=∂xj∂fi: row i is the gradient of output i (written as a row), and column j says how all the outputs respond when only input j is nudged. Its shape is (outputs, inputs).
Near a point where f is differentiable, f(x+Δ)≈f(x)+JΔ. A tiny square becomes nearly a parallelogram with sides h× (the columns of J). For polar coordinates at (r,θ)=(2,30∘) the columns are (0.866,0.5) and (−1,1.732).
Special cases: one output gives a one-row matrix, the transpose of the gradient; one input gives a velocity column; a linear map Wx+b has J=W; an elementwise function has a diagonal J, so a layer σ(Wx+b) has J=diag(σ′(z))W.
The chain rule for vector functions multiplies the Jacobians, Jf∘g(x)=Jf(g(x))Jg(x), which is the composition of two linear maps (P1.B6). For a deep network the Jacobian is a long product.
When m=n,∣detJ∣ is the local factor by which areas are multiplied (it is r for polar coordinates); a negative sign means a flip, and zero means a squash. It is the factor that a change of variables and a normalizing flow need.
Nobody builds the full J of a big layer. A Jacobian-vector productJv pushes a nudge forward (forward mode), and a vector-Jacobian productJ⊤g pulls a gradient back (reverse mode, backpropagation). For softmax, J=diag(p)−pp⊤.