Skip to content
mlmentorship

Matrix calculus for ML

Gradients, Jacobians, and Hessians for vector- and matrix-valued functions. The minimum needed to derive backprop and second-order methods.

Published · 5 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

Use Jacobian dimensions to trace how a transposed Jacobian maps an output-shaped upstream gradient to a parameter-shaped gradient, with each parameter collecting sensitivity through every output.

Preparing the visual…

Summary

Matrix calculus extends the ordinary derivative to functions whose inputs and/or outputs are vectors or matrices. The two organizing objects are the gradient (for scalar-valued functions) and the Jacobian (for vector-valued functions); the Hessian is the matrix of second partial derivatives.

Every learning algorithm computes derivatives of a scalar loss with respect to vector- or tensor-valued parameters. Knowing the right shapes and conventions saves hours of debugging. Backpropagation is matrix calculus applied recursively through a computation graph.

The four shape combinations

Gradient Jacobian ,
Second derivative: Hessian , (rarely used; tensor-valued)

Two competing conventions exist:

  • Numerator layout (used in physics, calculus textbooks): is a column vector matching .
  • Denominator layout (used in stats, ML): same; modern ML universally uses gradient = column vector with the same shape as the parameter.

For matrix parameters , the gradient has the same shape as .

Identities you actually use

FunctionGradient
, symmetric
( PD)

Chain rule

For :

  • Scalar scalar scalar: .
  • Vector vector scalar: where .

Backprop is just this chain rule applied to a computation graph layer-by-layer, with the Jacobian-vector product computed implicitly to avoid materializing the full Jacobian.

Learning objective

Use shapes to see how the transposed Jacobian pulls two output gradients back to three parameters.

A numeric vector-to-vector chain rule using a transposed Jacobian A function maps three parameters to two outputs, so its Jacobian has two rows and three columns. The upstream loss gradient is the two-vector 5, negative 2. Transposing the Jacobian produces a three-by-two matrix. Multiplication gives the three parameter gradients 8, negative 11, and negative 8. Each parameter gradient combines that parameter's sensitivity through both outputs. FORWARD SHAPE: 3 PARAMETERS -> 2 OUTPUTS theta 3 x 1 f, with J: 2 x 3 y = f(theta) 2 x 1 BACKWARD SHAPE: J^T g_y = g_theta J^T: 3 x 2 [ 2 1 ] [ -1 3 ] [ 0 4 ] x g_y [ 5 ] [ -2 ] = g_theta [ 8 ] [-11] [-8 ] EACH ROW COLLECTS BOTH OUTPUT ROUTES dL/dtheta1 = 2(5) + 1(-2) = 8 dL/dtheta2 = -1(5) + 3(-2) = -11 dL/dtheta3 = 0(5) + 4(-2) = -8
Read it this way: rows of J belong to outputs and columns belong to parameters. Transposing J puts one parameter on each result row, so multiplying by the two upstream loss gradients sums every output route into that parameter's gradient. The inner dimensions cancel: (3 x 2)(2 x 1) = 3 x 1. This is an original worked example checked against the JAX vector-Jacobian product contract.

Hessian and second-order methods

The Hessian describes local curvature. Second-order methods (Newton’s method, K-FAC, Shampoo) use it to scale gradients by inverse curvature: .

In modern deep learning, the Hessian is too large to materialize ( for parameters). Approximations:

  • Diagonal: keep only diagonal entries (RMSProp, Adam’s approximates this).
  • Block-diagonal: per-layer Fisher information (K-FAC).
  • Hessian-vector products: via two backward passes. Used in conjugate gradient, influence functions.

Common pitfalls

  • Mismatched layout conventions. Always check whether your reference uses numerator or denominator layout; the difference is a transpose.
  • Treating gradients as having the same shape as the loss. They have the shape of the parameter, not the loss.
  • Computing Jacobians explicitly. For a vector-to-vector function with both large, the Jacobian is entries. Use vector-Jacobian products via torch.autograd.grad or jax.vjp instead.
  • Forgetting bias dimensions. has summed over the batch dimension.