There are three cases in practice: a scalar with respect to a vector (a gradient), a vector with respect to a vector (a Jacobian), and a scalar with respect to a matrix (a weight gradient). With a few rules (linearity, product rule, element-wise functions, outer products), you can differentiate any neural-network layer by hand.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Three kinds of “how does it change?”
One number from many. A game score depends on 4 sliders. Its sensitivity is a list of 4 numbers, one per slider: a gradient.
Many numbers from many. 3 outputs from 5 inputs need a 3 × 5 table, the Jacobian.
One number from a grid. A “cat score” computed from a 10 × 10 image has a sensitivity for every pixel: a 10 × 10 grid called a saliency map.
Matrix-calculus explorer: formula vs finite differences
shapes: A: 3×2, x: 2, y: 3
rule: ∂y/∂x = A (numerator) or Aᵀ (denominator) · showing numerator (m×n) layout
input (x or X) · 2×1
-0.300
0.500
closed-form derivative · 3×2
-0.800
1.800
-1.700
0.600
-1.100
1.600
finite-difference check · 3×2
-0.800
1.800
-1.700
0.600
-1.100
1.600
largest difference: 8.8e-11 ✔ the formula is right
A vector-valued map: its derivative is a full matrix (the Jacobian).
Why engineers care
A neural network’s weights form big matrices. Training needs, for each weight, how much the error changes if that weight is nudged: a scalar’s derivative with respect to a matrix. Robot perception systems use saliency maps to check which pixels a vision model relies on.
📝 Quick check (Level 0)
0/3 answered
1. A score from 6 sliders: how many numbers in its gradient?
2. One score from a 5 × 5 image: how many sensitivities?
3. 3 outputs from 5 inputs need a table with…
Level 1: Beginner
★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.
The three cases
Case
Example
Derivative
scalar w.r.t. vector
f=a⊤x
∇xf=a
vector w.r.t. vector
y=Ax
∂y/∂x=A
scalar w.r.t. matrix
f=a⊤Xb=∑ijaiXijbj
∂f/∂X=ab⊤
scalar w.r.t. matrix
f=∥X∥F2
∂f/∂X=2X
The bilinear case is worth seeing entry by entry: ∂f/∂Xij=aibj, which is exactly the (i,j) entry of the outer product ab⊤.
📝 Quick check (Level 1)
0/3 answered
1. ∂(aᵀXb)/∂X for a = (1, 2), b = (3, 4): the (2, 2) entry?
2. ∂(Ax)/∂x = ?
3. ∂‖X‖²_F/∂X = ?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.
Rules you’ll use every day
Rule
Formula
linearity
∇(αf+βg)=α∇f+β∇g
quadratic form
∇(x⊤Ax)=(A+A⊤)x
product of vector functions
∇(u⊤v)=Ju⊤v+Jv⊤u
element-wise function σ(x)
J=diag(σ′(x)), so multiply element-wise: σ′(x)⊙v
layer y=Wx, upstream δ=∂L/∂y
∂L/∂W=δx⊤, ∂L/∂x=W⊤δ
The last row is the core of every linear layer’s backward pass. The weight gradient is an outer product (rank 1 per example), and the input gradient is multiplied by W⊤.
🐍 Exercise 1: gradients w.r.t. vectors and matrices
Derive ∇(‖Ax‖² + aᵀx) and ∂(aᵀXb + ‖X‖²_F)/∂X, and check every entry against finite differences.
📝 Quick check (Level 2)
0/3 answered
1. The Jacobian of an element-wise function is…
2. For y = Wx with upstream δ, ∂L/∂W = ?
3. ∇(xᵀx) at (1, 2, 3): the third component?
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.
Differentials, systematically
Using d(Ax)=Adx, the product rule d(u⊤v)=du⊤v+u⊤dv, and the trace trick (A3.3-1):
d(x⊤Ax)=dx⊤Ax+x⊤Adx=x⊤(A+A⊤)dx, so the gradient is (A+A⊤)x.
d(a⊤Xb)=a⊤dXb=tr(ba⊤dX)=⟨ab⊤,dX⟩, so the gradient is ab⊤.
The Hessian is the Jacobian of the gradient. For x⊤Ax it is A+A⊤.
Matrix-to-matrix maps: vectorization
For F(X)=AXB, stack columns: vec(AXB)=(B⊤⊗A)vec(X). The full Jacobian is a Kronecker product (A2.2-6) of size (mq)×(np). It is huge, which is why frameworks never form it. They use the products from A3.3-6 instead.
Exercise 3.1: Derive the backward pass of y = Wx from differentials
dL=δ⊤dy=δ⊤(dWx+Wdx)=⟨δx⊤,dW⟩+(W⊤δ)⊤dx. So ∂L/∂W=δx⊤ and ∂L/∂x=W⊤δ.
📝 Quick check (Level 3)
0/3 answered
1. The Hessian of xᵀAx is…
2. vec(AXB) = ?
3. d(aᵀXb) = ?
Level 4: Expert
★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.
Per-example gradients and their structure
For a batch, ∂L/∂W=∑iδixi⊤=Δ⊤X, a sum of rank-1 outer products. Exploiting this structure enables:
Differentially private training (DP-SGD; Abadi et al., 2016) clips each example’s gradient. Since ∥δx⊤∥F=∥δ∥∥x∥, the norms are cheap and no per-example matrix is needed (Exercise 2).
Kronecker-factored optimizers (K-FAC; Martens and Grosse, 2015; Shampoo) approximate curvature using the statistics of x and of δ separately.
Low-rank fine-tuning. LoRA (A2.3-6) restricts updates to low rank, consistent with the low-rank structure of gradients.
🐍 Exercise 2 (research): per-sample gradients and DP-style clipping
Form per-sample linear-layer gradients as outer products, check they sum to ΔᵀX and each has rank 1, then compute the clipped sum cheaply using ‖δxᵀ‖ = ‖δ‖‖x‖.
More uses
Saliency and attribution.∂s/∂x for an image shows which pixels matter (Simonyan, Vedaldi and Zisserman, 2014). Integrated gradients improve on it (Sundararajan, Taly and Yan, 2017). Robotics teams use these maps to audit what a perception network attends to (Practice Code 5).
Robot dynamics. Derivatives of the mass matrix M(q) with respect to joint angles give Coriolis terms. Analytic derivatives of rigid-body dynamics (Carpentier and Mansard, 2018) power fast model-predictive control.
References
M. Abadi, A. Chu, I. Goodfellow, H. B. McMahan, I. Mironov, K. Talwar, L. Zhang (2016). “Deep learning with differential privacy.” ACM CCS.
J. Martens, R. Grosse (2015). “Optimizing neural networks with Kronecker-factored approximate curvature.” ICML.
K. Simonyan, A. Vedaldi, A. Zisserman (2014). “Deep inside convolutional networks: visualising image classification models and saliency maps.” ICLR Workshop.
M. Sundararajan, A. Taly, Q. Yan (2017). “Axiomatic attribution for deep networks.” ICML.
J. Carpentier, N. Mansard (2018). “Analytical derivatives of rigid body dynamics algorithms.” RSS.
J. Magnus, H. Neudecker (2019). Matrix Differential Calculus, 3rd ed. Wiley.
Per-example weight gradients are rank-1 outer products. This is exploited for privacy, curvature approximations and low-rank adaptation.
Practice
Show:
Level 0
Problem 1Level 0
A score depends on 4 sliders. How many numbers are in its gradient?
4.
Problem 2Level 0
How many entries does the Jacobian of 3 outputs from 5 inputs have?
15.
Problem 3Level 0
One score from a 10 × 10 image. How many entries in its saliency map?
100.
Level 1
Problem 4Level 1
The second component of ∇(a⊤x) with a=(2,−3)?
−3.
Problem 5Level 1
∂(Ax)/∂x with A=(1324): the (2,1) entry?
3.
Problem 6Level 1
∂(a⊤Xb)/∂X with a=(1,2) and b=(3,4): the (2,2) entry?
a2b2=8.
Problem 7Level 1
∂∥X∥F2/∂X11 when X11=5?
10.
Level 2
Problem 8Level 2
A diagonal entry of the Jacobian of σ(x) at x=0?
σ′(0)=0.25.
Problem 9Level 2
The third component of ∇(x⊤x) at (1,2,3)?
6.
Problem 10Level 2
∇(x⊤Ax) with A=2I at (1,1): the first component?
(A+A⊤)x=4x, so 4.
Problem 11Level 2
∇(a⊤x)2=2(a⊤x)a with a=(1,1) and x=(2,3): the first component?
2⋅5⋅1=10.
Problem 12Level 2
How many entries does the full Jacobian of a map from 2×3 matrices to 2×3 matrices have?
6×6=36.
Problem 13Level 2
The first component of ∇1⊤ReLU(x) at x=(−1,2)?
0.
Level 3
Problem 14Level 3
How many entries does the vec-Jacobian of X↦AXB have, for A 2×3, X 3×4 and B 4×5?
The output has 10 entries and the input 12: 10×12=120.
Problem 15Level 3
∇(x⊤Ax) with A=(0010) at (1,1): the first component?
(A+A⊤)x=(1,1), so 1.
Problem 16Level 3
The Hessian of x⊤Ax with A=(1023): the (1,2) entry?
(A+A⊤)12=2+0=2.
Problem 17Level 3
Prove that ∂(a⊤Xb)/∂X=ab⊤.
a⊤Xb=∑ijaiXijbj, so ∂/∂Xij=aibj=(ab⊤)ij. ∎
Level 4
Problem 18Level 4
What is the rank of a single example’s linear-layer weight gradient δx⊤?
1.
Problem 19Level 4
DP clipping: ∥x∥=3, ∥δ∥=4, C=6. The scale factor min(1,C/∥δx⊤∥)?
6/12=0.5.
Problem 20Level 4
A batch of 32 rank-1 per-example gradients for a 100 × 100 weight. The largest possible rank of their sum?
32.
Coding practice
🐍 Code 1 (Level 1): the three basic derivatives
Return ∇(aᵀx), ∂(Ax)/∂x and ∂(aᵀXb)/∂X.
🐍 Code 2 (Level 2): element-wise functions
Show the sigmoid's Jacobian is diagonal, and compute ∇ₓ 1ᵀσ(Wx) = Wᵀσ′(Wx).
🐍 Code 3 (Level 2): the product rule for vectors
Differentiate (Ax)ᵀ(Bx) with the product rule, and check it numerically.
🐍 Code 4 (Level 3): the vec–Kronecker Jacobian
Verify vec(AXB) = (Bᵀ ⊗ A) vec(X) with column-major vec, and see how big the Jacobian gets.
🐍 Code 5 (Level 4): a saliency map
For the score w₂ᵀReLU(W₁x) of an 8 × 8 image, compute ∂s/∂x by hand, check it against finite differences, and find the most influential pixels.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Derivatives with Respect to Vectors and Matrices: topic test
10 questions, drawn at random from a bank of 16. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.