AI / ML Theory
Chapter 2 · 22 min

2.6 — Backprop, part 2: the recursion δ⁽ℓ⁾ = (W⁽ℓ⁺¹⁾)ᵀ δ⁽ℓ⁺¹⁾ ⊙ g'(z⁽ℓ⁾)

Goal: Derive the recursion that propagates the delta variable backwards through the network.

1. Why this session exists

Once we have δ(L)=y^−y\delta^{(L)} = \hat y - y at the output (2.5), we need a way to push that gradient signal backwards — to δ(L−1)\delta^{(L-1)}, then δ(L−2)\delta^{(L-2)}, all the way to δ(1)\delta^{(1)}. The chain rule from 0.3 gives us one recursion, with two factors at each step: a matrix multiply by W(ℓ+1)W^{(\ell+1)} and an elementwise multiply by g′(ℓ)(z(ℓ))g'^{(\ell)}(z^{(\ell)}). This session derives it.


2. Setup

Recall the forward pass at consecutive layers:

z(ℓ)=W(ℓ)a(ℓ−1)+b(ℓ),a(ℓ)=g(ℓ)(z(ℓ)),z(ℓ+1)=W(ℓ+1)a(ℓ)+b(ℓ+1).\begin{aligned} z^{(\ell)} &= W^{(\ell)} a^{(\ell-1)} + b^{(\ell)}, \\ a^{(\ell)} &= g^{(\ell)}(z^{(\ell)}), \\ z^{(\ell+1)} &= W^{(\ell+1)} a^{(\ell)} + b^{(\ell+1)}. \end{aligned}

The dependency graph at this step is

z(ℓ)  →g(ℓ)  a(ℓ)  →W(ℓ+1), + b(ℓ+1)  z(ℓ+1).z^{(\ell)} \;\xrightarrow{g^{(\ell)}}\; a^{(\ell)} \;\xrightarrow{W^{(\ell+1)},\, +\, b^{(\ell+1)}}\; z^{(\ell+1)}.

The loss LL depends on z(ℓ)z^{(\ell)} only through z(ℓ+1)z^{(\ell+1)} (via a(ℓ)a^{(\ell)}). By the chain rule,

∂L∂z(ℓ)  =  ∂L∂z(ℓ+1)⋅∂z(ℓ+1)∂a(ℓ)⋅∂a(ℓ)∂z(ℓ).\frac{\partial L}{\partial z^{(\ell)}} \;=\; \frac{\partial L}{\partial z^{(\ell+1)}} \cdot \frac{\partial z^{(\ell+1)}}{\partial a^{(\ell)}} \cdot \frac{\partial a^{(\ell)}}{\partial z^{(\ell)}}.

Each Jacobian factor is easy.


3. Each factor in turn

∂z(ℓ+1)/∂a(ℓ)=W(ℓ+1)\partial z^{(\ell+1)} / \partial a^{(\ell)} = W^{(\ell+1)}. Direct from the affine forward z(ℓ+1)=W(ℓ+1)a(ℓ)+b(ℓ+1)z^{(\ell+1)} = W^{(\ell+1)} a^{(\ell)} + b^{(\ell+1)}. In the numerator convention this Jacobian is the dℓ+1×dℓd_{\ell+1} \times d_\ell matrix W(ℓ+1)W^{(\ell+1)} itself.

