BackpropagationLesson 4 of 6
Work backward through a calculation
Forward values and backward loss derivatives
We know the rules. Now organize them so we can find both parameter derivatives in one pass.
Return to with separate parameters w and b. The observations are x = 2 and y = 5; the starting parameters are w = b = 1.
Forward: calculate the values
Section titled “Forward: calculate the values”Give each intermediate result a name:
| Operation | Calculation | Value |
|---|---|---|
| Multiply | 2 | |
| Add the bias | 3 | |
| Subtract the target | −2 | |
| Square and halve | 2 |
The dependencies form a computation graph: w and x feed the multiplication; p and b feed the addition; the prediction and y feed the subtraction; e feeds the loss.
The forward pass calculates values. Keep the values needed for the derivatives: the loss node needs e, and the multiplication node needs x to differentiate with respect to w.
Backward: calculate loss derivatives
Section titled “Backward: calculate loss derivatives”For any intermediate q, write . This means how quickly the final loss changes with q. It is a different number from q itself.
Start at the loss: . Moving L by a small amount changes L by that same amount. This 1 starts the calculation; it is not the loss value.
Work back through the operations:
| Derivative needed | Already known × local rate | Result |
|---|---|---|
| −2 | ||
| −2 | ||
| −2 | ||
| −2 | ||
| −4 |
Notice the split at the addition: the same helps calculate both and . We calculate it once and reuse it.
What moved backward?
Section titled “What moved backward?”The prediction did not run backward, and the parameters did not change. We passed derivative information from the loss toward the parameters. For this example the result is , exactly the gradient from lesson 2.
An optimizer can now use that gradient to update w and b. If it does, the next backward pass must use values from a new forward pass at the updated parameters.
What if a node is used more than once?
Initialize its accumulated derivative to zero. Every use adds its contribution. For , the multiply operation sends two contributions back to the same w node; they add to 2w. Overwriting the first contribution with the second would lose one path.
In a larger graph, process an operation after the derivative contributions from all of its downstream uses have been gathered. This is reverse dependency order.
Your turn: value or derivative?
Section titled “Your turn: value or derivative?”At the start of this lesson, p = 2. Is also 2? And why is twice ?
Work it through
No. : increasing p a little raises the prediction toward 5 and lowers the loss. The value 2 tells us what p is; −2 tells us its local effect on L.
w changes the prediction at rate x = 2, while b changes it at rate 1. They share the remaining path to L, so and .
Next: apply the same procedure to a hidden layer.