ContentsChapter 8 · Neural networks2 of 3

8.2

Backpropagation

In this chapter
  1. 8.1From logistic regression to an MLP
  2. 8.2Backpropagation
  3. 8.3Training
Before you readA hidden neuron feeds two outputs. If raising one output increases the loss and raising the other decreases it, can you tell whether to raise the hidden neuron’s value?
Watch: What is backpropagation really doing? · 3Blue1Brown · Neural networks, ch. 3

The forward pass computes a prediction and a loss. Backpropagation goes the other way: for each intermediate value, it finds how much the loss would change if that value increased a little. Over one edge, multiply the gradient coming back by the edge’s local derivative. Where several paths lead to the loss, add their contributions.

Take x=2x=2 z=wxz=wx and h=ReLU⁡(z)h=\operatorname{ReLU}(z) Let hh feed two outputs u=2hu=2h and v=−hv=-h with targets 00 and 11 Their squared-error loss is L=12[u2+(v−1)2]L=\tfrac12[u^2+(v-1)^2] The two output gradients are ∂L/∂u=u\partial L/\partial u=u and ∂L/∂v=v−1\partial L/\partial v=v-1 so the gradient at hh is

∂L∂h=u⋅2+(v−1)⋅(−1).\frac{\partial L}{\partial h} =u\cdot 2+(v-1)\cdot(-1).

At w=1w=1 h=2h=2 The output gradients 44 and −3-3 have opposite signs, but their contributions at hh are 88 and 33 increasing hh moves both outputs away from their targets. Follow the arrows back from the loss to see where the sum is formed.

∂L/∂h=8+3=11\partial L/\partial h=8+3=11∂L/∂w=22\partial L/\partial w=22
Read right to left. The two paths meet at h, where their gradients add. Move w below zero to see the ReLU block the gradient.
Recall
When a value affects the loss along two paths, which derivatives do you multiply and which do you add?

To get the gradient for ww continue backward:

∂L∂w=∂L∂h ReLU⁡′(z) x.\frac{\partial L}{\partial w} =\frac{\partial L}{\partial h}\, \operatorname{ReLU}'(z)\,x.

For z>0z>0 the ReLU passes the gradient back unchanged; for z<0z<0 it blocks it. At z=0z=0 the derivative is undefined; implementations typically use zero there. Backprop computes each intermediate gradient once and reuses it for all values that feed into it. It computes gradients; it doesn’t update weights.

Recall
What does a ReLU with negative input send back to its incoming weights for this example?
Exercise
In the two-output network above, set w=12w=\tfrac12 and keep x=2x=2 Find ∂L/∂h\partial L/\partial h and ∂L/∂w\partial L/\partial w in that order.
Exercise
Now set w=−12w=-\tfrac12 still with x=2x=2 Find ∂L/∂h\partial L/\partial h and ∂L/∂w\partial L/\partial w in that order.