Backpropagation

Volume II, Chapter 6 — Part I. The chain rule on computation graphs: forward and backward passes, general layer-wise gradient formulas, vector-Jacobian products, and the theoretical foundation of deep network training.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. Computation Graphs
  6. The Multivariate Chain Rule
  7. Forward Pass: Activations and Caching
  8. Backward Pass: Error Signals
  9. Layer-Wise Gradient Formulas
  10. The General Backpropagation Algorithm
  11. Vector-Jacobian Products
  12. Gradients for Common Operations
  13. Computational Complexity
  14. Worked Examples
  15. Connection to the Broader Curriculum
  16. Common Pitfalls and Misconceptions
  17. Research Perspective
  18. Summary of Takeaways
  19. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. Represent a neural network as a directed acyclic computation graph and identify intermediate variables.
  2. State and apply the multivariate chain rule for composed functions RnR\mathbb{R}^n \to \mathbb{R}.
  3. Derive the forward pass equations that cache activations for the backward pass.
  4. Derive the backward pass (error propagation) equations for a multi-layer network.
  5. Write the general formulas δl=(Wl+1)Tδl+1σ(zl)\delta^l = (W^{l+1})^T \delta^{l+1} \odot \sigma'(z^l) and L/Wl=δl(al1)T\partial \mathcal{L}/\partial W^l = \delta^l (a^{l-1})^T.
  6. Explain backpropagation as repeated vector-Jacobian products.
  7. Analyze the O(# parameters)O(\text{\# parameters}) computational cost of gradient computation via backpropagation.

Prerequisites

This chapter assumes familiarity with:


Notation

  • f(l)f^{(l)} — Activation at layer ll
  • W(l),b(l)\mathbf{W}^{(l)}, \mathbf{b}^{(l)} — Weight matrix and bias at layer ll
  • δ(l)=L/f(l)\delta^{(l)} = \partial \mathcal{L}/\partial f^{(l)} — Error signal (adjoint variable)
  • L\mathcal{L} — Scalar loss
  • \odot — Element-wise (Hadamard) product

Core Intuition

Training a neural network requires computing L/θ\partial \mathcal{L}/\partial \theta for every parameter θ\theta — potentially billions of them. Backpropagation is an efficient algorithm for this, discovered independently by Werbos (1974), Parker (1985), and popularized by Rumelhart, Hinton, and Williams (1986).

The core idea is elementary: a neural network is a composition of functions. The gradient of a composition is computed by the chain rule, applied systematically from the loss backward to the inputs. Backpropagation is not a new mathematical principle — it is the chain rule organized to reuse intermediate computations.

The efficiency comes from dynamic programming: each intermediate gradient is computed once and reused for all parameters downstream. Without this sharing, gradient computation would be exponentially expensive in network depth.

Series context. This opens Chapter 6 (Deep Learning Foundations) in Volume II. It connects Gradient Descent to all subsequent deep learning training — Batch Normalization, Self-Attention, and beyond.

Backpropagation Flow

x0.50∂=0.004h₁0.62∂=-0.008h₂0.39∂=-0.013h₃0.57∂=0.015ŷ0.52∂=-0.090Forward →← Backward (chain rule)
Input x
0.50
Forward activationsEdge gradient magnitude
Insight: Loss gradient flows backward via the chain rule: ∂L/∂w = ∂L/∂ŷ · ∂ŷ/∂h · ∂h/∂w. Thicker, redder edges carry larger gradients — notice how sigmoid saturation shrinks them.

Computation Graphs

Definition 1 (Computation Graph). A computation graph is a directed acyclic graph (DAG) where:

  • Leaf nodes are inputs (data x\mathbf{x}, parameters W,b\mathbf{W}, \mathbf{b})
  • Interior nodes are intermediate values computed from their parents
  • Root node is the scalar loss L\mathcal{L}

Each edge represents a functional dependency: if node vv depends on node uu, then v/u\partial v / \partial u is well-defined.

Definition 2 (Neural Network as Composition). An LL-layer network defines a composition:

L=fLfL1f1,(1)\mathcal{L} = \ell \circ f_L \circ f_{L-1} \circ \cdots \circ f_1, \tag{1}

where fl(al1)=σ(Wlal1+bl)f_l(\mathbf{a}^{l-1}) = \sigma(W^l \mathbf{a}^{l-1} + \mathbf{b}^l) and \ell is the loss function.

Proposition 1. Any differentiable computation — matrix multiplications, activations, normalization, attention — can be represented as a computation graph. Backpropagation applies to the entire graph.


The Multivariate Chain Rule

Theorem 1 (Multivariate Chain Rule). Let f:RmRnf: \mathbb{R}^m \to \mathbb{R}^n and g:RnRg: \mathbb{R}^n \to \mathbb{R} be differentiable. Then h=gf:RmRh = g \circ f: \mathbb{R}^m \to \mathbb{R} is differentiable and

xh=(fx)Tyg,y=f(x),(2)\nabla_{\mathbf{x}} h = \left(\frac{\partial f}{\partial \mathbf{x}}\right)^T \nabla_{\mathbf{y}} g, \quad \mathbf{y} = f(\mathbf{x}), \tag{2}

where f/xRn×m\partial f / \partial \mathbf{x} \in \mathbb{R}^{n \times m} is the Jacobian of ff.

Proof. For each component, h/xj=i=1n(g/yi)(yi/xj)\partial h / \partial x_j = \sum_{i=1}^{n} (\partial g / \partial y_i)(\partial y_i / \partial x_j). Stacking: xh=JfTyg\nabla_{\mathbf{x}} h = \mathbf{J}_f^T \nabla_{\mathbf{y}} g. \blacksquare

Definition 3 (Error Signal / Adjoint). Define the error signal at node v\mathbf{v}:

vˉ=Lv.(3)\bar{\mathbf{v}} = \frac{\partial \mathcal{L}}{\partial \mathbf{v}}. \tag{3}

Backpropagation computes vˉ\bar{\mathbf{v}} for every node by propagating from the loss backward.


Forward Pass: Activations and Caching

Definition 4 (Layer Notation). For layer l=1,,Ll = 1, \ldots, L:

zl=Wlal1+bl,al=σ(zl),(4)\mathbf{z}^l = W^l \mathbf{a}^{l-1} + \mathbf{b}^l, \quad \mathbf{a}^l = \sigma(\mathbf{z}^l), \tag{4}

with a0=x\mathbf{a}^0 = \mathbf{x} (input) and y^=aL\hat{\mathbf{y}} = \mathbf{a}^L (output).

Algorithm (Forward Pass). For l=1,,Ll = 1, \ldots, L:

  1. Compute pre-activation: zl=Wlal1+bl\mathbf{z}^l = W^l \mathbf{a}^{l-1} + \mathbf{b}^l

  2. Compute activation: al=σ(zl)\mathbf{a}^l = \sigma(\mathbf{z}^l)

  3. Cache zl\mathbf{z}^l and al1\mathbf{a}^{l-1} for the backward pass

  4. Compute loss: L=(aL,y)\mathcal{L} = \ell(\mathbf{a}^L, \mathbf{y})

All cached values are needed for gradient computation — memory cost is O(Lwidth)O(L \cdot \text{width}).


Backward Pass: Error Signals

We derive gradients for a two-layer network, then generalize.

Setup. Layer 2 (output): z2=W2a1+b2\mathbf{z}^2 = W^2 \mathbf{a}^1 + \mathbf{b}^2, y^=σ(z2)\hat{\mathbf{y}} = \sigma(\mathbf{z}^2). Layer 1 (hidden): z1=W1x+b1\mathbf{z}^1 = W^1 \mathbf{x} + \mathbf{b}^1, a1=σ(z1)\mathbf{a}^1 = \sigma(\mathbf{z}^1).

Definition 5 (Output Error). Define δl=L/zl\delta^l = \partial \mathcal{L} / \partial \mathbf{z}^l (error at pre-activation of layer ll).

Step 1: Output layer error. For cross-entropy with sigmoid output and binary labels:

δ2=Lz2=y^y.(5)\delta^2 = \frac{\partial \mathcal{L}}{\partial \mathbf{z}^2} = \hat{\mathbf{y}} - \mathbf{y}. \tag{5}

Proof. L=ylogσ(z)(1y)log(1σ(z))\mathcal{L} = -y \log \sigma(z) - (1-y)\log(1-\sigma(z)). Using σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z)):

