Matrix Calculus

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

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.

Overview

Start with a scalar loss

Let x be a column vector and A a fixed matrix. For f(x) = ½xᵀAx, the gradient is ½(A + Aᵀ)x. If A is symmetric, this simplifies to Ax. The symmetry condition matters; writing Ax without it is a common error.

∇ₓ(½xᵀAx) = ½(A + Aᵀ)x

For A = I, f(x) = ½‖x‖² and ∇f = x. A gradient descent step is x ← x − ηx, where η is the learning rate. The gradient has the same shape as x, which is a useful implementation check.

In this guide
  1. Shapes and conventions
  2. Check a derivative by expanding the scalar
  3. Try this above
  4. Sources and next steps

Matrix Calculus

Matrix calculus keeps track of both derivative values and their shapes when functions take vectors and matrices.

Shapes and conventions

A scalar differentiated with respect to a vector gives a vector; a vector-valued function differentiated with respect to a vector gives a Jacobian matrix. Authors differ on whether gradients are written as rows or columns, so read the convention and verify dimensions before multiplying. The Jacobian and Hessian lesson defines these arrays; the chain rule explains how they compose.

In a neural network, matrix calculus expresses how a scalar loss changes through matrix multiplications and activation functions. Derivatives must flow through the transpose implied by the chosen vector convention; shape checking catches many mistakes, but it cannot replace checking the mathematical definition.

Check a derivative by expanding the scalar

With x=(x₁,…,xₙ) as a column vector, f(x)=½xᵀAx is a scalar. Expanding the quadratic shows that only the symmetric part of A contributes: xᵀAx=xᵀ[(A+Aᵀ)/2]x. Differentiating gives ∇f=½(A+Aᵀ)x. If A is symmetric, this is Ax; if it is not, writing Ax is generally wrong. For A=I, f=½Σxᵢ², so each partial derivative is xᵢ and the gradient is x.

Shapes are a practical correctness check. A scalar differentiated with respect to an n-vector yields n first derivatives; a vector-valued function differentiated with respect to an n-vector yields a Jacobian. Some books write the gradient as a row and others as a column, so transpose conventions affect formulas even when the underlying derivative is the same. For a vector loss in code, check whether the library returns a gradient of the parameter shape. Verify a formula by differentiating a small two-variable example or finite differences, especially when a transpose appears. See Jacobians and Hessians for the higher-order objects.

Try this above

Change the vector and matrix entries and compare the displayed derivative with the symmetric and nonsymmetric cases.

For f(x)=½‖Ax−b‖², the residual is r=Ax−b and the gradient is Aᵀr=Aᵀ(Ax−b). The transpose appears because a small input change Δx produces output change AΔx, and the scalar loss change is rᵀAΔx=(Aᵀr)ᵀΔx. This derivation is a shape-safe way to recover the gradient. It also shows why least squares leads to the normal equations at a stationary point. Verify dimensions at each multiplication rather than memorising a formula without its vector convention.

Sources and next steps

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

Work through the matrix derivative identities used in backpropagation. Use dimension analysis to verify each gradient before coding it.

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.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.