7 routes42 sections324 units

Unit 2.3 · about 4 h

61 / 324

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.

  • derive

You can now

  • Differentiate composite scalar, vector and matrix functions with differentials and the operator chain rule.
  • Derive \(d(X^{-1}) = -X^{-1} dX X^{-1}\) 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 rule
  • Matrix derivatives for affine, quadratic and least-squares objectives

Study, in this order

5 steps

  1. Operator chain rule and the inverse differential
    MIT 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, \(d(A^3) = A^2 dA + A dA A + dA A^2\), \(d(A^{-1}) = -A^{-1} dA A^{-1}\), forward versus reverse Jacobian accumulation

  2. Jacobians of matrix functions
    MIT 18.S096 - Matrix Calculus for ML

    Lecture 2, Part 2 notes (course notes Chapter 3): the Jacobian of a matrix function via \(\operatorname{vec}\), the \(4 \times 4\) Jacobian of matrix squaring, the identity \((A \otimes B) \operatorname{vec}(C) = \operatorname{vec}(BCA^\top)\), and why forming Kronecker Jacobians costs \(m^4\) (Section 3.3.3)

    Depth · Read for the vec picture only; compute gradients with the Frobenius route in the next item.

  3. Gradients in general vector spaces
    MIT 18.S096 - Matrix Calculus for ML

    Lecture 4, Part 1 notes (course notes Section 5.1): the Frobenius inner product \(\operatorname{tr}(A^\top B)\), gradients of matrix-input functions read off \(df = \operatorname{tr}(G^\top dA)\), with the Frobenius-norm and \(x^\top Ay\) (gradient \(xy^\top\)) examples

  4. Matrix gradients and gradient identities
    Mathematics 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, \(a^\top Xb\) and \(x^\top Bx\)

  5. Derivative of the determinant and inverse
    MIT 18.S096 - Matrix Calculus for ML

    Lecture 5, Part 1 (course notes Chapter 7): derivative of the matrix determinant and inverse, including \(d \log \det X = \operatorname{tr}(X^{-1} dX)\)

Focus on

  • \(d(fg) = (df)g + f(dg)\) keeps factor order. Matrices do not commute, so \(d(A^3)\) has three distinct terms.
  • \(d(X^{-1})\) comes from differentiating the constant \(XX^{-1} = I\); the final left-multiplication by \(X^{-1}\) needs \(X\) to be invertible.
  • Read gradients off differentials with the Frobenius product: \(df = \operatorname{tr}(G^\top dX)\) means \(\operatorname{grad} f = G\).
  • For three or more composed maps, accumulate Jacobians from the small side. Scalar-output chains go left to right.

Reproduce closed-book

Closed-book, derive \(d(X^{-1}) = -X^{-1} dX X^{-1}\) from \(d(XX^{-1}) = 0\). Check in float64 NumPy with \(X = \left[\begin{smallmatrix}3&1\\1&2\end{smallmatrix}\right], V = \left[\begin{smallmatrix}0.5&-0.25\\0.25&0.5\end{smallmatrix}\right], h = 10^{-5}\): \(\|(X + hV)^{-1} - (X^{-1} - h X^{-1} V X^{-1})\|_F\) = 9.3e-12, a scaled residual of 0.093, consistent with an \(O(h^2)\) remainder.

Traps

  • Writing \(d(A^3) = 3 A^2 dA\). By hand with \(A = \left[\begin{smallmatrix}0&1\\0&0\end{smallmatrix}\right]\) and \(dA = \left[\begin{smallmatrix}0&0\\1&0\end{smallmatrix}\right]\): \(A^2 = 0\) so \(3 A^2 dA = 0\), but the product rule gives \(A dA A = A\), which is nonzero.
  • Forming a vec-Kronecker Jacobian to get a gradient. The differential plus the Frobenius product gives the gradient with no \(n^2 \times n^2\) matrix.
  • Quoting \(d \log \det X\) without invertibility. The formula needs invertible \(X\); the det gradient \(\det(X)(X^{-1})^\top\) shows the same requirement.

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 \(\Theta(n^2)\) Sherman-Morrison argument). Official solutions exist; attempt before reading them

Write every answer as \(f'(x)[dx]\) first, then convert to the requested Jacobian or gradient form.

Self-test

  1. Derive \(d(X^{-1})\) and name the step where invertibility is used.

  2. For \(f(X) = \operatorname{tr}(AXB)\), give \(\operatorname{grad} f\) and the cyclic step.

  3. Chain \(\mathbb{R}^{100} \to \mathbb{R}^{50} \to \mathbb{R}^{20} \to \mathbb{R}^1\): which accumulation order, with flop counts?

  4. Problem Set 1, Problem 2(b)(ii) computes a Jacobian in \(\Theta(n^2)\) given \(A^{-1}\). What is the structural reason?

Completion criterion

Stop when

You derive \(d(X^{-1})\) and one trace or quadratic gradient closed-book with shapes stated, and a NumPy finite-difference check agrees to 1e-9 relative.

Mark passed, then open 2.4 Only mark this when you can do it without notes.

Leads to

Resources for this unit

Also in this section

  • Hands-On ML - Autodiff notebook
    Aurelien Geronrequiredimplementation
    Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.
  • PyTorch Autograd mechanics
    PyTorch documentationrequiredreference
    Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.
  • MIT 18.02SC - Multivariable Calculus Unit 2
    MIT OpenCourseWareoptionalrefresher
    Use sessions 24-38 only if partial derivatives, gradients, Hessians or the multivariable chain rule are weak.
  • CS231n - Linear Classification
    Stanford Universitysupportreference
    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.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels