Guides / Matrix calculus
Jacobians and the chain rule: outer Jacobian on the left, and backpropagate with the transpose
Short answer
Compose: for y = g(u) and u = f(x), ∂y/∂x = (∂y/∂u)(∂u/∂x), outer on the left, with shapes (p × m)(m × n) = p × n. Backpropagate: for a scalar L, ∇xL = (∂u/∂x)T∇uL. Through z = Wx that is WTg, and the weights get ∇WL = g xT. Through an elementwise h = φ(z) it is φ′(z) ⊙ g, with φ′ evaluated at the input z, not at the output h.
Why it works
Each map is approximately linear near the point: du ≈ Jf dx, then dy ≈ Jg du. Substituting gives dy ≈ JgJf dx, so the Jacobian of the composition is the product with the outer map on the left: the matrix that acts last is written first, as in y = B(Ax) = (BA)x.
For a scalar loss, transpose the whole product: ∇xL = JfTJgT∇yL. Evaluated right to left, every step is a transposed Jacobian times a vector, so no Jacobian has to be multiplied by another Jacobian. That is backpropagation, and it is why a network's gradient costs about as much as its forward pass. An elementwise layer's Jacobian is diag(φ′(z)), and multiplying a vector by a diagonal matrix is an elementwise product, so that step never builds the matrix at all.
Example 1: multiply the Jacobians, outer on the left
Let u = (x1 + x2, x1x2) and y = (u12, u1 + u2). Find ∂y/∂x at x = (1, 2).
- u = (3, 2)The inner map at the point. The outer Jacobian is evaluated here.
- ∂u/∂x = 11x2x1Row i holds the partials of ui; at (1, 2) the second row is (2, 1).
- ∂y/∂u = 2u1011At u = (3, 2) the first row is (6, 0).
- 60111121Outer Jacobian on the left.
- ∂y/∂x = 6632Check directly: y1 = (x1 + x2)2 has partials 2(x1 + x2) = 6, and y2 = x1 + x2 + x1x2 has (1 + x2, 1 + x1) = (3, 2).
Example 2: a backward pass through a tanh layer
A layer computes z = Wx, h = tanh(z), with W = 1−120 and x = (1, 1). The loss's gradient arriving at h is g = (1, 2). Find ∇xL.
- z = (0, 2)The forward pass: 1 − 1 and 2 + 0.
- h ≈ (0, 0.9640)tanh 0 = 0 and tanh 2 ≈ 0.9640.
- tanh′(z) = 1 − h2The derivative at the input z, computed from the saved output h.
- ≈ (1, 0.07065)1 − 0.96402.
- ∇zL ≈ (1, 0.1413)tanh′(z) ⊙ g: the diagonal Jacobian acts entry by entry.
- ∇xL = WT∇zLThe transposed Jacobian of z = Wx.
- ≈ (1.2826, −1)Rows of WT are (1, 2) and (−1, 0). A finite-difference check of L = gTtanh(Wx) gives the same numbers.
Example 3: the gradient for the weights
For the layer of Example 2, find ∇WL.
- dL = (∇zL)TdzL depends on W only through z.
- dz = (dW)xx is fixed while W changes.
- dL = tr((∇zL xT)TdW)Write the number as a trace and cycle the product.
- ∇WL = ∇zL xTAn outer product: 2 × 1 times 1 × 2, the shape of W.
- ≈ 110.14130.1413Row i is (∇zL)i times xT = (1, 1). Each weight's gradient is the error at its output times the input it multiplies.
Example 4: why backprop multiplies from the loss end
A loss L(h3) sits on three layers of width n = 1000. Its gradient is the product J3J2J1 of a 1 × n row and two n × n Jacobians. Count the multiplications for each order.
- (J3J2)J1From the loss end: each step is a row times a matrix.
- n2 + n2 = 2 × 106A 1 × n row times an n × n matrix costs n2 multiplications, twice.
- J3(J2J1)From the input end: a matrix times a matrix first.
- n3 + n2 ≈ 109An n × n times n × n product costs n3.
- about 500 times cheaperThe product is the same either way, since matrix multiplication is associative. Only the cost differs, and starting from the scalar loss keeps every intermediate a vector.
Common mistakes, and the exact line they happen on
1. Multiplying in the order the forward pass runs
The composition of Example 1.
- ∂u/∂x and ∂y/∂u correctRight so far: both Jacobians are right at the point.
- (∂u/∂x)(∂y/∂u)This is the mistake. f runs first, so its Jacobian looks like it goes first. Both are 2 × 2, so the product exists and the shapes give no warning, but it is 71131, not 6632. The outer Jacobian goes on the left.
2. Evaluating the derivative at the output
The tanh layer of Example 2.
- h ≈ (0, 0.9640)Right so far.
- tanh′(h) ≈ (1, 0.4434)This is the mistake. The derivative belongs at the point tanh was applied to, z = (0, 2). tanh′(h) = 1 − tanh2(h) applies tanh twice. The right factor is 1 − h2 ≈ (1, 0.0707); the wrong one is about six times too big in the second entry and sends (2.7735, −1) back to x.
3. Multiplying by W instead of WT
The last step of Example 2, with ∇zL ≈ (1, 0.1413).
- ∇zL ≈ (1, 0.1413)Right so far.
- W∇zL ≈ (0.8587, 2)This is the mistake. The forward pass multiplies by W, and the backward pass multiplies by its transpose. W is square, so the wrong product has the right shape and nothing complains. The gradient is WT∇zL ≈ (1.2826, −1); a finite-difference check catches the swap at once.
Practice
With A a 5 × 3 matrix and B a 2 × 5 matrix, what is ∂y/∂x for y = B(Ax), and its shape?
Show answer
BA, which is 2 × 3.
Find the Jacobian of h = ReLU(z) at z = (2, −1, 3).
Show answer
diag(1, 0, 1): ReLU′ is 1 where z > 0 and 0 where z < 0.
For L = gT(Wx) with W = 1234 and g = (1, −1), find ∇xL.
Show answer
WTg = (1 − 3, 2 − 4) = (−2, −2).
Find ∇ sin(aTx) at x = (2, −1) for a = (1, 2).
Show answer
cos(aTx) a, and aTx = 0, so (1, 2).
For the layer of Example 2, what is ∇WL if the input is x = (2, 0) instead and ∇zL is unchanged?
Show answer
∇zL xT: first column (2, 0.2826), second column 0. A weight that multiplies a zero input gets no gradient.