Ten problems on Jacobians of elementwise and affine maps, the order of the chain rule, vector-Jacobian products, and gradients through one and two layers, with worked solutions and the shape mistakes that hide inside them.
Before you start
A backward pass is the chain rule applied to vectors and matrices. Each step multiplies by a Jacobian or its transpose, and the wrong answers come from four places: factors in the wrong order, a derivative evaluated at the wrong point, a Jacobian replaced by the vector that describes its action, and factors transposed until the shapes fit. These ten problems work through all four.
The conventions are those of the previous page: vectors are columns, Jacobians are in numerator layout (∂y/∂x is m×n for ,y∈Rm,),x∈Rn), the gradient of a scalar is the transpose of its row derivative, a matrix gradient has the shape of the matrix, and ∥⋅∥ is the Euclidean norm.
σ is the logistic sigmoid, ,σ(t)=1/(1+e−t), applied elementwise to vectors. Its derivative is ,σ′(t)=σ(t)(1−σ(t)), also applied elementwise, so σ′(z)=σ(z)⊙(1−σ(z)) for a vector .z.
⊙ is the elementwise product, .(u⊙v)i=uivi.diag(v) is the diagonal matrix with v on the diagonal, and .diag(v)u=v⊙u.
One layer is ,z=Wx+b,,h=σ(z), with ,W∈Rm×n,x∈Rn and .b,z,h∈Rm.z is the pre-activation and h the output.
The chain rule in numerator layout: for y=g(u) and ,u=f(x),.∂y/∂x=(∂y/∂u)(∂u/∂x). The outer Jacobian goes on the left. With ,y∈Rp,,u∈Rm,x∈Rn the shapes are .(p×m)(m×n)=p×n.
Rule of thumb: for a scalar loss you never need the full Jacobian of an elementwise layer, only its action on a vector, and that action is an elementwise product.
Let h=σ(z) elementwise, .z∈Rn. Compute ∂h/∂z and state its shape.
·
Let z=Wx+b with .W∈Rm×n. Compute ∂z/∂x and .∂z/∂b.
··
Let .h=σ(Wx+b). Compute ∂h/∂x using the chain rule, with shapes at every step.
··
Let y=B(Ax) with ,A∈Rm×n,.B∈Rp×m. Write ∂y/∂x as a product of Jacobians and check the shapes. Which order is forced?
··
Let L=21∥h−y∥2 with h=σ(Wx+b) and y constant. Compute ,∇xL, and write it without any diag matrix.
··
For a scalar loss L and ,h=σ(Wx+b), show that ∇xL=(∂h/∂x)⊤∇hL and simplify it to a form that never forms the m×m Jacobian of .σ. Why does this matter when ?m=4096?
···
Two layers: ,h1=σ(W1x),,h2=W2h1,.L=21∥h2−y∥2. Compute .∇xL.
···
Let L=21∥Wx−y∥2 with .W∈Rm×n. Compute ∇WL and state its shape.
··
Let u=x/∥x∥2 for .x=0. Compute .∂u/∂x.
···
Compute .∇x∥σ(Wx)∥22.
Answers
,∂h/∂z=diag(σ′(z)),n×n
∂z/∂x=W ();m×n);∂z/∂b=Im
,∂h/∂x=diag(σ′(z))W,m×n
,∂y/∂x=BA,;(p×m)(m×n)=p×n;AB is not defined unless ,p=n, and is wrong even then
∇xL=W⊤(σ′(z)⊙(h−y))
;∇xL=W⊤(σ′(z)⊙∇hL); the diag never appears, so the elementwise layer costs O(m) storage instead of the O(m2) of its Jacobian
∇xL=W1⊤(σ′(z1)⊙W2⊤(h2−y))
,∇WL=(Wx−y)x⊤,m×n
,∂u/∂x=∥x∥1(I−x^x^⊤),x^=x/∥x∥
,∇x∥σ(Wx)∥2=2W⊤(σ′(z)⊙σ(z)),z=Wx
Worked solutions
Problem 1
Let h=σ(z) elementwise, .z∈Rn. Compute ∂h/∂z and state its shape.
hi=σ(zi) for .i=1,…,n.Elementwise means output i is computed from input i alone.
∂hi/∂zj=σ′(zi) if ,i=j, and 0 if .i=j.hi contains no zj with ,j=i, so those partials vanish; the remaining one is the ordinary derivative of σ at .zi.
The Jacobian has σ′(z1),…,σ′(zn) on the diagonal and zeros elsewhere.Entry (i,j) of ∂h/∂z is ,∂hi/∂zj, so step 2 fills it entry by entry.
,∂h/∂z=diag(σ′(z)),n×nOne row per hi and one column per ,zj, and both have n entries.
Problem 2
Let z=Wx+b with .W∈Rm×n. Compute ∂z/∂x and .∂z/∂b.
.zi=∑jWijxj+bi.Writing entry i out shows which terms contain each xj and each .bk.
.∂zi/∂xj=Wij.Only term j of the sum contains ,xj, with coefficient ;Wij;bi does not depend on .x.
∂zi/∂bk=1 if ,i=k, and 0 otherwise.bk appears only in ,zk, with coefficient 1, and the sum does not depend on .b.
∂z/∂x=W ();m×n);∂z/∂b=ImStep 2 fills the m×n Jacobian with the entries of ;W; step 3 fills the m×m Jacobian with ones on the diagonal. The previous page's ∂(Ax)/∂x=A is the case .b=0.
Problem 3
Let .h=σ(Wx+b). Compute ∂h/∂x using the chain rule, with shapes at every step.
Let ,z=Wx+b, so .h=σ(z).Naming the pre-activation splits h into two maps whose Jacobians are Problems 1 and 2.
.∂h/∂x=(∂h/∂z)(∂z/∂x).Chain rule in numerator layout: the Jacobian of the outer map, ,σ, goes on the left.
,∂h/∂z=diag(σ′(z)),;m×m;,∂z/∂x=W,.m×n.Problem 1 with m in place of ,n, since z has m entries, and Problem 2.
.(m×m)(m×n)=m×n.The inner dimensions agree, and the result has one row per hi and one column per ,xj, the shape ∂h/∂x must have.
,∂h/∂x=diag(σ′(z))W,m×nRow i of this product is row i of W scaled by :σ′(zi):hi depends on x only through ,zi, and zi depends on x through row i of .W.
Problem 4
Let y=B(Ax) with ,A∈Rm×n,.B∈Rp×m. Write ∂y/∂x as a product of Jacobians and check the shapes. Which order is forced?
Let ,u=Ax, so .y=Bu.Naming the intermediate vector makes y a composition of two linear maps, which is the form the chain rule takes.
,∂u/∂x=A,;m×n;,∂y/∂u=B,.p×m.The Jacobian of a linear map is its matrix (Problem 2 with ).b=0).
.∂y/∂x=(∂y/∂u)(∂u/∂x)=BA.The outer map is ,B, so its Jacobian goes on the left.
.(p×m)(m×n)=p×n. The other order, ,(m×n)(p×m), needs .n=p.Only BA has inner dimensions that agree for every ,m,n,p, and its result is ,p×n, one row per yi and one column per .xj. The shapes force this order.
,∂y/∂x=BA,;(p×m)(m×n)=p×n;AB is not defined unless ,p=n, and is wrong even thenDirectly, ,y=(BA)x, whose Jacobian is .BA. When ,p=n,AB is ,m×m, not ;p×n; when all three sizes are equal it has the right shape, but AB=BA in general.
Problem 5
Let L=21∥h−y∥2 with h=σ(Wx+b) and y constant. Compute ,∇xL, and write it without any diag matrix.
Let ,z=Wx+b, so h=σ(z) and .L=21(h−y)⊤(h−y).The squared norm is the inner product of h−y with itself, which the product rule can differentiate.
,dL=(h−y)⊤dh, so ,∂L/∂h=(h−y)⊤,.1×m.Product rule gives two equal scalar terms, and the 21 cancels the 2; y is constant.
,∂h/∂x=diag(σ′(z))W,.m×n.Problem 3.
,∂L/∂x=(h−y)⊤diag(σ′(z))W, with shapes .(1×m)(m×m)(m×n)=1×n.Chain rule with the outer Jacobian, that of L with respect to ,h, on the left.
,∇xL=W⊤diag(σ′(z))(h−y), with shapes .(n×m)(m×m)(m×1)=n×1.The gradient is the transpose of the row; transposing a product reverses it, and a diagonal matrix is its own transpose.
.diag(σ′(z))(h−y)=σ′(z)⊙(h−y).A diagonal matrix times a vector scales entry i of the vector by diagonal entry ,i, which is exactly the elementwise product.
∇xL=W⊤(σ′(z)⊙(h−y))Substitute step 6 into step 5. Shape check: ,(n×m)(m×1)=n×1, the shape of .x.
Problem 6
For a scalar loss L and ,h=σ(Wx+b), show that ∇xL=(∂h/∂x)⊤∇hL and simplify it to a form that never forms the m×m Jacobian of .σ. Why does this matter when ?m=4096?
,∂L/∂x=(∂L/∂h)(∂h/∂x), with shapes .(1×m)(m×n)=1×n.L depends on x only through ,h, so the chain rule applies with L as the outer map.
.∇xL=(∂h/∂x)⊤(∂L/∂h)⊤=(∂h/∂x)⊤∇hL.Transpose both sides: the gradient is the transposed row, a transposed product reverses its order, and (∂L/∂h)⊤ is by definition .∇hL.
Let .z=Wx+b. Then .(∂h/∂x)⊤=(diag(σ′(z))W)⊤=W⊤diag(σ′(z)).Problem 3, transposed; a diagonal matrix is its own transpose.
.∇xL=W⊤(diag(σ′(z))∇hL).Multiplying right to left means the m×m matrix only ever acts on the vector ,∇hL, so the matrix diag(σ′(z))W is never needed.
.diag(σ′(z))∇hL=σ′(z)⊙∇hL.A diagonal matrix acting on a vector is an elementwise product: m multiplications, and only the m numbers σ′(z) are stored.
;∇xL=W⊤(σ′(z)⊙∇hL); the diag never appears, so the elementwise layer costs O(m) storage instead of the O(m2) of its JacobianAt m=4096 the Jacobian of σ has 40962≈16.8 million entries, about 67 MB in 32-bit floats, per example and per layer, and all but 4096 of them are zero. The elementwise form stores 4096 numbers, and the remaining work is the product with ,W⊤, which the backward pass pays anyway.
Problem 7
Two layers: ,h1=σ(W1x),,h2=W2h1,.L=21∥h2−y∥2. Compute .∇xL.
Let ,z1=W1x, so .h1=σ(z1). Take ,x∈Rn,,W1∈Rk×n,,W2∈Rp×k, so z1,h1∈Rk and .h2,y∈Rp.σ′ has to be evaluated at the pre-activation, so it needs a name; the sizes let every product below be checked.
,∇h2L=h2−y,.p×1.The previous page's ∇x∥Ax−b∥2=2A⊤(Ax−b) with ,A=I, halved.
,∇h1L=W2⊤(h2−y), with shapes .(k×p)(p×1)=k×1.Problem 6's rule: the gradient with respect to a map's input is its transposed Jacobian times the gradient with respect to its output, and ∂h2/∂h1=W2 (Problem 2).
,∇z1L=σ′(z1)⊙∇h1L,.k×1.The same rule through :σ: the transposed Jacobian is ,diag(σ′(z1)), and it acts on a vector as an elementwise product.
,∇xL=W1⊤∇z1L, with shapes .(n×k)(k×1)=n×1.The same rule once more, with .∂z1/∂x=W1.
∇xL=W1⊤(σ′(z1)⊙W2⊤(h2−y))Substitute steps 3 and 4 into step 5. Evaluated from the inside out, this is backpropagation: the last layer's transpose acts first and the first layer's acts last.
Problem 8
Let L=21∥Wx−y∥2 with .W∈Rm×n. Compute ∇WL and state its shape.
Let .r=Wx−y. Then L=21∑iri2 with ,ri=∑jWijxj−yi, so .L=21∑i(∑jWijxj−yi)2.A matrix gradient is defined entry by entry, so write L in terms of the entries .Wij.
.∂L/∂Wij=ri∂ri/∂Wij.Wij sits in row i of ,W, which only builds ,ri, so only the i-th square depends on it; its derivative is ri times the derivative of .ri.
.∂ri/∂Wij=xj.In ,∑j′Wij′xj′, only the j′=j term contains ,Wij, with coefficient .xj.
.(∇WL)ij=(Wx−y)ixj.Entry (i,j) of ∇WL is ∂L/∂Wij (the previous page's convention).
A matrix with entries aibj is the outer product .ab⊤.:(ab⊤)ij=aibj:a supplies the row index and b the column index.
,∇WL=(Wx−y)x⊤,m×nStep 5 with a=Wx−y and .b=x. Shape check: ,(m×1)(1×n)=m×n, the shape of ,W, so W−η∇WL makes sense.
Problem 9
Let u=x/∥x∥2 for .x=0. Compute .∂u/∂x.
.u=x∥x∥−1.A vector times a scalar function of ,x, which the product rule handles.
.du=∥x∥−1dx+xd(∥x∥−1).Product rule; both factors depend on .x.
.d(∥x∥−1)=−∥x∥−2d∥x∥=−∥x∥−3x⊤dx.Scalar chain rule for ,t↦1/t, then ,d∥x∥=x⊤dx/∥x∥, which is the previous page's ∇x∥x∥=x/∥x∥ written as a differential.
.du=∥x∥−1dx−∥x∥−3xx⊤dx.Substitute step 3; x(x⊤dx)=(xx⊤)dx by associativity.
,∂u/∂x=∥x∥1(I−∥x∥2xx⊤),.n×n.The Jacobian is the matrix that multiplies ;dx; factor out .1/∥x∥.
Let ,x^=x/∥x∥, the unit vector along x (it equals ).u). Then .xx⊤/∥x∥2=x^x^⊤.Split ∥x∥2 into one ∥x∥ for each factor of .x.
,∂u/∂x=∥x∥1(I−x^x^⊤),x^=x/∥x∥Sanity check: scaling x does not change ,u, so the Jacobian must send the direction of x to zero, and .(I−x^x^⊤)x^=x^−x^=0.
Problem 10
Compute .∇x∥σ(Wx)∥22.
Let z=Wx and ,h=σ(z), so the function is .f=∥h∥2=h⊤h.Naming the pre-activation and the output splits f into a layer (Problem 3) and a squared norm (previous page).
.∇hf=2h=2σ(z).The previous page's ∇x(x⊤Ax)=(A+A⊤)x with .A=I.
.∇xf=(∂h/∂x)⊤∇hf.Problem 6: the gradient with respect to x is the transposed Jacobian of h times the gradient with respect to .h.
.(∂h/∂x)⊤=W⊤diag(σ′(z)).Problem 3 with ,b=0, transposed.
,∇xf=W⊤(σ′(z)⊙2σ(z)), with shapes .(n×m)(m×1)=n×1.The diagonal matrix acts on 2σ(z) as an elementwise product (Problem 6).
,∇x∥σ(Wx)∥2=2W⊤(σ′(z)⊙σ(z)),z=WxThe scalar 2 moves out of the elementwise product and the matrix product. For the sigmoid, ,σ′(z)⊙σ(z)=h⊙h⊙(1−h), which is how it is computed from the saved forward output.
Where this goes wrong
1. Multiplying the Jacobians in the wrong order
The forward pass applies A first and B second, and scalars commute, so writing the derivatives in the order the maps run looks harmless.
;y=B(Ax);∂(Ax)/∂x=A and ∂(Bu)/∂u=BRight so far: both Jacobians are correct, and only their order is still to be decided.
“A is applied first and B second, so the derivative is A then .B.”The analogy that causes the mistake: reading the product in the order the forward pass runs, as if it were the scalar ,g′(f(x))f′(x), whose factors commute.
∂y/∂x=ABIn numerator layout the inner Jacobian goes on the right: ,y=(BA)x, so the Jacobian is .BA. The shapes refuse :AB:(m×n)(p×m) exists only when ,n=p, and even then it is m×m instead of .p×n.
2. Evaluating σ′ at the output instead of the pre-activation
For the sigmoid, ,σ′(z)=σ(z)(1−σ(z))=h(1−h), so the derivative is usually computed from the saved output .h. Written as a function call, that invites a slip.
,z=Wx+b,,h=σ(z),,L=21∥h−y∥2,∇hL=h−yRight so far: this is steps 1 and 2 of Problem 5.
,σ′(z)=h⊙(1−h), “so the sigmoid's derivative is a function of h”The identity is true; reading it as “σ′ takes h” is the analogy that causes the mistake.
∇xL=W⊤(σ′(h)⊙(h−y))σ′ is a function of :z: the chain rule evaluates the derivative of σ at the point σ was applied to. σ′(h)=σ(h)(1−σ(h)) applies the sigmoid twice. The correct line is ,W⊤(σ′(z)⊙(h−y)), which is .W⊤(h⊙(1−h)⊙(h−y)). The slip survives because for ,h∈(0,1),σ′(h) lies between about 0.197 and 0.25, so the gradient has a plausible size. A finite-difference check catches it at once, and so does noticing that the factor never drops below about 0.197, where the true σ′(z) goes to 0 for large .∣z∣.
3. Writing the Jacobian of an elementwise layer as a vector
Problem 6 says never to build diag(σ′(z)) in code, and the shorthand that replaces it leaks back into the derivation.
,h=σ(z),,L=21∥h−y∥2,,∂L/∂h=(h−y)⊤,1×mRight so far: the row derivative of the loss with respect to .h.
“The m×m Jacobian is diagonal, so keep only the diagonal.”The analogy that causes the mistake: the storage advice of Problem 6, which is about the product, is applied to the Jacobian itself.
∂h/∂z=σ′(z) (a vector)An m×1 vector is not the Jacobian of an m-vector with respect to an m-vector. The chain rule then gives ,(h−y)⊤σ′(z), a 1×1 inner product where a 1×m row belongs. As a Jacobian, in a derivation, write ,diag(σ′(z)),.m×m. As a vector-Jacobian product, in code or a gradient formula, write σ′(z)⊙g for the incoming gradient .g. Both say the same thing; the bare vector says neither.
4. Transposing until the shapes fit
The scalar rule dwd21(wx−y)2=(wx−y)x says there are two factors, Wx−y and ,x, and it is tempting to arrange them in whichever order multiplies.
L=21∥Wx−y∥2 with ;W∈Rm×n; the factors are Wx−y ()m×1) and x ()n×1)Right so far: these are the two factors in the gradient.
“(Wx−y)⊤x does not multiply unless ,m=n, so transpose the other one.”The analogy that causes the mistake: the scalar rule gives the factors but not their arrangement, and the shapes are used to pick one.
∇WL=x(Wx−y)⊤It multiplies, but it is ,n×m, the transpose of W's shape, so W−η∇WL is undefined unless ,m=n, and wrong even then. The index derivation (Problem 8) gives :∂L/∂Wij=(Wx−y)ixj: the row index belongs to Wx−y and the column index to ,x, so the gradient is .(Wx−y)x⊤. The gradient must have W's shape, and that fixes which factor is the column.