To train a network by gradient descent, you need the derivative of its loss with respect to every weight. Here is the smallest network that shows how, with one input $x$, one hidden ramp unit and one output: $$z = wx + b, \qquad h = \operatorname{ReLU}(z), \qquad \hat{y} = vh + c, \qquad L = (\hat{y} - y)^{2},$$ for one data point $(x, y)$. The loss depends on the weights $w$, $b$, $v$ and $c$ through a chain of simple steps, so the tool is the chain rule.
Key idea
Why it works
Worked examples
Worked example: All four derivatives
- Forward: $z = 1 \cdot 2 - 1 = 1$, $h = \operatorname{ReLU}(1) = 1$, $\hat{y} = 2 \cdot 1 + 0.5 = 2.5$, and $L = (2.5 - 3)^{2} = 0.25$.
- Start at the loss: $\frac{\partial L}{\partial \hat{y}} = 2(2.5 - 3) = -1$. So $\frac{\partial L}{\partial c} = -1$ and $\frac{\partial L}{\partial v} = -1 \cdot h = -1$.
- Into the hidden unit: $\frac{\partial L}{\partial h} = -1 \cdot v = -2$. The switch is on ($z = 1 > 0$), so $\frac{\partial L}{\partial z} = -2$.
- Into the first layer: $\frac{\partial L}{\partial w} = -2 \cdot x = -4$ and $\frac{\partial L}{\partial b} = -2$.
- All four are negative: the prediction $2.5$ is too low, and raising any weight a little would raise it.
Answer: $\frac{\partial L}{\partial w} = -4$, $\frac{\partial L}{\partial b} = -2$, $\frac{\partial L}{\partial v} = -1$, $\frac{\partial L}{\partial c} = -1$
Worked example: Check with a nudge
- Nudge $w$ up: $w = 1.001$ gives $z = 1.002$, $\hat{y} = 2.504$ and $L = (-0.496)^{2} = 0.246016$.
- Nudge it down: $w = 0.999$ gives $\hat{y} = 2.496$ and $L = 0.254016$.
- $\frac{0.246016 - 0.254016}{0.002} = -4$. ✓ This check costs two forward passes for each weight; backpropagation found all four derivatives in one backward pass.
Answer: The nudge agrees: $-4$.
Worked example: When the switch is off
- Forward: $z = 1 \cdot (-1) - 1 = -2$, so $h = 0$, $\hat{y} = 0.5$ and $L = (0.5 - 1)^{2} = 0.25$.
- $\frac{\partial L}{\partial \hat{y}} = -1$, so $\frac{\partial L}{\partial c} = -1$, but $\frac{\partial L}{\partial v} = -1 \cdot 0 = 0$.
- The switch is off: $\operatorname{ReLU}'(-2) = 0$, so $\frac{\partial L}{\partial z} = 0$ and $\frac{\partial L}{\partial w} = \frac{\partial L}{\partial b} = 0$.
- Small changes to $w$ and $b$ leave $z$ negative and the output unchanged, so this example cannot teach them anything. Only $c$ learns from it.
Answer: Only $\frac{\partial L}{\partial c} = -1$ is not $0$.
With more units the same rules run on vectors. For a layer $\mathbf{h} = \operatorname{ReLU}(W\mathbf{x} + \mathbf{b})$ the backward step is $$\frac{\partial L}{\partial \mathbf{z}} = \frac{\partial L}{\partial \mathbf{h}} \odot \operatorname{ReLU}'(\mathbf{z}), \qquad \frac{\partial L}{\partial W} = \frac{\partial L}{\partial \mathbf{z}}\,\mathbf{x}^{T}, \qquad \frac{\partial L}{\partial \mathbf{x}} = W^{T}\frac{\partial L}{\partial \mathbf{z}},$$ where $\odot$ multiplies entry by entry. The last formula hands the signal to the layer before, which repeats the same steps. With many data points, average the gradients from each one. The network that trains itself in Neural Network Layers works exactly this way.
Common mistake
Common mistake