CALCULUS / 5. CHAIN RULE & BACKPROP
The Chain Rule — Heart of Backpropagation
How gradients flow backward through composed functions
EXPLANATION
The chain rule is the single most important calculus concept for deep learning. Backpropagation is nothing but the chain rule applied to a computation graph. For a composition F(x) = f(g(x)): F'(x) = f'(g(x)) · g'(x) In Leibniz notation (dg "cancels"): dF/dx = (df/dg) · (dg/dx) For long compositions (deep networks): d/dx[f(g(h(u(v(x)))))] = (df/dg) · (dg/dh) · (dh/du) · (du/dv) · (dv/dx) Each layer contributes one term to the product. Multivariable chain rule (from Raschka's appendix): d/dx[f(g(x), h(x))] = (∂f/∂g)(dg/dx) + (∂f/∂h)(dh/dx) In vector form: ∇f · v'(x) Vanishing gradients: when many terms in this product are small (< 1), their product → 0. Gradients in early layers become tiny — the network stops learning. This is why: • Deep networks with sigmoid activations don't train • ReLU doesn't saturate → no vanishing gradient • Residual connections add a "gradient highway"
DIAGRAM
Forward pass (left to right):
x → [Layer 1: g] → h₁ → [Layer 2: f] → ŷ → Loss
Backward pass (right to left, chain rule):
dL/dx = (dL/dŷ) · (dŷ/dh₁) · (dh₁/dx)
──────── ───────── ─────────
output layer 2 layer 1
gradient gradient gradient
Each layer "passes back" its gradient multiplied by local gradient.
Example: 2-layer network
z₁ = W₁x + b₁
a₁ = ReLU(z₁)
z₂ = W₂a₁ + b₂
L = MSE(z₂, y)
dL/dW₁ = (dL/dz₂)(dz₂/da₁)(da₁/dz₁)(dz₁/dW₁)CODE