Lz=yσ(z)+1y1σ(z)=σ(z)yσ(z)(1σ(z)).(6)\frac{\partial \mathcal{L}}{\partial z} = -\frac{y}{\sigma(z)} + \frac{1-y}{1-\sigma(z)} = \frac{\sigma(z) - y}{\sigma(z)(1-\sigma(z))}. \tag{6}

Multiplying by σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z)) gives σ(z)y=y^y\sigma(z) - y = \hat{y} - y. \blacksquare

Step 2: Parameter gradients at layer 2.

LW2=δ2(a1)T,Lb2=δ2.(7)\frac{\partial \mathcal{L}}{\partial W^2} = \delta^2 (\mathbf{a}^1)^T, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{b}^2} = \delta^2. \tag{7}

Proof. Since z2=W2a1+b2\mathbf{z}^2 = W^2 \mathbf{a}^1 + \mathbf{b}^2, we have zi2/Wij2=aj1\partial z_i^2 / \partial W_{ij}^2 = a_j^1 and L/Wij2=δi2aj1\partial \mathcal{L}/\partial W_{ij}^2 = \delta_i^2 a_j^1. Matrix form: L/W2=δ2(a1)T\partial \mathcal{L}/\partial W^2 = \delta^2 (\mathbf{a}^1)^T. \blacksquare

