Calculus and Automatic Differentiation

Dive into Deep Learning · §34

Calculus and Automatic Differentiation: From Local Models to Backpropagation
Taylor approximations · descent lemma · gradients & Hessians · computation graphs · matrix calculus · autodiff · expectation gradients

Two Core Questions Behind Every Training Step

Motivation

  • 1. How far can a local slope be trusted? The gradient \nabla\ell(\mathbf{w}) is a first-order linear model; how rapidly curvature bends the loss away determines which learning rates \eta are safe.
  • 2. Why is the gradient of 10^8 weights so cheap? Perturbing 10^8 weights one by one would take ~12 days, whereas one backward pass returns all 10^8 exact partial derivatives in ~30 ms.

Three Pillars Carrying Deep Learning Calculus

Unifying Principles

  • Pillar 1: The Descent Lemma (§§34.1–34.2): Taylor’s Lagrange remainder converts a curvature bound |f''| \le L into safe step sizes 0 < \eta < 2/L (\eta^\star = 1/L).
  • Pillar 2: Reverse-Mode Jacobian Products (§§34.3–34.5): Associating ((\mathbf{u}^\top\mathbf{J}_K)\mathbf{J}_{K-1})\cdots\mathbf{J}_1 from the output backward computes all gradients in O(1) sweeps.
  • Pillar 3: Differentiating Expectations (§§34.6–34.7): Score-function (REINFORCE) vs. pathwise reparameterization trade generality against Monte Carlo variance.

Chapter 34 Roadmap: From 1D Slices to Sampling

Architectural Progression

  • Part I: Local Models (§§34.1–34.2): Taylor polynomials, Lagrange remainders, descent lemma, gradients \nabla f \perp level sets, and symmetric Hessians \mathbf{H}_f.
  • Part II: Chain Rule & Autodiff (§§34.3–34.5): Path sums on DAGs, differential method df = \langle \nabla f, d\mathbf{X}\rangle, dual numbers, Wengert tape, 2\sqrt{T} checkpointing, HVPs, and implicit diff.
  • Part III: Integrals & Expectations (§§34.6–34.7): Riemann sums, FTC, Fubini, |\det\mathbf{J}_{\boldsymbol{\phi}}| in flows, Leibniz rule, REINFORCE, and pathwise gradients.

Running Benchmarks Anchoring Each Section

Concrete Pedagogical Thread

  • Local & Matrix Models (§§34.1–34.4): Logistic loss \ell(w) = \log(1+e^{-w}) (L = 1/4), log-sum-exp at (0, \log 2) (\nabla f = (\tfrac{1}{3}, \tfrac{2}{3})^\top), 4-input integer DAG (\bar{w} = 73{,}472), and 2-2-1 ReLU MLP.
  • Autodiff & Stochastic Calculus (§§34.5–34.7): From-scratch Dual/Var engines (802 vs. 80{,}200 ops), polar Gaussian integral \sqrt{2\pi}, and \mathbb{E}[X^2] variance (\sigma = 2.0 vs. 7.18).

How to Navigate Chapter 34

Study Guide

  • If you know introductory calculus: Skim the differentiation table in §34.1 and the Riemann sum / antiderivative review in §34.6, and jump directly to the Mean Value Theorem, Lagrange remainder, and descent lemma (§34.1) and change of variables in \mathbb{R}^n (§34.6).
  • Essential for deep learning theory & derivations: Master the differential method df = \langle \nabla f, d\mathbf{X}\rangle and trace move rule in §34.4—it replaces hundred-entry matrix derivative tables and prevents silent transpose bugs on square matrices.
  • Essential for systems & modern ML: Study §34.5 (Automatic Differentiation) for why reverse mode costs ~3 forward passes, how activation checkpointing shrinks memory from O(T) to 2\sqrt{T}, how forward-over-reverse computes \mathbf{H}\mathbf{v} without a 4 TB Hessian, and §34.7 for Monte Carlo gradient variance.

Up next (§34.1 Single-Variable Calculus): How first and second derivatives build local linear and quadratic models, bound approximation error, and certify safe step sizes for gradient descent.