What is matrix calculus?
Matrix calculus is the notation and rules for differentiating functions whose inputs or outputs are vectors or matrices. A scalar function of a vector has a gradient. A vector-valued function has a Jacobian, the matrix of every output's partial derivative with respect to every input. The rules let you write these derivatives in a few symbols instead of one partial at a time.
Deep learning is written in vectors and matrices, so its derivatives are too. A layer y = Wx + b has a Jacobian with respect to x and a gradient with respect to W, and backpropagation multiplies these Jacobians layer by layer. Matrix calculus also appears in least squares, Gaussian likelihoods, change-of-variables formulas for normalizing flows, and second-order optimizers.
Key ideas
- The Jacobian of f: ℝⁿ → ℝᵐ is an m×n matrix of partial derivatives.
- For a scalar function the Jacobian is a single row, the transpose of the gradient.
- A linear map x ↦ Ax has Jacobian A everywhere.
- |det J| is the local factor by which a map scales area or volume.
- The chain rule for vector functions multiplies Jacobians: J_{f∘g} = J_f · J_g.
- Backpropagation computes vector-Jacobian products instead of full Jacobian matrices.
Common questions
What are numerator layout and denominator layout?
They are two conventions for arranging the same partial derivatives. Numerator layout makes ∂y/∂x an m×n matrix with one row per output; denominator layout uses the transpose. Textbooks and papers mix them, so many apparent sign or transpose errors are layout mismatches. Checking that matrix shapes line up is the quickest test.
What is the gradient of a loss with respect to a weight matrix?
For a layer y = Wx with upstream gradient g = ∂L/∂y, the gradient is ∂L/∂W = g·xᵀ, an outer product with the same shape as W. Over a batch these outer products are summed, which is why weight gradients are computed as one matrix multiplication of the upstream gradients with the layer inputs.
How is the Hessian related to the Jacobian?
The Hessian of a scalar function is the Jacobian of its gradient. For a model with n parameters it has n² entries, so it is almost never formed in deep learning. Methods that need curvature use Hessian-vector products instead, which autodiff computes without building the matrix.
When do you need the full Jacobian?
Rarely in training, since a scalar loss needs only vector-Jacobian products. Full Jacobians appear in normalizing flows (for the determinant), in robustness analysis, and in some physics and control models. Libraries such as JAX provide jacfwd and jacrev, which build them column by column or row by row.
Related explainers
- Partial Derivatives & Gradients — Extend single-variable derivatives to functions of multiple variables and gradient vectors
- The Chain Rule — How derivatives compose through layers
- Linear Algebra — Vectors, matrices, and the language of data
- Backpropagation — How networks learn from their mistakes
- Eigenvalues & Eigenvectors — How matrices transform space — eigenvectors, eigenvalues, and decomposition
- Vanishing & Exploding Gradients — Why deep networks struggle and how to fix it
References
- Petersen & Pedersen, "The Matrix Cookbook"
- Parr & Howard (2018), "The Matrix Calculus You Need For Deep Learning"
- Deisenroth, Faisal & Ong, "Mathematics for Machine Learning", ch. 5 Vector Calculus
- Dinh, Sohl-Dickstein & Bengio (2016), "Density estimation using Real NVP"
Last updated