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
d(a'x)/dx = a and d(x'Ax)/dx = (A + A')x are the two identities almost everything else is built from.y = Wx, the weight gradient is dL/dW = (dL/dy) x' — an outer product.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.
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.
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.
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.