Skip to content
nonconvexcard

Multivariable and Matrix Calculus

Gradients, Jacobians, Hessians and Taylor expansion, set up so that backpropagation and every smoothness assumption later are routine.

FoundationsFoundations⏱ 3 hours13 min readgradientjacobianhessiantaylorchain rule
4 problems →

Optimisation theory is calculus with inequalities attached. The assumptions that appear in every convergence theorem later — LL-smoothness, μ\mu-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

DefinitionDifferentiability

f:Rd→Rmf : \R^d \to \R^m is differentiable at xx if there is a linear map Df(x):Rd→RmDf(x) : \R^d \to \R^m with

f(x+h)=f(x)+Df(x)h+o(∥h∥)as h→0.f(x+h) = f(x) + Df(x)h + o(\norm{h}) \quad \text{as } h \to 0 .

In coordinates Df(x)Df(x) is the Jacobian J∈Rm×dJ \in \R^{m \times d} with Jij=∂fi/∂xjJ_{ij} = \partial f_i/\partial x_j. When m=1m=1 we write the derivative as a column vector, the gradient ∇f(x)∈Rd\nabla f(x) \in \R^d, so that Df(x)h=⟨∇f(x),h⟩Df(x)h = \ip{\nabla f(x)}{h}.

DefinitionDirectional derivative

The derivative of ff at xx along vv is

Dvf(x)=lim⁡t↓0f(x+tv)−f(x)t=⟨∇f(x),v⟩,D_v f(x) = \lim_{t \downarrow 0} \frac{f(x+tv) - f(x)}{t} = \ip{\nabla f(x)}{v},

the last equality holding when ff is differentiable at xx. A function can have all directional derivatives at a point without being differentiable there; f(x1,x2)=x13/(x12+x22)f(x_1,x_2) = x_1^3/(x_1^2+x_2^2) at the origin is the standard example.

DefinitionHessian

If ∇f\nabla f is itself differentiable at xx, its Jacobian is the Hessian ∇2f(x)∈Rd×d\nabla^2 f(x) \in \R^{d\times d} with [∇2f(x)]ij=∂2f/∂xi∂xj[\nabla^2 f(x)]_{ij} = \partial^2 f/\partial x_i \partial x_j.

TheoremSymmetry of the Hessian

If the second partial derivatives of ff exist and are continuous in a neighbourhood of xx, then ∇2f(x)\nabla^2 f(x) 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

TheoremTaylor's theorem with integral remainder

Let f:Rd→Rf : \R^d \to \R be twice continuously differentiable. For all x,yx, y,

f(y)=f(x)+⟨∇f(x),y−x⟩+∫01(1−t) (y−x)⊤∇2f(x+t(y−x))(y−x) dt.f(y) = f(x) + \ip{\nabla f(x)}{y - x} + \int_0^1 (1-t)\,(y-x)^\top \nabla^2 f\big(x + t(y-x)\big)(y-x)\,dt .

In particular there is ξ\xi on the segment [x,y][x,y] with f(y)=f(x)+⟨∇f(x),y−x⟩+12(y−x)⊤∇2f(ξ)(y−x)f(y) = f(x) + \ip{\nabla f(x)}{y-x} + \tfrac12 (y-x)^\top\nabla^2 f(\xi)(y-x).

DefinitionL-smoothness

ff is LL-smooth if ∇f\nabla f is LL-Lipschitz: ∥∇f(x)−∇f(y)∥2≤L∥x−y∥2\norm{\nabla f(x) - \nabla f(y)}_2 \le L\norm{x-y}_2 for all x,yx,y. For twice-differentiable ff this is equivalent to −LI⪯∇2f(x)⪯LI-L I \preceq \nabla^2 f(x) \preceq L I for all xx.

LemmaDescent lemma

If ff is LL-smooth then for all x,yx,y,

∣f(y)−f(x)−⟨∇f(x),y−x⟩∣≤L2∥y−x∥22.\big| f(y) - f(x) - \ip{\nabla f(x)}{y-x} \big| \le \frac{L}{2}\norm{y-x}_2^2 .

In particular f(y)≤f(x)+⟨∇f(x),y−x⟩+L2∥y−x∥22f(y) \le f(x) + \ip{\nabla f(x)}{y-x} + \frac{L}{2}\norm{y-x}_2^2.

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 1/L1/L appears everywhere: it is the exact minimiser of t↦f(x)−t∥∇f(x)∥2+Lt22∥∇f(x)∥2t \mapsto f(x) - t\norm{\nabla f(x)}^2 + \frac{L t^2}{2}\norm{\nabla f(x)}^2.

The chain rule and reverse-mode differentiation

