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

Scalar and multivariable chain ruleMatrix derivatives for affine, quadratic and least-squares objectives
1

Read

In this order, only the parts listed

0 of 5 done

  1. 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

  2. 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.

  3. 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

  4. 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

  5. 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

2

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.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive from . Check in float64 NumPy with : = 9.3e-12, a scaled residual of 0.093, consistent with an remainder.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 2.For , give and the cyclic step.Answer

    by , so under the Frobenius product.

  3. 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. 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 .

5

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

Notes

Resources · 2 for this unit, 4 more in the section

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.