Multivariable and Matrix Calculus
Gradients, Jacobians, Hessians and Taylor expansion, set up so that backpropagation and every smoothness assumption later are routine.
Optimisation theory is calculus with inequalities attached. The assumptions that appear in every convergence theorem later — -smoothness, -strong convexity, bounded gradients — are all statements about the objects defined here, and the descent lemma proved below is used in essentially every proof on the gradient descent page.
Derivatives as linear maps
is differentiable at if there is a linear map with
In coordinates is the Jacobian with . When we write the derivative as a column vector, the gradient , so that .
The derivative of at along is
the last equality holding when is differentiable at . A function can have all directional derivatives at a point without being differentiable there; at the origin is the standard example.
If is itself differentiable at , its Jacobian is the Hessian with .
If the second partial derivatives of exist and are continuous in a neighbourhood of , then is symmetric.
This is Clairaut–Schwarz; the proof is in any analysis text and is not reproduced here. The consequence we use constantly is that the spectral theorem applies to Hessians, so "curvature" is a list of real eigenvalues, and the second-order conditions for a minimum become eigenvalue conditions.
Taylor expansion and the descent lemma
Let be twice continuously differentiable. For all ,
In particular there is on the segment with .
is -smooth if is -Lipschitz: for all . For twice-differentiable this is equivalent to for all .
If is -smooth then for all ,
In particular .
Key idea
The descent lemma converts a global assumption about the gradient into a usable quadratic upper bound on the function. Every proof of a gradient-descent rate in this laboratory begins by minimising that upper bound, which is why the step size appears everywhere: it is the exact minimiser of .
The chain rule and reverse-mode differentiation
If is differentiable at and is differentiable at , then is differentiable at with
Consider a composition of maps, as in a feedforward network:
The chain rule gives with . Matrix multiplication is associative, so this product may be evaluated in any order — and the order is exactly the difference between forward and reverse mode.
- Forward pass. Compute and store .
- Seed. Set , the gradient of the scalar objective with respect to the output.
- Backward pass. For : set and accumulate the gradient with respect to the parameters of layer as .
- Return and the accumulated parameter gradients.
For a scalar-valued built from elementary operations, reverse mode computes the full gradient at a cost within a constant factor of one function evaluation, using memory proportional to the number of stored intermediates. Forward mode costs one function evaluation per input coordinate.
Matrix derivative identities
Throughout, , , , and all gradients are with respect to the indicated variable.
| Function | Derivative | Notes |
|---|---|---|
| if symmetric | ||
| normal equations set this to zero | ||
| undefined at | ||
| with respect to | ||
| as a differential | ||
| , | elementwise | |
| , | PSD, singular on |
The softmax Jacobian, since it is used in every LLM derivation
.
Definition.
Differentiate the quotient, separating the i = j and i ≠ j cases.
.
Collect into matrix form.
For any , , and the expression vanishes for .
The Jacobian is PSD and annihilates the all-ones vector, because shifting all logits by a constant does not change the distribution.
That last line is worth keeping: the softmax Jacobian is a variance under the predicted distribution. It is why gradients vanish when the model is confident — a confident has small variance — and it reappears as the Fisher information of the categorical family in the language modelling derivations.
Problems
Tightness of the descent lemmaProof · Beginner · 15 min The softmax Jacobian is a covarianceProof · Intermediate · 30 min Gradient and Hessian of log-determinantProof · Advanced · 30 min All directional derivatives exist, yet the function is not differentiableProof · Intermediate · 30 minReferences
- BookWalter Rudin (1976). Principles of Mathematical AnalysisChapter 9 for differentiation in several variables, including the statements quoted without proof here.
- BookJan R. Magnus and Heinz Neudecker (2019). Matrix Differential Calculus with Applications in Statistics and EconometricsThe systematic reference for matrix derivative identities.
- BookAndreas Griewank and Andrea Walther (2008). Evaluating Derivatives: Principles and Techniques of Algorithmic DifferentiationThe authoritative account of forward and reverse mode automatic differentiation.
These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.
Results stated on this page
- TheoremTaylor's theorem with integral remainderproof includedA twice-differentiable function equals its linear model plus an exact curvature term.
- LemmaDescent lemma for L-smooth functionsproof includedL-smoothness sandwiches a function between its linear model and that model plus a quadratic.
- TheoremChain rule for compositionsThe Jacobian of a composition is the product of the Jacobians, in reverse order of evaluation.
- TheoremSymmetry of the HessianContinuous second partials commute, so the Hessian is symmetric.