Math & Statistics

Chain Rule Behind Backprop

Understanding the multivariable calculus chain rule that enables reverse-mode automatic differentiation.

🟡 intermediate5 min readcalculusdeep-learning
Backpropagation (Backprop) is the algorithmic application of the multivariable calculus Chain Rule for computing exact loss gradients with respect to all trainable neural network weights. For composed functions y = f(g(x)), the chain rule multiplies local Jacobian matrices: dy/dx = (dy/dg) · (dg/dx). Reverse-Mode Automatic Differentiation traverses computation graphs backward from loss L, caching intermediate activation values to compute all N weight gradients in a single backward pass with O(N) compute complexity.

The Multivariable Chain Rule

Consider a simple computational graph:

x --► [ Layer 1: g(x) ] --► z --► [ Layer 2: f(z) ] --► Loss L

We want to find how changing input $x$ affects final Loss $L$:

dL/dx = (dL/dz) * (dz/dx)

For multivariable vector representations ($x \in \mathbb{R}^n, z \in \mathbb{R}^m$):

The chain rule multiplies Jacobian Matrices:

$$\frac{\partial L}{\partial x} = \frac{\partial L}{\partial z} \cdot J_{z, x}$$

  FORWARD PASS:   Inputs x ──► [ Layer 1 ] ──► z ──► [ Layer 2 ] ──► Loss L
                  (Compute & Cache Activations x, z in VRAM)

  BACKWARD PASS:  Inputs x ◄── [ dL/dx = dL/dz · dz/dx ] ◄── dL/dz ◄── Loss L = 1.0
                  (Propagate Loss Gradients Backward via Chain Rule!)

Why Reverse-Mode is 1,000,000x Faster

Suppose a neural network has $N = 100,000,000$ weight parameters and $1$ scalar loss output $L$.

  1. Forward-Mode Autodiff: Perturbs 1 input weight at a time. Requires $100,000,000$ separate forward passes to compute all weight gradients!
  2. Reverse-Mode Autodiff (Backprop): Starts at final scalar loss $L$ ($\frac{\partial L}{\partial L} = 1.0$) and sweeps backward once. Computes all $100,000,000$ weight gradients in a single backward pass!

Step-by-Step Backprop for a Linear Layer + Activation

Let $z = W x + b$, and $a = \sigma(z)$, and Loss $L = \text{MSE}(a, y)$.

Step 1: Upstream Gradient

Assume we receive upstream loss gradient $\frac{\partial L}{\partial a}$ from layer above.

Step 2: Local Activation Derivative

Pass gradient through non-linear activation $\sigma$:

$$\frac{\partial L}{\partial z} = \frac{\partial L}{\partial a} \odot \sigma'(z) \quad (\text{Hadamard Element-wise Product})$$

Step 3: Weight and Bias Gradients

Compute gradients for weights $W$ and bias $b$:

$$\frac{\partial L}{\partial W} = \frac{\partial L}{\partial z} \cdot x^T, \quad \frac{\partial L}{\partial b} = \frac{\partial L}{\partial z}$$

Step 4: Downstream Gradient

Pass gradient back to previous layer inputs $x$:

$$\frac{\partial L}{\partial x} = W^T \cdot \frac{\partial L}{\partial z}$$

This recursive 4-step sequence repeats through all layers of the neural network!

Say this out loud

Backpropagation applies the multivariable chain rule backward through a computational graph. Reverse-mode automatic differentiation computes exact gradients for all N weights in a single backward pass, making deep learning computationally feasible. Intermediate activation tensors computed during the forward pass must be cached in VRAM to evaluate local partial derivatives during the backward pass.

Follow-ups to expect

Check yourself

Question 1 of 3

Why is Reverse-Mode Automatic Differentiation (Backprop) computationally superior to Forward-Mode for deep neural networks with millions of parameters N and scalar loss L?

More in Math & Statistics

See all →
Bayes’ Theorem4 minCentral Limit Theorem4 minLaw of Large Numbers4 min