Skip to content

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 y^=wx+b\hat y=wx+b with separate parameters w and b. The observations are x = 2 and y = 5; the starting parameters are w = b = 1.

Give each intermediate result a name:

OperationCalculationValue
Multiplyp=wx=12p=wx=1\cdot22
Add the biasy^=p+b=2+1\hat y=p+b=2+13
Subtract the targete=y^y=35e=\hat y-y=3-5−2
Square and halveL=e2/2=(2)2/2L=e^2/2=(-2)^2/22

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.

For any intermediate q, write gq=L/qg_q=\partial L/\partial q. This means how quickly the final loss changes with q. It is a different number from q itself.

Start at the loss: gL=L/L=1g_L=\partial L/\partial L=1. 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 neededAlready known × local rateResult
geg_egLe=1(2)g_L\cdot e=1\cdot(-2)−2
gy^g_{\hat y}ge1g_e\cdot1−2
gpg_pgy^1g_{\hat y}\cdot1−2
gbg_bgy^1g_{\hat y}\cdot1−2
gwg_wgpx=(2)2g_p\cdot x=(-2)\cdot2−4

Notice the split at the addition: the same gy^=2g_{\hat y}=-2 helps calculate both gpg_p and gbg_b. We calculate it once and reuse it.

Step through the forward values, then the backward derivatives. The parameter values remain fixed throughout the calculation.

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 (gw,gb)=(4,2)(g_w,g_b)=(-4,-2), 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 u=wwu=w\cdot w, 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.

At the start of this lesson, p = 2. Is gpg_p also 2? And why is gwg_w twice gbg_b?

Work it through

No. gp=2g_p=-2: 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 gw=(2)2=4g_w=(-2)\cdot2=-4 and gb=(2)1=2g_b=(-2)\cdot1=-2.

Next: apply the same procedure to a hidden layer.

Sources and further reading

Definition

Read the full glossary entry →