Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Matrix and vector calculus

Derivatives with Respect to Vectors and Matrices

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.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Three kinds of “how does it change?”

  1. One number from many. A game score depends on 4 sliders. Its sensitivity is a list of 4 numbers, one per slider: a gradient.
  2. Many numbers from many. 3 outputs from 5 inputs need a 3 × 5 table, the Jacobian.
  3. 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.8001.800
-1.7000.600
-1.1001.600
finite-difference check · 3×2
-0.8001.800
-1.7000.600
-1.1001.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. 1. A score from 6 sliders: how many numbers in its gradient?
  2. 2. One score from a 5 × 5 image: how many sensitivities?
  3. 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⊤xf = \mathbf a^\top\mathbf x ∇xf=a\nabla_{\mathbf x}f = \mathbf a
vector w.r.t. vector y=Ax\mathbf y = A\mathbf x ∂y/∂x=A\partial\mathbf y/\partial\mathbf x = A
scalar w.r.t. matrix f=a⊤Xb=∑ijaiXijbjf = \mathbf a^\top X\mathbf b = \sum_{ij}a_iX_{ij}b_j ∂f/∂X=ab⊤\partial f/\partial X = \mathbf a\mathbf b^\top
scalar w.r.t. matrix f=∥X∥F2f = \lVert X\rVert_F^2 ∂f/∂X=2X\partial f/\partial X = 2X

The bilinear case is worth seeing entry by entry: ∂f/∂Xij=aibj\partial f/\partial X_{ij} = a_ib_j, which is exactly the (i,j)(i,j) entry of the outer product ab⊤\mathbf a\mathbf b^\top.

📝 Quick check (Level 1)
0/3 answered
  1. 1. ∂(aᵀXb)/∂X for a = (1, 2), b = (3, 4): the (2, 2) entry?
  2. 2. ∂(Ax)/∂x = ?
  3. 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\nabla(\alpha f + \beta g) = \alpha\nabla f + \beta\nabla g
quadratic form ∇(x⊤Ax)=(A+A⊤)x\nabla(\mathbf x^\top A\mathbf x) = (A + A^\top)\mathbf x
product of vector functions ∇(u⊤v)=Ju⊤v+Jv⊤u\nabla(\mathbf u^\top\mathbf v) = J_{\mathbf u}^\top\mathbf v + J_{\mathbf v}^\top\mathbf u
element-wise function σ(x)\sigma(\mathbf x) J=diag⁡(σ′(x))J = \operatorname{diag}(\sigma'(\mathbf x)), so multiply element-wise: σ′(x)⊙v\sigma'(\mathbf x)\odot\mathbf v
layer y=Wx\mathbf y = W\mathbf x, upstream δ=∂L/∂y\boldsymbol\delta = \partial L/\partial\mathbf y ∂L/∂W=δx⊤\partial L/\partial W = \boldsymbol\delta\mathbf x^\top, ∂L/∂x=W⊤δ\partial L/\partial\mathbf x = W^\top\boldsymbol\delta

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⊤W^\top.

🐍 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. 1. The Jacobian of an element-wise function is…
  2. 2. For y = Wx with upstream δ, ∂L/∂W = ?
  3. 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)=A dxd(A\mathbf x) = A\,d\mathbf x, the product rule d(u⊤v)=du⊤v+u⊤dvd(\mathbf u^\top\mathbf v) = d\mathbf u^\top\mathbf v + \mathbf u^\top d\mathbf v, and the trace trick (A3.3-1):

  • d(x⊤Ax)=dx⊤Ax+x⊤A dx=x⊤(A+A⊤)dxd(\mathbf x^\top A\mathbf x) = d\mathbf x^\top A\mathbf x + \mathbf x^\top A\,d\mathbf x = \mathbf x^\top(A + A^\top)d\mathbf x, so the gradient is (A+A⊤)x(A + A^\top)\mathbf x.
  • d(a⊤Xb)=a⊤dX b=tr⁡(ba⊤dX)=⟨ab⊤,dX⟩d(\mathbf a^\top X\mathbf b) = \mathbf a^\top dX\,\mathbf b = \operatorname{tr}(\mathbf b\mathbf a^\top dX) = \langle\mathbf a\mathbf b^\top, dX\rangle, so the gradient is ab⊤\mathbf a\mathbf b^\top.

The Hessian is the Jacobian of the gradient. For x⊤Ax\mathbf x^\top A\mathbf x it is A+A⊤A + A^\top.

Matrix-to-matrix maps: vectorization

For F(X)=AXBF(X) = AXB, stack columns: vec⁡(AXB)=(B⊤⊗A)vec⁡(X)\operatorname{vec}(AXB) = (B^\top\otimes A)\operatorname{vec}(X). The full Jacobian is a Kronecker product (A2.2-6) of size (mq)×(np)(mq)\times(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=δ⊤(dW x+W dx)=⟨δx⊤,dW⟩+(W⊤δ)⊤dxdL = \boldsymbol\delta^\top d\mathbf y = \boldsymbol\delta^\top(dW\,\mathbf x + W\,d\mathbf x) = \langle\boldsymbol\delta\mathbf x^\top, dW\rangle + (W^\top\boldsymbol\delta)^\top d\mathbf x. So ∂L/∂W=δx⊤\partial L/\partial W = \boldsymbol\delta\mathbf x^\top and ∂L/∂x=W⊤δ\partial L/\partial\mathbf x = W^\top\boldsymbol\delta.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The Hessian of xᵀAx is…
  2. 2. vec(AXB) = ?
  3. 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\partial L/\partial W = \sum_i\boldsymbol\delta_i\mathbf x_i^\top = \Delta^\top 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∥\lVert\boldsymbol\delta\mathbf x^\top\rVert_F = \lVert\boldsymbol\delta\rVert\lVert\mathbf x\rVert, 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\mathbf x and of δ\boldsymbol\delta 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\partial s/\partial\mathbf 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)M(\mathbf 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

  1. M. Abadi, A. Chu, I. Goodfellow, H. B. McMahan, I. Mironov, K. Talwar, L. Zhang (2016). “Deep learning with differential privacy.” ACM CCS.
  2. J. Martens, R. Grosse (2015). “Optimizing neural networks with Kronecker-factored approximate curvature.” ICML.
  3. K. Simonyan, A. Vedaldi, A. Zisserman (2014). “Deep inside convolutional networks: visualising image classification models and saliency maps.” ICLR Workshop.
  4. M. Sundararajan, A. Taly, Q. Yan (2017). “Axiomatic attribution for deep networks.” ICML.
  5. J. Carpentier, N. Mansard (2018). “Analytical derivatives of rigid body dynamics algorithms.” RSS.
  6. J. Magnus, H. Neudecker (2019). Matrix Differential Calculus, 3rd ed. Wiley.