VISUAL LESSON · 2 PICTURES

Calculus Backprop

How does an error at the final output know which specific weight in the first layer caused the mistake?

Progress stays on this device · no login
BEFORE THE FIRST PICTURE

How this lesson unfolds.

When an AI makes an error on a prediction, that mistake occurs at the very end of a long chain of millions of calculations. Backpropagation is the algorithmic implementation of the calculus chain rule, propagating credit and blame backward through each layer to tell every individual weight exactly how much to adjust.

Picture 1 of 2 · tap to inspect in full resolution
01

How does an error at the final output know which specific weight in the first layer caused the mistake?

INTUITION

Imagine an assembly line where Worker A cuts metal rods, Worker B bends them into hooks, and Quality Control tests the hook strength. If a hook breaks under 5 pounds of pressure instead of 10, Quality Control doesn't just yell at the factory; they calculate: 'The hook failed by 5 units of strength (dL/dhook = 5). The hook angle depends on Worker B's bending tool (dhook/dbend = 0.5). Worker B's bend depends on Worker A's rod thickness (dbend/dthickness = 2.0).' By multiplying these sensitivity ratios together (5 × 0.5 × 2.0 = 5.0), Quality Control knows precisely how sensitive the final failure was to Worker A's original cut. In calculus, each derivative is a conversion rate; backprop multiplies these conversion rates backward from loss to weights.

✦ More context & mechanics
Conceptual Mechanics & Logic

Let an artificial neuron compute z = w^T x + b, followed by activation a = f(z), contributing to an objective loss L(a, y).

  1. Scalar Chain Rule Unrolling:

To determine the gradient of loss L with respect to weight wᵢ:

(∂ L) / (∂ wᵢ) = (∂ L) / (∂ a) · (∂ a) / (∂ z) · (∂ z) / (∂ wᵢ)

Evaluating each local derivative:

  • Output sensitivity: (∂ L) / (∂ a)
  • Activation local gradient: (∂ a) / (∂ z) = f'(z)
  • Linear local gradient: (∂ z) / (∂ wᵢ) = xᵢ

Combining terms and defining the incoming error signal δ = (∂ L) / (∂ z) = (∂ L) / (∂ a) f'(z):

(∂ L) / (∂ wᵢ) = δ · xᵢ, (∂ L) / (∂ b) = δ · 1, (∂ L) / (∂ xᵢ) = δ · wᵢ

  1. Concrete Little Numerical Example:

Let x = 2.0, w = 3.0, b = 1.0, with identity activation a = z, and target y = 5.0.

  • Forward pass: z = 3.0(2.0) + 1.0 = 7.0 ⟹ a = 7.0.
  • Loss: L = 1/2(a - y)² = 1/2(7.0 - 5.0)² = 2.0.
  • Backward pass: (∂ L) / (∂ a) = a - y = 2.0.
  • Local sensitivity: δ = (∂ L) / (∂ z) = 2.0 × 1 = 2.0.
  • Weight gradient: (∂ L) / (∂ w) = δ · x = 2.0 × 2.0 = 4.0.
  • Bias gradient: (∂ L) / (∂ b) = δ · 1 = 2.0.
  • Input gradient: (∂ L) / (∂ x) = δ · w = 2.0 × 3.0 = 6.0.
  1. Matrix Vectorized Formulation:

For a batched layer Z = X W^T + b, given downstream error Δ = (∂ L) / (∂ Z) ∈ R^B × d_(out):

(∂ L) / (∂ W) = Δ^T X ∈ R^d_(out) × d_(in), (∂ L) / (∂ X) = Δ W ∈ R^B × d_(in), (∂ L) / (∂ b) = ∑_(j=1)^B Δ_(j, :) ∈ R^d_(out)

Real-World Usage

