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 · 7 min read ·Core ·Foundation

Visual quick review

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.

Learning objective

A numeric vector-to-vector chain rule using a transposed Jacobian

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; columns belong to parameters. Transposition collects every output route into each parameter's gradient. The shapes are (3 x 2)(2 x 1) = 3 x 1. The example follows the JAX vector-Jacobian product contract.
Check: Why does theta2 receive -1 times 5 plus 3 times -2 rather than only one output's contribution?
Full explanation 7 min read
Reading depth

Summary

Matrix calculus transports local changes through vector-valued maps while preserving which output depends on which parameter. Use column gradients and the Jacobian convention throughout this lesson. For a scalar loss, the gradient has the parameter’s shape; the Jacobian records every output-parameter sensitivity. Twice continuously differentiable scalar losses also have a symmetric Hessian of second derivatives.

The four shape combinations

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

Layout names differ across references; inspect the declared Jacobian entries and dimensions rather than guessing from a field’s terminology. Define the matrix gradient by . For , that gradient has shape .

Identities you actually use

FunctionGradient
, symmetric
( PD)

Chain rule

Let and let be the incoming loss gradient. Since , the loss change is

Therefore . This reverse operation is a vector-Jacobian product under the usual row-cotangent terminology, not the forward product .

For , the Jacobian is

With , its transpose returns . For example, parameter two receives through the two output routes.

Learning objective

Contribution tiles: gather into parameter columns

Each input collects contributions from every affected outputOutput gradient (2,1) weights the two Jacobian rows. Summing each input column gives gradient (5,8).Gather every output contribution intoeach input2 units4 units3 units4 unitsInput 1 receives 5; input 2 receives8.Sum down each input column, notacross output rows.
Read it this way: Output gradient (2,1) weights the two Jacobian rows. Summing each input column gives gradient (5,8). Which input loses a contribution if only one output row is propagated?
Reference drawing

Learning objective

Each parameter receives contributions from every output it influences

Each input collects contributions from every affected outputOutput gradient (2,1) weights the two Jacobian rows. Summing each input column gives gradient (5,8).Gather every output contribution intoeach input2 units4 units3 units4 unitsInput 1 receives 5; input 2 receives8.Sum down each input column, notacross output rows.
Derivation
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; columns belong to parameters. Transposition collects every output route into each parameter's gradient. The shapes are (3 x 2)(2 x 1) = 3 x 1. The example follows the JAX vector-Jacobian product contract.
Check: Why does theta2 receive -1 times 5 plus 3 times -2 rather than only one output's contribution?
Read it this way: Output gradient (2,1) weights the two Jacobian rows. Summing each input column gives gradient (5,8). Which input loses a contribution if only one output row is propagated?

Change the Requirement

Differentiate a Trainable Matrix

New task: Differentiate with respect to the matrix, input, and bias.

Picture and change: Expand inside .

Check: For and , .

Use when: Parameters are arrays rather than one vector. For a shared bias across examples, sum example gradients; a mean loss additionally divides by batch size.

Hessian and second-order methods

New task: Obtain curvature along a fixed direction without storing the Hessian .

Picture and change: Differentiate the scalar with respect to . For a twice continuously differentiable loss, this returns .

Check: has . For , the result is . Its Newton step reaches the minimum in one update because this particular loss is a strictly convex quadratic.

Use when: A solver needs Hessian products, not all entries. Automatic differentiation can compose forward and reverse operations without materializing . An indefinite Hessian does not guarantee a descending Newton direction; damping or trust-region safeguards may be needed.

Adam’s second moment is an average of squared stochastic gradients, not generally a Hessian diagonal. K-FAC approximates Fisher-related blocks; Shampoo uses gradient-based preconditioners. Neither should be described as an exact Hessian inverse.

Common pitfalls

  • Wrong orientation. Multiplying a Jacobian by a two-coordinate incoming gradient is dimensionally invalid. Its transpose has the required shape.
  • Wrong parameter shape. A scalar loss can have a matrix gradient.
  • Full Jacobian allocation. A dense Jacobian has entries. Reverse products avoid building that matrix when only one incoming gradient is needed.
  • Wrong reduction. Sum shared-parameter contributions before applying the loss’s declared sum or mean normalization.

Reconstruct

Why is the gradient of not always ?

Reasoned answer

The differential contributes through both occurrences of , giving . It reduces to only when is symmetric. For and , the gradient is , not .