TheoremChain rule

If g:Rd→Rmg : \R^d \to \R^m is differentiable at xx and f:Rm→Rkf : \R^m \to \R^k is differentiable at g(x)g(x), then f∘gf \circ g is differentiable at xx with

D(f∘g)(x)=Df(g(x))⋅Dg(x).D(f\circ g)(x) = Df(g(x)) \cdot Dg(x) .

Consider a composition of nn maps, as in a feedforward network:

f=fn∘fn−1∘⋯∘f1,x0=x,xi=fi(xi−1).f = f_n \circ f_{n-1} \circ \dots \circ f_1, \qquad x_0 = x,\quad x_i = f_i(x_{i-1}) .

The chain rule gives Df(x)=JnJn−1⋯J1Df(x) = J_n J_{n-1}\cdots J_1 with Ji=Dfi(xi−1)J_i = Df_i(x_{i-1}). 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.

Algorithm — Reverse-mode differentiation (backpropagation)
  1. Forward pass. Compute and store x1,…,xnx_1, \dots, x_n.
  2. Seed. Set xˉn=∇xnL\bar{x}_n = \nabla_{x_n} \mathcal{L}, the gradient of the scalar objective with respect to the output.
  3. Backward pass. For i=n,n−1,…,1i = n, n-1, \dots, 1: set xˉi−1=Ji⊤xˉi\bar{x}_{i-1} = J_i^\top \bar{x}_i and accumulate the gradient with respect to the parameters of layer ii as (∂xi/∂θi)⊤xˉi(\partial x_i/\partial \theta_i)^\top \bar{x}_i.
  4. Return xˉ0\bar{x}_0 and the accumulated parameter gradients.
PropositionCost of reverse mode

For a scalar-valued ff built from nn 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, x∈Rdx \in \R^d, A∈Rd×dA \in \R^{d\times d}, X∈Rn×pX \in \R^{n \times p}, and all gradients are with respect to the indicated variable.

FunctionDerivativeNotes
a⊤xa^\top xaa
x⊤Axx^\top A x(A+A⊤)x(A + A^\top)x2Ax2Ax if AA symmetric
12∥Ax−b∥22\tfrac12\norm{Ax - b}_2^2A⊤(Ax−b)A^\top(Ax-b)normal equations set this to zero
∥x∥2\norm{x}_2x/∥x∥2x/\norm{x}_2undefined at x=0x=0
log⁡det⁡X\log\det XX−⊤X^{-\top}X≻0X \succ 0
tr⁡(AX)\tr(AX)A⊤A^\topwith respect to XX
tr⁡(X⊤AX)\tr(X^\top A X)(A+A⊤)X(A + A^\top)X
∥X∥F2\norm{X}_F^22X2X
X−1X^{-1}−X−1(dX)X−1-X^{-1}(dX)X^{-1}as a differential
σ(x)\sigma(x), σ(t)=11+e−t\sigma(t)=\tfrac{1}{1+e^{-t}}diag⁡ ⁣(σ(x)(1−σ(x)))\diag\!\big(\sigma(x)(1-\sigma(x))\big)elementwise
softmax⁡(z)\operatorname{softmax}(z)diag⁡(p)−pp⊤\diag(p) - pp^\top, p=softmax⁡(z)p = \operatorname{softmax}(z)PSD, singular on 1\one

The softmax Jacobian, since it is used in every LLM derivation

  1. pi=ezi/∑kezkp_i = e^{z_i}/\sum_k e^{z_k}.

    Definition.

  2. ∂pi∂zj=δijezi∑kezk−eziezj(∑kezk)2=δijpi−pipj.\frac{\partial p_i}{\partial z_j} = \frac{\delta_{ij}e^{z_i}\sum_k e^{z_k} - e^{z_i}e^{z_j}}{\left(\sum_k e^{z_k}\right)^2} = \delta_{ij}p_i - p_ip_j .

    Differentiate the quotient, separating the i = j and i ≠ j cases.

  3. ∂p∂z=diag⁡(p)−pp⊤\dfrac{\partial p}{\partial z} = \diag(p) - pp^\top.

    Collect into matrix form.

  4. For any vv, v⊤(diag⁡(p)−pp⊤)v=Ei∼p[vi2]−(Ei∼p[vi])2=Var⁡i∼p(vi)≥0v^\top(\diag(p) - pp^\top)v = \E_{i \sim p}[v_i^2] - (\E_{i\sim p}[v_i])^2 = \Var_{i\sim p}(v_i) \ge 0, and the expression vanishes for v=1v = \one.

    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 pp 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 min

References

  • 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

Problems (4)

All problems

Papers that use this mathematics