Automatic differentiation engines (PyTorch's `torch.autograd`, JAX's `jax.grad`, and TensorFlow's `tf.GradientTape`) build dynamic Directed Acyclic Graphs (DAGs) during the forward pass and execute this exact reverse-mode vector-Jacobian product (VJP) during `.backward()`.

History & Origin Story

Reverse-mode automatic differentiation was introduced in Seppo Linnainmaa's 1970 master's thesis in Finland. Paul Werbos applied it to neural networks in his 1974 Harvard PhD thesis. In 1986, David Rumelhart, Geoffrey Hinton, and Ronald Williams published their landmark Nature paper demonstrating backprop's ability to discover internal hidden representations.

How to Memorize & Golden Words
  • Golden Axiom: "Forward calculates answers; backward multiplies local conversion rates to apportion blame."
  • The master gradient formula: (∂ L) / (∂ w) = δ · x_(in) (error incoming × input activation).
  • To fix a weight, look at the error it caused times how loudly the input was speaking.
Picture 2 of 2 · tap to inspect in full resolution
02

Why does repeated chain-rule multiplication across deep layers lead to exponential decay or explosive blowup of gradient signals?

INTUITION

Think of compound interest at a bank. If your savings grow by 5\% each year, after 50 years your money multiplies by (1.05)^(50) ≈ 11.5 times. But if a bank charges an inflation penalty of 5\% (0.95 factor), after 50 years your money shrivels to (0.95)^(50) ≈ 0.077 (a 92\% loss!). Now imagine a 50-layer deep neural network during backpropagation: if each layer scales gradients by 0.90, the signal reaching layer 1 is (0.90)^(50) ≈ 0.005 (vanished to zero!). If each scales by 1.20, the gradient becomes (1.20)^(50) ≈ 9100 (exploded to infinity!).

MATHEMATICAL STATEMENT
  1. $$Model deep gradient propagation as product prod W_l^T, demonstrating that singular values lambda != 1 cause exponential scaling lambda^L as depth L grows.$$
  • Learning goalDerive the exponential scaling of gradient norms across deep layer compositions and evaluate the stabilizing effect of residual connections.
  • Helpful foundationsMatrix eigenvalues and singular values · Deep Residual Learning for Image Recognition (He et al., 2015)
  • Common trapBelieving smaller learning rates fix vanishing gradients; if the gradient itself has vanished to 10^(-15), multiplying by a smaller learning rate makes weight updates even smaller, paralyzing learning.
  • Next connectionWhen cross-entropy loss meets softmax activation at the output layer, how does the backward pass avoid exploding into massive tensor products? We reveal the miracle cancellation.
✦ More context & mechanics
Conceptual Mechanics & Logic

In an L-layer linear network with weight matrices W₁, ṡ, W_L:

(∂ L) / (∂ h₀) = ( ∏_(l=1)^L W_l^T ) (∂ L) / (∂ h_L)

Let λ_l denote the spectral norm (largest singular value) of W_l. If weights are shared or identically distributed with average eigenvalue λ̄:

\| (∂ L) / (∂ h₀) \| ≈ λ̄^L \| (∂ L) / (∂ h_L) \|

Two catastrophic regimes emerge as depth L ≫ 1:

  1. Vanishing Gradients (λ̄ < 1):

lim_(L → ∞) λ̄^L = 0

Earlier layers receive zero gradient updates, freezing feature learning.

  1. Exploding Gradients (λ̄ > 1):

lim_(L → ∞) λ̄^L = ∞

Updates overflow IEEE float32 representations into `NaN`, destabilizing training.

Real-World Usage

This mathematical dilemma was solved in computer vision and transformers by Residual Connections (ResNets): h_(l+1) = h_l + F(h_l). Because (∂ h_(l+1)) / (∂ h_l) = I + (∂ F) / (∂ h_l), gradients have an uninterrupted identity highway where λ̄ ≈ 1.0.

History & Origin Story

Sepp Hochreiter thoroughly analyzed the vanishing gradient problem in his 1991 German diploma thesis, which motivated the invention of LSTMs (Hochreiter & Schmidhuber, 1997) and later ResNets (He et al., 2015).

How to Memorize & Golden Words
  • Numbers below 1 multiply to zero; numbers above 1 multiply to infinity.
  • Residual skip connections add +1.0 to save the gradient from dying.