Multivariable and matrix calculus
Jacobians, Hessians, the identities you need, and what autograd really computes
Models are built from vectors and matrices, so their derivatives are too. Jacobians generalize the derivative to vector-valued functions, Hessians measure curvature, and a handful of matrix-calculus identities let you derive the gradients of linear regression (3.2 Linear regression), logistic regression (3.3 Logistic regression and softmax classification) and softmax cross-entropy (4.3 Loss functions catalog) in a few lines instead of pages. You will also learn what autograd actually computes — vector-Jacobian products — which is why backpropagation (4.4 Backpropagation, fully derived) costs about as much as a forward pass no matter how many parameters a model has.
You'll be able to
- Write the Jacobian of a vector-valued function and compose Jacobians with the chain rule
- Use the second-order Taylor expansion and the Hessian to classify critical points and define convexity
- Apply the core matrix-calculus identities, including the backward rule for a linear layer
- Derive the gradients of least squares and of softmax cross-entropy, and explain vector-Jacobian products
Key ideas
- The Jacobian J (m×n, Jᵢⱼ = ∂fᵢ/∂xⱼ) is the matrix of a vector function's local linear approximation; chain rule = Jacobian product.
- Second-order Taylor: f(x+δ) ≈ f + ∇fᵀδ + ½δᵀHδ; Hessian eigenvalues are curvatures; H ≻ 0 minimum, mixed signs saddle, H ⪰ 0 everywhere ⇔ convex.
- Code convention: the gradient of a scalar loss has the same shape as the tensor it is taken with respect to.
Show 4 more key ideas
- Identities: ∇(aᵀx) = a; ∇(xᵀAx) = (A + Aᵀ)x; ∇‖Xw − y‖² = 2Xᵀ(Xw − y).
- Linear-layer backward rule: for Z = XW with upstream G, ∂L/∂W = XᵀG and ∂L/∂X = GWᵀ (shape trick recovers it).
- Softmax + cross-entropy has the gradient ∇_z L = p − y: prediction minus target.
- Autograd computes vector-Jacobian products right-to-left, never full Jacobians; backward cost ≈ forward cost, memory = stored activations.
Suggested path through this lesson
Jacobians: derivatives of vector-valued functions
For the Jacobian has entries and gives the local linear approximation
The chain rule becomes a matrix product: , which is why the matrix algebra of 1.2 Matrices as transformations matters here. Three Jacobians cover most of deep learning: a linear map has ; an element-wise function has ; and a layer therefore has . A scalar function is the case : its Jacobian is the row vector .
Hessians, Taylor expansion and curvature
Going one order further gives the quadratic model of a function near :
The Hessian is symmetric (mixed partials commute), so by the spectral theorem of 1.3 Eigen-things, SVD and low rank its eigenvalues are real and its eigenvectors orthogonal: each eigenvalue is the curvature of along its eigenvector. At a critical point (), means a local minimum, a maximum, and mixed signs a saddle — for , and the origin is a saddle. A function is convex exactly when everywhere; then every local minimum is global, which is why the classical models of Phase 3 are so well behaved. Minimizing the quadratic model gives Newton's step , which solves a quadratic in one move. With parameters the Hessian has entries, impossible at , so deep learning uses first-order methods with cheap curvature estimates such as Adam's second moments (1.10 Optimization: from gradient descent to Adam, 4.5 Optimizers and learning-rate schedules).
Intuition. The gradient tells you which way is down; the Hessian tells you how the slope changes — how far you can safely step before the ground curves back up.
Layout conventions: read this before any paper
There are two conventions for : numerator layout (the Jacobian above) and denominator layout (its transpose). Papers mix them, which is the source of most "my shapes are transposed" confusion. The convention every ML library uses in code is simpler and worth memorizing: the gradient of a scalar loss with respect to any tensor has the same shape as that tensor, so you can subtract it in a gradient step. This lesson writes Jacobians in numerator layout and gradients as same-shape tensors.
The identities you actually need
| expression | gradient or Jacobian | note |
|---|---|---|
| linear | ||
| , which is for symmetric | quadratic forms | |
| the case | ||
| Jacobian | linear map | |
| least squares | ||
| with upstream | , | one example |
| with upstream | , | a whole batch — the linear-layer backward rule |
The last two rows are backpropagation through a linear layer; you will use them in 4.4 Backpropagation, fully derived without thinking. A reliable way to recover them is the shape trick: must have the shape of (), and the only product of and with that shape is the outer product .
Derivation 1: the least-squares gradient
Expand . The first term is a quadratic form with the symmetric matrix (gradient ), the second is linear in with , the third is constant:
Setting it to zero gives the normal equations (3.2 Linear regression). The Hessian is , so the problem is convex and this critical point is the global minimum. Notice the pattern "residual pushed back through " — it is the batch backward rule from the table.
Derivation 2: softmax with cross-entropy
For logits , the softmax is and the cross-entropy loss against a one-hot target is . The softmax Jacobian is
where is 1 when and 0 otherwise. Then by the chain rule,
So : prediction minus target. The same form appears in logistic regression (3.3 Logistic regression and softmax classification), and it is why frameworks fuse softmax and cross-entropy into one numerically stable operation (4.3 Loss functions catalog).
Math check. For : . For : . Both match . ✓
Numbers: , . Then with sum , so , and : the gradient pushes the correct logit up and the others down, in proportion to how much probability they wrongly took.
Vector-Jacobian products: what autograd really computes
For a chain the gradient is
Reverse-mode differentiation evaluates this from the right: starting with the vector , each step is a vector-Jacobian product — a matrix-vector operation that never forms itself (which for a layer with a million inputs and outputs would have entries). The whole backward pass costs a small constant times the forward pass, independent of how many parameters you have, which is the economic miracle that makes deep learning possible. Its price is memory: every layer's activations must be kept until the backward pass reaches them (4.4 Backpropagation, fully derived, 4.12 Compute, hardware and scale basics). Forward mode computes Jacobian-vector products instead, cheap when inputs are few and outputs many — the opposite of a loss function, which is why ML libraries default to reverse mode.
Where this shows up later
- Normal equations and ridge regression — 3.2 Linear regression, 3.6 Regularization and generalization; logistic-regression gradient — 3.3 Logistic regression and softmax classification; loss functions on logits — 4.3 Loss functions catalog.
- Backpropagation through linear layers uses exactly the table above — 4.4 Backpropagation, fully derived; autograd in PyTorch — 2.4 PyTorch I: tensors, autograd and the training loop.
- Curvature, conditioning and Adam's curvature estimates — 1.10 Optimization: from gradient descent to Adam, 4.5 Optimizers and learning-rate schedules; memory cost of stored activations — 4.12 Compute, hardware and scale basics.
Common pitfalls (4)
- Writing ∇(xᵀAx) = 2Ax for a non-symmetric A; the correct form is (A + Aᵀ)x.
- Mixing numerator and denominator layouts within one derivation and ending with transposed shapes.
- Trying to materialize a Jacobian in code (torch.autograd.functional.jacobian on a big layer) when a vector-Jacobian product is all you need.
- Forgetting the Σᵢ yᵢ = 1 step in the softmax derivation and getting ∂L/∂zⱼ = pⱼ − yⱼ only for the target class.
Recap: all 7 key points
- The Jacobian J (m×n, Jᵢⱼ = ∂fᵢ/∂xⱼ) is the matrix of a vector function's local linear approximation; chain rule = Jacobian product.
- Second-order Taylor: f(x+δ) ≈ f + ∇fᵀδ + ½δᵀHδ; Hessian eigenvalues are curvatures; H ≻ 0 minimum, mixed signs saddle, H ⪰ 0 everywhere ⇔ convex.
- Code convention: the gradient of a scalar loss has the same shape as the tensor it is taken with respect to.
- Identities: ∇(aᵀx) = a; ∇(xᵀAx) = (A + Aᵀ)x; ∇‖Xw − y‖² = 2Xᵀ(Xw − y).
- Linear-layer backward rule: for Z = XW with upstream G, ∂L/∂W = XᵀG and ∂L/∂X = GWᵀ (shape trick recovers it).
- Softmax + cross-entropy has the gradient ∇_z L = p − y: prediction minus target.
- Autograd computes vector-Jacobian products right-to-left, never full Jacobians; backward cost ≈ forward cost, memory = stored activations.
Watch
Why a quadratic model captures curvature; the Hessian is this idea in many dimensions.
Read
Exercises
- By hand: two gradients and a Hessian
Compute of and solve for the minimizer. Then compute the Hessian of at and and classify the points. Expected: (ridge regression); has a zero Hessian (degenerate), is a saddle.
- PyTorch: verify the linear-layer backward rule
Create random
X(8×5),W(5×3) withrequires_grad=True, computeL = (X @ W).pow(2).sum()and callL.backward(). ComputeG = 2 * (X @ W)by hand and check thatW.gradequalsX.T @ GandX.gradequalsG @ W.T. Expected: both match to about . - NumPy: softmax cross-entropy gradient with a gradient check
Implement
softmax_ce(z, y)returning the loss and the gradient for a batch of logits (use the log-sum-exp trick: subtract the row max before exponentiating). Check it against the central-difference gradient from lesson 1.4 on random logits. Expected: relative error below ; your function should not overflow for logits of size 1000.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →