Unit 3 of 6 · about 4 hours · derive
The chain rule and matrix derivatives
Matrix backprop is the chain rule with shapes. Differentials keep the product order that index notation hides, and that order decides the flop count.
You'll be able to
- Differentiate composite scalar, vector and matrix functions with differentials and the operator chain rule.
- Derive and gradients of affine, quadratic and trace objectives with correct shapes.
- Predict Jacobian accumulation cost for a chain of maps and choose left-to-right versus right-to-left order.
Covers
Read
In this order, only the parts listed
0 of 5 done
- Operator chain rule and the inverse differentialMIT 18.S096 - Matrix Calculus for ML
Lecture 1, Part 2 notes (course notes Chapter 2, Sections 2.4-2.6): operator sum, product and chain rules, , , forward versus reverse Jacobian accumulation
- Jacobians of matrix functionsMIT 18.S096 - Matrix Calculus for ML
Lecture 2, Part 2 notes (course notes Chapter 3): the Jacobian of a matrix function via , the Jacobian of matrix squaring, the identity , and why forming Kronecker Jacobians costs (Section 3.3.3)
Read for the vec picture only; compute gradients with the Frobenius route in the next item.
- Gradients in general vector spacesMIT 18.S096 - Matrix Calculus for ML
Lecture 4, Part 1 notes (course notes Section 5.1): the Frobenius inner product , gradients of matrix-input functions read off , with the Frobenius-norm and (gradient ) examples
- Matrix gradients and gradient identitiesMathematics for Machine Learning - Ch. 5
Sections 5.2.2 and 5.4-5.5 (pp. 148-149, 155-158): chain rule as matrix multiplication, gradients of matrices, identities (5.99)-(5.108) covering transpose, trace, determinant, inverse, and
- Derivative of the determinant and inverseMIT 18.S096 - Matrix Calculus for ML
Lecture 5, Part 1 (course notes Chapter 7): derivative of the matrix determinant and inverse, including
Focus on
- keeps factor order. Matrices do not commute, so has three distinct terms.
- comes from differentiating the constant ; the final left-multiplication by needs to be invertible.
- Read gradients off differentials with the Frobenius product: means .
- For three or more composed maps, accumulate Jacobians from the small side. Scalar-output chains go left to right.
Common mistakes · 3
- Writing . By hand with and : so , but the product rule gives , which is nonzero.
- Forming a vec-Kronecker Jacobian to get a gradient. The differential plus the Frobenius product gives the gradient with no matrix.
- Quoting without invertibility. The formula needs invertible ; the det gradient shows the same requirement.
Try it yourself
Closed-book, before checking anything
float64 NumPy with : = 9.3e-12, a scaled residual of 0.093, consistent with an remainder.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Derive and name the step where invertibility is used.Answer
is constant, so by the product rule. Left-multiplying by needs invertible, giving .
2.For , give and the cyclic step.Answer
by , so under the Frobenius product.
3.Chain : which accumulation order, with flop counts?Answer
Left to right (reverse mode). Jacobians are , , : left-to-right costs multiplications, right-to-left costs . Reverse wins by about 17x.
4.Problem Set 1, Problem 2(b)(ii) computes a Jacobian in given . What is the structural reason?Answer
With , , so the Jacobian is the rank-1 outer product . Sherman-Morrison gives , per vector once is known, so , and their outer product each cost .
Practice
- MIT 18.S096 Problem Set 1, Problems 1 and 2 (operator and matrix derivatives)
Problem 1 (linear-operation derivatives including transpose and trace) and Problem 2 (gradients, Jacobians and linear operators, including the Sherman-Morrison argument). Official solutions exist; attempt before reading them
Write every answer as first, then convert to the requested Jacobian or gradient form.
You're done when
You derive and one trace or quadratic gradient closed-book with shapes stated, and a NumPy finite-difference check agrees to 1e-9 relative.
Only when you can do this without notes.
Leads to
- Gradients of logistic, softmax cross-entropy and regularized lossesLoss gradients apply these differentials to least squares, logistic and softmax objectives.
- Computation graphs and reverse accumulationThe scalar engine mechanizes this chain rule with topological backward; check it against centered differences.
Notes
Resources · 2 for this unit, 4 more in the section
- MIT 18.S096 - Matrix Calculus for MLMIT OpenCourseWare · primary
Use lectures 1-5, the Hessian portion of lecture 7 and computational-graph portion of lecture 8; do the supplied problems and notebooks.
- Mathematics for Machine Learning - Ch. 5Deisenroth, Faisal & Ong · reference
Use Chapter 5 as the compact derivative, Jacobian, chain-rule and backpropagation reference.
Also in this section
- Hands-On ML - Autodiff notebookAurelien Geron · required
Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.
- PyTorch Autograd mechanicsPyTorch documentation · required
Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.
- MIT 18.02SC - Multivariable Calculus Unit 2MIT OpenCourseWare · optional
Use sessions 24-38 only if partial derivatives, gradients, Hessians or the multivariable chain rule are weak.
- CS231n - Linear ClassificationStanford University · support
Read the Softmax classifier section for the cross-entropy loss formula with both the information-theory and MLE views, and Practical issues: Numeric stability for the max-subtraction trick. Derive the softmax gradient by hand; do not implement the course assignment here.