Lesson 4.4core4 h⚡ live demo

Backpropagation, fully derived

The chain rule on a graph, organized so every gradient costs one backward pass

Backpropagation is the algorithm that makes deep learning possible: it computes the gradient of a scalar loss with respect to millions of parameters for roughly twice the cost of one forward pass. Deriving it once by hand, for a real softmax classifier, removes the magic from autograd, lets you debug shapes and NaNs with confidence (4.8 Training dynamics and debugging), and is the basis of every cost estimate in the field — the famous "6N FLOPs per token" of 7.9 Scaling laws and compute-optimal training is a statement about backprop.

You'll be able to

  • Build a computational graph for an expression and apply the chain rule node by node, forward and backward
  • Derive the gradients of a 2-layer network with softmax + cross-entropy with respect to every weight and bias, with shapes checked
  • Explain vector–Jacobian products and why reverse-mode differentiation is the right choice for scalar losses
  • Estimate the compute (≈ 2× forward) and memory (stored activations) cost of backprop
  • Implement a micrograd-style scalar autograd engine and verify it with gradient checking

Key ideas

  1. Backprop computes ∇L for all parameters in about 2× the cost of a forward pass, independent of the number of parameters.
  2. On a computational graph, each node multiplies the upstream gradient by its local derivative; gradients arriving along different paths are summed.
  3. For softmax + cross-entropy the output error is δ⁽ᴸ⁾ = p − y; for each layer δ⁽ˡ⁾ = (W⁽ˡ⁺¹⁾ᵀ δ⁽ˡ⁺¹⁾) ⊙ φ'(z⁽ˡ⁾).
Show 4 more key ideas
  1. Weight gradients are outer products δ⁽ˡ⁾ h⁽ˡ⁻¹⁾ᵀ (batched: Δᵀ H / B); bias gradients are δ⁽ˡ⁾ summed over the batch.
  2. Reverse mode chains vector–Jacobian products from the loss backward, never forming Jacobians; it is optimal for scalar losses.
  3. Backward needs the stored forward activations, so memory scales with batch × depth; checkpointing recomputes them to save memory.
  4. Always verify a hand-written backward with centered finite differences in float64 (relative error < 1e-7).

Suggested path through this lesson