Matrix Calculus

The handful of derivative identities every gradient in machine learning is assembled from, and the shape rule that catches the mistakes.

Overview

Why it needs its own name

Nothing here is new calculus. Differentiating x'Ax is the [product rule](the_chain_rule.html) and [partial derivatives](partial_derivatives_and_gradient.html) applied to a sum, and you could always expand into indices and grind it out.

What matrix calculus adds is notation that stays compact as the objects grow, plus a small collection of identities worth memorising so you never expand anything.

The demonstration on this page is deliberately one-dimensional. The calculus underneath is the same in any number of dimensions; it is only the shapes that change, and the shapes are the part you have to be careful about.

Matrix Calculus

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

The gradient of a scalar with respect to a matrix has the same shape as that matrix. That single rule catches most errors.
d(a'x)/dx = a and d(x'Ax)/dx = (A + A')x are the two identities almost everything else is built from.
For a linear layer y = Wx, the weight gradient is dL/dW = (dL/dy) x' — an outer product.
The demonstration is the scalar case, because the shapes are the only thing that changes and the calculus underneath does not.

Matrix Calculus

The short list of identities behind every gradient you will write, and the dimension check that makes them safe.

The shape rule

The single most useful fact, and the one that catches almost every mistake:

The gradient of a scalar with respect to something has the same shape as that something.

A loss is a scalar. Differentiate it by a 784×128 weight matrix and the result is 784×128. By a length-128 bias vector, and it is length 128.

This is why gradient descent can write W -= lr * dW at all: the update has to have the same shape as the thing it updates. It is also the fastest debugging tool available. If a hand-derived gradient comes out the wrong shape, the derivation is wrong, and you know before running anything.

Two conventions exist for laying out derivatives — numerator layout and denominator layout — and they differ by a transpose. Papers rarely say which they use. The shape rule resolves it every time: whichever orientation matches the parameter is the one meant.

The identities

Almost everything reduces to these:

ExpressionDerivative with respect to x
a'xa
x'aa
x'x2x
x'Ax(A + A')x, which is 2Ax when A is symmetric
Ax (Jacobian)A

And two more, with respect to a matrix:

ExpressionDerivative with respect to W
a'Wbab'
tr(W'A)A

The pattern in the first table is worth noticing: they are the matrix versions of d(ax)/dx = a and d(ax^2)/dx = 2ax. The (A + A') appears because both copies of x in x'Ax contribute, and when A is symmetric they contribute identically.

Working an example

Least squares, from the identities alone.

L = ||Ax - b||^2 = (Ax - b)'(Ax - b)
  = x'A'Ax - 2b'Ax + b'b

Differentiate term by term. The first is x'Mx with M = A'A, which is symmetric, giving 2A'Ax. The second is linear in x, giving -2A'b. The third has no x.

dL/dx = 2A'Ax - 2A'b

Set it to zero and you have the normal equations, A'Ax = A'b — the formula [the QR module](qr_decomposition.html) then explains why you should not solve directly.

The layer everyone needs

For a linear layer y = Wx + b with loss L:

dL/dW  =  (dL/dy) x'        an outer product
dL/db  =  dL/dy
dL/dx  =  W' (dL/dy)        passed back to the previous layer

Three lines, and they are the whole of backpropagation through a dense layer.

Check them against the shape rule. If dL/dy is m-long and x is n-long, the outer product is m×n — the shape of W. And W' is n×m, so W'(dL/dy) is n-long, matching x. Every term lands where it should, and if it does not, something is transposed.

The transpose in that last line is why the backward pass is sometimes described as running the network in reverse: the same weights, applied the other way round.

Where it goes wrong

Mixing layout conventions mid-derivation. Pick one, and use the shape rule to check.

Forgetting that x'Ax gives (A + A')x. The shortcut 2Ax is only valid for symmetric A.

Deriving without checking shapes. It costs seconds and catches most errors.

Trusting a hand derivative without a numerical check. Compare against a finite difference on a small random input. Every framework ships a gradient checker for this, and it is worth using when writing a custom operation.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What shape is the gradient of a scalar loss with respect to a 784x128 weight matrix?

  2. What is d(x'Ax)/dx?

  3. For a linear layer y = Wx, what is dL/dW?

Cheat sheet

Matrix Calculus

Nothing here is new calculus. Differentiating x'Ax is the [product rule](the_chain_rule.html) and [partial derivatives](partial_derivatives_and_gradient.html) applied to a sum, and you could always expand into indices and grind it out.

MATHS · vizlearn.in/maths/matrix_calculus.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.