∂a(ℓ)/∂z(ℓ)=diag⁡(g′(ℓ)(z(ℓ)))\partial a^{(\ell)} / \partial z^{(\ell)} = \operatorname{diag}\bigl(g'^{(\ell)}(z^{(\ell)})\bigr). The activation g(ℓ)g^{(\ell)} is applied elementwise, so each ai(ℓ)a^{(\ell)}_i depends only on zi(ℓ)z^{(\ell)}_i. The Jacobian is diagonal with ii-th entry g′(zi(ℓ))g'(z^{(\ell)}_i).

∂L/∂z(ℓ+1)=(δ(ℓ+1))⊤\partial L / \partial z^{(\ell+1)} = (\delta^{(\ell+1)})^\top by definition of the next-layer delta — written as a row in the numerator convention.


4. Putting it together

Substituting,

(δ(ℓ))⊤  =  (δ(ℓ+1))⊤⋅W(ℓ+1)⋅diag⁡ ⁣(g′(ℓ)(z(ℓ))).\bigl(\delta^{(\ell)}\bigr)^\top \;=\; (\delta^{(\ell+1)})^\top \cdot W^{(\ell+1)} \cdot \operatorname{diag}\!\bigl(g'^{(\ell)}(z^{(\ell)})\bigr).

Transpose both sides to get a column:

δ(ℓ)  =  diag⁡ ⁣(g′(ℓ)(z(ℓ)))⋅(W(ℓ+1))⊤⋅δ(ℓ+1).\delta^{(\ell)} \;=\; \operatorname{diag}\!\bigl(g'^{(\ell)}(z^{(\ell)})\bigr) \cdot (W^{(\ell+1)})^\top \cdot \delta^{(\ell+1)}.

Multiplying a vector by diag⁡(g′(z))\operatorname{diag}(g'(z)) is elementwise multiplication by g′(z)g'(z). So

 δ(ℓ)  =  ((W(ℓ+1))⊤ δ(ℓ+1)) ⊙ g′(ℓ)(z(ℓ))  ∈  Rdℓ. \boxed{\ \delta^{(\ell)} \;=\; \Bigl((W^{(\ell+1)})^\top\, \delta^{(\ell+1)}\Bigr) \,\odot\, g'^{(\ell)}\bigl(z^{(\ell)}\bigr) \;\in\; \mathbb{R}^{d_\ell}.\ }

This is the backprop recursion. Two factors per step:

  1. Matrix-multiply step. Multiply δ(ℓ+1)∈Rdℓ+1\delta^{(\ell+1)} \in \mathbb{R}^{d_{\ell+1}} by (W(ℓ+1))⊤∈Rdℓ×dℓ+1(W^{(\ell+1)})^\top \in \mathbb{R}^{d_\ell \times d_{\ell+1}}. This "spreads" the gradient signal across layer-ℓ\ell units in proportion to how strongly each contributed to layer ℓ+1\ell+1. Cost Θ(dℓ dℓ+1)\Theta(d_\ell\, d_{\ell+1}) multiply-adds.
  2. Activation step. Elementwise multiply by g′(ℓ)(z(ℓ))∈Rdℓg'^{(\ell)}(z^{(\ell)}) \in \mathbb{R}^{d_\ell}. This "scales" the gradient by how responsive each unit was at its current operating point. Cost Θ(dℓ)\Theta(d_\ell) multiplies.

Total cost per backward step: dominated by the matmul — same order as the forward.


5. Sanity check: dimensions

  • W(ℓ+1)W^{(\ell+1)} is dℓ+1×dℓd_{\ell+1} \times d_\ell, so (W(ℓ+1))⊤(W^{(\ell+1)})^\top is dℓ×dℓ+1d_\ell \times d_{\ell+1}.
  • δ(ℓ+1)\delta^{(\ell+1)} is dℓ+1d_{\ell+1}, so (W(ℓ+1))⊤δ(ℓ+1)(W^{(\ell+1)})^\top \delta^{(\ell+1)} is dℓd_\ell ✓.
  • g′(ℓ)(z(ℓ))g'^{(\ell)}(z^{(\ell)}) is dℓd_\ell, so the Hadamard product is dℓd_\ell ✓.
  • δ(ℓ)\delta^{(\ell)} is dℓd_\ell ✓.

6. Batched form

Define Δ(ℓ)∈RB×dℓ\Delta^{(\ell)} \in \mathbb{R}^{B \times d_\ell} (rows = examples). The recursion becomes

 Δ(ℓ)  =  (Δ(ℓ+1) W(ℓ+1)) ⊙ g′(ℓ) ⁣(Z(ℓ))  ∈  RB×dℓ. \boxed{\ \Delta^{(\ell)} \;=\; \bigl(\Delta^{(\ell+1)}\, W^{(\ell+1)}\bigr) \,\odot\, g'^{(\ell)}\!\bigl(Z^{(\ell)}\bigr) \;\in\; \mathbb{R}^{B \times d_\ell}.\ }

Worked dimensions:

  • Δ(ℓ+1)\Delta^{(\ell+1)} is B×dℓ+1B \times d_{\ell+1}.
  • W(ℓ+1)W^{(\ell+1)} is dℓ+1×dℓd_{\ell+1} \times d_\ell.
  • Δ(ℓ+1)W(ℓ+1)\Delta^{(\ell+1)} W^{(\ell+1)} is B×dℓB \times d_\ell ✓.
  • g′(Z(ℓ))g'(Z^{(\ell)}) is B×dℓB \times d_\ell ✓.
  • Hadamard product is B×dℓB \times d_\ell ✓.

The single-example form has (W(ℓ+1))⊤δ(W^{(\ell+1)})^\top \delta, but the batched row-major form has Δ W(ℓ+1)\Delta\, W^{(\ell+1)} without a transpose — this is the row-vs-column distinction. Keep one convention and stick to it.


7. Why this is efficient

The forward pass at layer ℓ\ell does one matrix-multiply AW⊤A W^\top of cost Θ(B dℓ dℓ−1)\Theta(B\, d_\ell\, d_{\ell-1}).

The backward pass at layer ℓ\ell does one matrix-multiply ΔW\Delta W of cost Θ(B dℓ dℓ+1)\Theta(B\, d_\ell\, d_{\ell+1}) plus one elementwise op of cost Θ(B dℓ)\Theta(B\, d_\ell).

Backward = forward in cost. This is the fundamental reason gradient descent on deep nets is practical: the price of one gradient computation is one extra forward-pass-equivalent of FLOPs. Forward + backward + parameter update ≈ 3× one forward pass. Without the trick — say, computing ∂L/∂θ\partial L / \partial \theta via NN finite-difference forward passes — we'd be paying N×N \times forward. For N∼109N \sim 10^9 parameters, that is the difference between hours and millennia.


8. Takeaways

  • δ(ℓ)=((W(ℓ+1))⊤δ(ℓ+1))⊙g′(ℓ)(z(ℓ))\delta^{(\ell)} = ((W^{(\ell+1)})^\top \delta^{(\ell+1)}) \odot g'^{(\ell)}(z^{(\ell)}) — the central recursion.
  • Batched: Δ(ℓ)=(Δ(ℓ+1)W(ℓ+1))⊙g′(Z(ℓ))\Delta^{(\ell)} = (\Delta^{(\ell+1)} W^{(\ell+1)}) \odot g'(Z^{(\ell)}).
  • Cost per layer = one matmul, same scale as the forward pass.
  • This is why deep learning is computationally feasible.

Self-test

(a) Derive the recursion line by line from the chain rule on z(ℓ)→a(ℓ)→z(ℓ+1)z^{(\ell)} \to a^{(\ell)} \to z^{(\ell+1)}. Check each Jacobian shape.

(b) For an MLP with widths (d0,d1,d2,d3)=(10,64,64,3)(d_0, d_1, d_2, d_3) = (10, 64, 64, 3) and ReLU activations, compute δ(2)\delta^{(2)} given δ(3)\delta^{(3)}, W(3)W^{(3)}, and z(2)z^{(2)}. Write out shapes at every step.

(c) Explain in words why g′(z(ℓ))g'(z^{(\ell)}) being small (sigmoid saturation, say) attenuates δ(ℓ)\delta^{(\ell)} and what that means for the trainability of deep sigmoid networks. (Foreshadow of 2.9.)

(d) Suppose layer ℓ+1\ell + 1 uses the identity activation. Show that g′(z(ℓ))g'(z^{(\ell)}) still appears, but g′(ℓ+1)(z(ℓ+1))=1g'^{(\ell+1)}(z^{(\ell+1)}) = 1 disappears one step down the chain. What does this say about the role of nonlinearities in shaping gradient flow?