Step 3: Propagate error to layer 1.

δ1=(W2)Tδ2σ(z1),(8)\delta^1 = (W^2)^T \delta^2 \odot \sigma'(\mathbf{z}^1), \tag{8}

where \odot denotes element-wise (Hadamard) product.

Proof. By the chain rule: L/zj1=i(L/zi2)(zi2/aj1)(aj1/zj1)=iδi2Wij2σ(zj1)\partial \mathcal{L}/\partial z_j^1 = \sum_i (\partial \mathcal{L}/\partial z_i^2)(\partial z_i^2 / \partial a_j^1)(\partial a_j^1 / \partial z_j^1) = \sum_i \delta_i^2 W_{ij}^2 \cdot \sigma'(z_j^1). Vector form: δ1=(W2)Tδ2σ(z1)\delta^1 = (W^2)^T \delta^2 \odot \sigma'(\mathbf{z}^1). \blacksquare

Step 4: Parameter gradients at layer 1.

LW1=δ1xT,Lb1=δ1.(9)\frac{\partial \mathcal{L}}{\partial W^1} = \delta^1 \mathbf{x}^T, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{b}^1} = \delta^1. \tag{9}

Layer-Wise Gradient Formulas

Theorem 2 (General Backpropagation Equations). For an LL-layer network with loss L\mathcal{L}, the error signals satisfy:

δL=zLL=y^σ(zL),(10)\delta^L = \nabla_{\mathbf{z}^L} \mathcal{L} = \frac{\partial \ell}{\partial \hat{\mathbf{y}}} \odot \sigma'(\mathbf{z}^L), \tag{10} δl=(Wl+1)Tδl+1σ(zl),l=L1,,1,(11)\delta^l = (W^{l+1})^T \delta^{l+1} \odot \sigma'(\mathbf{z}^l), \quad l = L-1, \ldots, 1, \tag{11}

and the parameter gradients are:

LWl=δl(al1)T,Lbl=δl.(12)\frac{\partial \mathcal{L}}{\partial W^l} = \delta^l (\mathbf{a}^{l-1})^T, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{b}^l} = \delta^l. \tag{12}

Proof. By induction on ll. The base case l=Ll = L follows from the chain rule through the loss. For the inductive step, assume δl+1=L/zl+1\delta^{l+1} = \partial \mathcal{L}/\partial \mathbf{z}^{l+1}. Since zl+1=Wl+1σ(zl)+bl+1\mathbf{z}^{l+1} = W^{l+1}\sigma(\mathbf{z}^l) + \mathbf{b}^{l+1}:

Lzjl=iδil+1Wijl+1σ(zjl)=[(Wl+1)Tδl+1]jσ(zjl).(13)\frac{\partial \mathcal{L}}{\partial z_j^l} = \sum_i \delta_i^{l+1} W_{ij}^{l+1} \cdot \sigma'(z_j^l) = \left[(W^{l+1})^T \delta^{l+1}\right]_j \cdot \sigma'(z_j^l). \tag{13}

This gives (11). For (12): L/Wl=δl(al1)T\partial \mathcal{L}/\partial W^l = \delta^l (\mathbf{a}^{l-1})^T follows from zil/Wijl=ajl1\partial z_i^l / \partial W_{ij}^l = a_j^{l-1}. \blacksquare

Important equation. Equations (11)–(12) are the workhorse of deep learning. Modern automatic differentiation systems implement these recurrences as reverse-mode adjoint passes over the computation graph.


The General Backpropagation Algorithm

Algorithm (Backpropagation).

Input: Network with parameters {Wl,bl}l=1L\{W^l, \mathbf{b}^l\}_{l=1}^L, input x\mathbf{x}, target y\mathbf{y}.

  1. Forward pass: Compute and cache zl,al\mathbf{z}^l, \mathbf{a}^l for all ll.
  2. Output error: δL=y^σ(zL)\delta^L = \nabla_{\hat{\mathbf{y}}} \ell \odot \sigma'(\mathbf{z}^L).
  3. Backward pass: For l=L1,,1l = L-1, \ldots, 1:
    • δl=(Wl+1)Tδl+1σ(zl)\delta^l = (W^{l+1})^T \delta^{l+1} \odot \sigma'(\mathbf{z}^l)
  4. Gradients: For l=1,,Ll = 1, \ldots, L:
    • WlL=δl(al1)T\nabla_{W^l} \mathcal{L} = \delta^l (\mathbf{a}^{l-1})^T
    • blL=δl\nabla_{\mathbf{b}^l} \mathcal{L} = \delta^l
  5. Update: θθαθL\theta \leftarrow \theta - \alpha \nabla_\theta \mathcal{L} (gradient descent step)

Vector-Jacobian Products

Definition 6 (VJP). The vector-Jacobian product of f:RmRnf: \mathbb{R}^m \to \mathbb{R}^n at x\mathbf{x} with cotangent vector vRn\mathbf{v} \in \mathbb{R}^n is:

vTJf(x)Rm,(14)\mathbf{v}^T \mathbf{J}_f(\mathbf{x}) \in \mathbb{R}^m, \tag{14}

where Jf=f/x\mathbf{J}_f = \partial f / \partial \mathbf{x}.

Proposition 2. Backpropagation computes VJPs in reverse topological order. Each node receives a cotangent vˉ\bar{\mathbf{v}} and passes JfTvˉ\mathbf{J}_f^T \bar{\mathbf{v}} to its parents.

This abstraction generalizes backpropagation beyond feedforward networks to arbitrary DAGs (residual connections, attention, branching).

Theorem 3 (Automatic Differentiation). Reverse-mode automatic differentiation (backpropagation) computes the full gradient θLRp\nabla_{\boldsymbol{\theta}} \mathcal{L} \in \mathbb{R}^p for pp parameters in O(cost of forward pass)O(\text{cost of forward pass}) operations, regardless of pp.

Forward-mode AD computes one directional derivative in O(forward pass)O(\text{forward pass}) but requires pp passes for the full gradient — impractical for neural networks where p1p \gg 1.


Gradients for Common Operations

Proposition 3 (Linear Layer). For z=Wa+b\mathbf{z} = W\mathbf{a} + \mathbf{b} with upstream gradient zˉ=L/z\bar{\mathbf{z}} = \partial \mathcal{L}/\partial \mathbf{z}:

LW=zˉaT,Lb=zˉ,La=WTzˉ.(15)\frac{\partial \mathcal{L}}{\partial W} = \bar{\mathbf{z}}\, \mathbf{a}^T, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{b}} = \bar{\mathbf{z}}, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{a}} = W^T \bar{\mathbf{z}}. \tag{15}

Proposition 4 (Element-wise Activation). For a=σ(z)\mathbf{a} = \sigma(\mathbf{z}):

Lz=Laσ(z).(16)\frac{\partial \mathcal{L}}{\partial \mathbf{z}} = \frac{\partial \mathcal{L}}{\partial \mathbf{a}} \odot \sigma'(\mathbf{z}). \tag{16}

Proposition 5 (Softmax + Cross-Entropy). For KK-class softmax output with cross-entropy loss:

Lz=p^y,(17)\frac{\partial \mathcal{L}}{\partial \mathbf{z}} = \hat{\mathbf{p}} - \mathbf{y}, \tag{17}

where p^=softmax(z)\hat{\mathbf{p}} = \text{softmax}(\mathbf{z}) and y\mathbf{y} is the one-hot target — the same clean form as (5).

Proposition 6 (ReLU). For a=max(0,z)a = \max(0, z): L/z=L/a1[z>0]\partial \mathcal{L}/\partial z = \partial \mathcal{L}/\partial a \cdot \mathbb{1}[z > 0].


Computational Complexity

Theorem 4 (Backprop Cost). For a network with WW weights and forward pass cost CfwdC_{\text{fwd}}, backpropagation computes all gradients in O(Cfwd)O(C_{\text{fwd}}) — typically O(W)O(W).

Proof sketch. Each edge in the computation graph is traversed once in the forward pass and once in the backward pass. The total work is at most twice the forward cost. \blacksquare

Comparison. Numerical gradient via finite differences requires p+1p + 1 forward passes for pp parameters — O(pCfwd)O(p \cdot C_{\text{fwd}}), infeasible for p109p \sim 10^9.


Worked Examples

Example 1: Scalar Two-Layer Network

Let xRx \in \mathbb{R}, W1,W2,b1,b2RW^1, W^2, b^1, b^2 \in \mathbb{R}, σ=ReLU\sigma = \text{ReLU}. Forward: z1=W1x+b1z^1 = W^1 x + b^1, a1=max(0,z1)a^1 = \max(0, z^1), z2=W2a1+b2z^2 = W^2 a^1 + b^2, y^=z2\hat{y} = z^2 (linear output), L=12(y^y)2\mathcal{L} = \frac{1}{2}(\hat{y} - y)^2.

Backward: δ2=y^y\delta^2 = \hat{y} - y, L/W2=δ2a1\partial \mathcal{L}/\partial W^2 = \delta^2 a^1, δ1=W2δ21[z1>0]\delta^1 = W^2 \delta^2 \cdot \mathbb{1}[z^1 > 0], L/W1=δ1x\partial \mathcal{L}/\partial W^1 = \delta^1 x.

Example 2: Batch Processing

For a mini-batch XRB×d\mathbf{X} \in \mathbb{R}^{B \times d}, gradients accumulate over batch dimension: L/Wl=1Bb=1Bδbl(abl1)T\partial \mathcal{L}/\partial W^l = \frac{1}{B}\sum_{b=1}^{B} \delta_b^l (\mathbf{a}_b^{l-1})^T.

Example 3: Depth and Vanishing Gradients

For σ=sigmoid\sigma = \text{sigmoid}, σ(z)1/4|\sigma'(z)| \leq 1/4. Through LL layers: δ1(1/4)LδL0\|\delta^1\| \leq (1/4)^L \|\delta^L\| \to 0 as LL \to \infty. This motivates ReLU, residual connections, and careful initialization.


Connection to the Broader Curriculum

Backpropagation is the computational engine for:

Gradient Descent provides the optimizer; backpropagation provides the gradients.


Common Pitfalls and Misconceptions

Pitfall 1: Thinking backpropagation is separate from the chain rule. It is the chain rule, organized efficiently. There is no additional mathematical content.

Pitfall 2: Forgetting to cache forward pass values. The backward pass requires al1\mathbf{a}^{l-1} and zl\mathbf{z}^l from the forward pass. Recomputing them doubles cost.

Pitfall 3: Confusing L/al\partial \mathcal{L}/\partial \mathbf{a}^l with δl=L/zl\delta^l = \partial \mathcal{L}/\partial \mathbf{z}^l. The error signal δl\delta^l is at the pre-activation. The activation gradient requires multiplying by σ(zl)\sigma'(\mathbf{z}^l).

Pitfall 4: Numerical gradient checking on large networks. Finite-difference checking is O(p)O(p) — use only on tiny subnetworks for debugging.

Pitfall 5: Ignoring vanishing/exploding gradients. Deep networks require architectural mitigations (residual connections, normalization, gated activations) beyond naive backpropagation.


Research Perspective

The modern history of backpropagation begins with Werbos's 1974 PhD thesis, was independently rediscovered by Parker (1985), and was popularized by Rumelhart, Hinton, and Williams (1986), who demonstrated learning of internal representations.

Automatic differentiation was formalized by Wengert (1964) and developed into modern AD theory (Griewank & Walther, 2008). Reverse-mode AD computes the full gradient of a scalar loss in time proportional to a single forward pass — the computational foundation of deep learning optimization.

Open research directions include:

  • Gradient-free optimization for non-differentiable objectives (evolution strategies, REINFORCE)
  • Second-order methods using Hessian information (K-FAC, natural gradient)
  • Memory-efficient backprop (gradient checkpointing, reversible networks)
  • Understanding gradient dynamics in overparameterized networks (neural tangent kernel regime)

Backpropagation remains the dominant training algorithm four decades after its popularization — a testament to the chain rule's enduring power.


Summary of Takeaways

  • Error signalδl=L/zl\delta^l = \partial \mathcal{L}/\partial \mathbf{z}^l — Upstream gradient at layer ll
  • Backprop recurrenceδl=(Wl+1)Tδl+1σ(zl)\delta^l = (W^{l+1})^T \delta^{l+1} \odot \sigma'(\mathbf{z}^l) — Propagate errors backward
  • Weight gradientL/Wl=δl(al1)T\partial \mathcal{L}/\partial W^l = \delta^l (\mathbf{a}^{l-1})^T — Outer product of error and input
  • Output layer (CE+sigmoid)δL=y^y\delta^L = \hat{\mathbf{y}} - \mathbf{y} — Clean gradient form
  • VJPvTJf\mathbf{v}^T \mathbf{J}_f — General backprop abstraction
  • ComplexityO(Cfwd)O(C_{\text{fwd}}) — Same order as one forward pass

Next article: Universal Approximation Theorem → — why depth and width give neural networks their expressive power.


Exercises

Exercise 1 (Chain rule). Derive (8) explicitly for a scalar two-layer network without matrix notation.

Exercise 2 (Softmax gradient). Prove (17) by differentiating categorical cross-entropy through softmax.

Exercise 3 (Generalization). Extend Theorem 2 to a network with skip connections: al=σ(zl)+al1\mathbf{a}^l = \sigma(\mathbf{z}^l) + \mathbf{a}^{l-1}. How does the error propagate?

Exercise 4 (VJP). Express the full backpropagation algorithm for a DAG (not just a chain) using VJP notation.

Exercise 5 (Complexity). Count the number of multiply-add operations for forward and backward passes of a single fully connected layer mapping RnRm\mathbb{R}^n \to \mathbb{R}^m.

Exercise 6 (Vanishing gradients). For σ=tanh\sigma = \tanh, bound σ(z)|\sigma'(z)| and derive the worst-case gradient decay through LL layers.

Exercise 7 (Batch). Show that batch gradient 1BbLb\frac{1}{B}\sum_b \nabla \mathcal{L}_b equals the gradient of the average loss L=1BbLb\mathcal{L} = \frac{1}{B}\sum_b \mathcal{L}_b.

Exercise 8 (Conceptual). Why is reverse-mode AD preferred over forward-mode for neural network training? Give a quantitative argument using typical values of pp (parameters) and output dimension.