Hyoungseo Son
Matrix Calculus & Autodiff
Contents · Matrix Calculus & Autodiff

Part 0 · Matrix Calculus

  • Matrix Calculus: Gradients and Jacobians

Part 1 · Automatic Differentiation

Part 2 · Backpropagation

Part 3 · Differentiable Simulation

Part 0 · Matrix Calculus

Matrix Calculus: Gradients and Jacobians

Calculus on scalars asks how one output moves when one input moves. Vector calculus asks the same of many outputs and many inputs at once, and packs the answer into a matrix. That matrix, the Jacobian, is the derivative: it is the best linear map that copies the behavior of a nonlinear function near a point. Every dynamics linearization, policy gradient, and differentiable rollout is an instance of assembling and multiplying these matrices.

Gradients and Jacobians

For a scalar field f:RnRf:\mathbb{R}^n\to\mathbb{R} the derivative is the gradient, the column of partials f=(f/x1,,f/xn)\nabla f = (\partial f/\partial x_1,\dots,\partial f/\partial x_n)^{\top}. For a vector field f:RnRmf:\mathbb{R}^n\to\mathbb{R}^m each output has its own gradient, and stacking them by row gives the Jacobian

J=fxRm×n,Jij=fixj.J = \frac{\partial f}{\partial x}\in\mathbb{R}^{m\times n}, \qquad J_{ij} = \frac{\partial f_i}{\partial x_j}.

Row ii is fi\nabla f_i^{\top}; column jj is f/xj\partial f/\partial x_j, the direction the whole output moves when input jj moves. Two conventions coexist. The numerator layout above sends outputs to rows, so JRm×nJ\in\mathbb{R}^{m\times n}. The denominator layout transposes this, putting inputs on rows, so a scalar gradient comes out as a column. The math is identical; only the shapes and the order of factors in the chain rule flip. Pick one and hold it fixed.1

The Jacobian is the best local linear map

Near a point xx the first-order Taylor expansion is

f(x+δ)=f(x)+Jδ+o(δ).f(x+\delta) = f(x) + J\,\delta + o(\lVert\delta\rVert).

So JδJ\delta is the linear map that best reproduces ff for small displacements: the derivative is not a number attached to a point, it is a matrix. Feed it the basis vector eje_j and you recover column jj, confirming that columns are the responses to unit input moves.

Because JJ is a linear map, its geometry is the SVD story. Write J=UΣVJ = U\Sigma V^{\top}. A small sphere of perturbations δ=ε\lVert\delta\rVert = \varepsilon around xx is sent to an ellipsoid around f(x)f(x) whose semi-axes are εσiui\varepsilon\,\sigma_i u_i: the singular values are the local stretch factors and detJ=iσi\det J = \prod_i \sigma_i is the local volume scale.1 The right singular vectors viv_i mark the input directions that stretch most and least; each maps to σiui\sigma_i u_i.

The chain rule is matrix multiplication

Compose h=fgh = f\circ g. The Jacobian of the composition is the product of the Jacobians, in output-to-input order,

Jfg(x)=Jf ⁣(g(x))Jg(x).J_{f\circ g}(x) = J_f\!\big(g(x)\big)\,J_g(x).

This one identity is the whole of backpropagation and of gradient flow through a simulator: a deep network or a TT-step rollout is a long composition, and its Jacobian is the product of the per-layer or per-step Jacobians.2 Forward mode multiplies this product right to left onto an input perturbation; reverse mode multiplies left to right onto an output cotangent, which is why reverse mode is cheap when there are few outputs, such as a scalar loss.

A worked 2×2 example

Take f(x,y)=(x2y,  xy)f(x,y) = (x^2 - y,\; xy). Differentiating each component,

J=[(x2y)x(x2y)y(xy)x(xy)y]=[2x1yx],J(1,2)=[2121].J = \begin{bmatrix} \dfrac{\partial(x^2-y)}{\partial x} & \dfrac{\partial(x^2-y)}{\partial y} \\[6pt] \dfrac{\partial(xy)}{\partial x} & \dfrac{\partial(xy)}{\partial y} \end{bmatrix} = \begin{bmatrix} 2x & -1 \\ y & x \end{bmatrix}, \qquad J\big|_{(1,2)} = \begin{bmatrix} 2 & -1 \\ 2 & 1 \end{bmatrix}.

Verify column one against a central finite difference with h=103h = 10^{-3}. The column is f/x\partial f/\partial x, so perturb only xx at the point (1,2)(1,2):

f(1+h,2)=(0.997999,  2.002),f(1h,2)=(1.001999,  1.998),f(1{+}h,\,2) = (-0.997999,\; 2.002),\qquad f(1{-}h,\,2) = (-1.001999,\; 1.998), f(1+h,2)f(1h,2)2h=(0.004,  0.004)0.002=[2.0002.000]=[2xy](1,2).\frac{f(1{+}h,2) - f(1{-}h,2)}{2h} = \frac{(0.004,\; 0.004)}{0.002} = \begin{bmatrix} 2.000 \\ 2.000 \end{bmatrix} = \begin{bmatrix} 2x \\ y \end{bmatrix}\bigg|_{(1,2)}.

The finite difference reproduces the analytic column to the digits shown, with error O(h2)O(h^2). This is exactly the check the demo runs live for every point.

Jacobian as a local linear mapinteractive
input: circle at p (drag)
output: J maps it to an ellipse
σ₁u₁σ₂u₂exact image f(p+δ)solid = J·δ (linear)
p = (1.00, 2.00)
J analytic
2.000-1.0002.0001.000
J finite-diff (h=1e-3)
2.000-1.0002.0001.000
det J
4.00
σ₁
2.83
σ₂
1.41
Drag the input point p. Left: a circle of radius ε of input perturbations, with the right singular directions v₁, v₂. Right: its image. The solid curve is the exact nonlinear image f(p+δ); the filled ellipse is the linear prediction J·δ with semi-axes σ₁u₁, σ₂u₂. Shrink ε and the two coincide, the meaning of best local linear map. The J readouts show the analytic and h=1e-3 finite-difference matrices agreeing entry by entry.

Where these Jacobians show up

Linearizing continuous dynamics x˙=f(x,u)\dot{x} = f(x,u) gives the pair A=x˙/xA = \partial\dot{x}/\partial x and B=x˙/uB = \partial\dot{x}/\partial u; LQR and iLQR consume exactly these Jacobians at each nominal point to build their local quadratic models. In reinforcement learning the policy gradient is a Jacobian of sampled returns with respect to parameters, and value-function fitting rides the Jacobian of the critic. Differentiable simulation multiplies per-step Jacobians through a rollout so a loss can be pushed back onto controls, masses, or contact parameters.2 In every case the object being assembled is the same matrix of partials, and the chain rule is the same matrix product.

Footnotes

  1. Petersen & Pedersen, The Matrix Cookbook (2012). 2

  2. Baydin et al., Automatic Differentiation in Machine Learning: a Survey (2018). 2