Basis Lab

From a Straight Line to Backpropagation

One Sweep Back Instead of Seven Chains

Last timeTwo Layers, Every Derivative by Hand

The seven derivatives of a two unit network are built out of the same two numbers, and computing those once and sweeping backwards costs about three forward passes.

Seven derivatives came out of the two unit network, one for each of its seven

parameters, and each was written as its own chain of factors. Laid out side by

side those chains repeat themselves. Two numbers appear over and over, and they

are not cheap numbers: one of them contains a squashing function and its

derivative.

That repetition is not an accident of a network with two hidden units. It is the

shape of every network, and deciding to pay for it once is the whole of the

method this lesson names.

The network, and the numbers the forward pass left behind

The model is the one from the previous two lessons. The input is scaled as

u=x/12u = x/12, two hidden units squash a line each, and the prediction is a

weighted sum of the two unit outputs plus an offset:

$y^=v1t1+v2t2+c,tj=tanh⁡(wju+bj)\hat{y} = v_1 t_1 + v_2 t_2 + c, \qquad t_j = \tanh(w_j u + b_j)$

Here wjw_j and bjb_j are the weight and offset inside unit jj, tjt_j is that

unit's output, vjv_j is the weight the prediction puts on it, and cc is the

prediction's own offset. That is seven parameters. For one measurement with

input xx and value yy the contribution to the total squared error is

E=(y^−y)2E = (\hat{y} - y)^2, and r=y^−yr = \hat{y} - y is the miss.

The arithmetic is done at x=4x = 4, where y=15y = 15 and u=4/12=1/3u = 4/12 = 1/3, with the

starting weights w1=1w_1 = 1, b1=0b_1 = 0, v1=20v_1 = 20, w2=−2w_2 = -2, b2=0.5b_2 = 0.5,

v2=10v_2 = 10 and c=15c = 15. The forward pass gives

a1=1/3+0=0.333333a_1 = 1/3 + 0 = 0.333333 and t1=tanh⁡(0.333333)=0.321513t_1 = \tanh(0.333333) = 0.321513, then

a2=−2/3+0.5=−0.166667a_2 = -2/3 + 0.5 = -0.166667 and t2=tanh⁡(−0.166667)=−0.165140t_2 = \tanh(-0.166667) = -0.165140, and so

$y^=20(0.321513)+10(−0.165140)+15=6.430255−1.651404+15=19.778851\hat{y} = 20(0.321513) + 10(-0.165140) + 15 = 6.430255 - 1.651404 + 15 = 19.778851$

The miss is r=19.778851−15=4.778851r = 19.778851 - 15 = 4.778851 and the contribution to the error is

r2=22.837413r^2 = 22.837413.

FIG 1One pass out, one pass back
The solid arrows are the ordinary evaluation of the model and the dashed ones are the sweep that produces the gradient. Every dashed arrow carries one number and every box on the way back is where that number is turned into the next one. The picture is worth holding on to because the backward arrows do not fan out as the forward ones do: thirteen boxes, and the sweep visits each of them once.

The same two numbers, seven times over

Differentiate the error contribution with respect to the prediction first, before

touching any parameter. Since E=(y^−y)2E = (\hat{y} - y)^2,

$∂E∂y^=2(y^−y)=2r=2(4.778851)=9.557701\frac{\partial E}{\partial \hat{y}} = 2(\hat{y} - y) = 2r = 2(4.778851) = 9.557701$

Call that number δ\delta. It is the quantity arriving at the prediction, and

every one of the seven derivatives is built on it.

Next, the quantity arriving at a hidden unit's sum aja_j. The error reaches aja_j

only through tjt_j, and then only through the prediction, so two factors stand

between them: the weight vjv_j, which says how much the prediction moves when

tjt_j moves, and the local slope of the squashing function, which differentiating

the hyperbolic tangent gives as 1−tj21 - t_j^2. Write gjg_j for the product:

$gj=δ vj (1−tj2)g_j = \delta \, v_j \, (1 - t_j^2)$

With t12=0.3215132=0.103370t_1^2 = 0.321513^2 = 0.103370 the first unit's local slope is

1−0.103370=0.8966301 - 0.103370 = 0.896630, and with t22=(−0.165140)2=0.027271t_2^2 = (-0.165140)^2 = 0.027271 the

second's is 0.9727290.972729. So

$g1=9.557701×20×0.896630=171.394349g_1 = 9.557701 \times 20 \times 0.896630 = 171.394349$

$g2=9.557701×10×0.972729=92.970498g_2 = 9.557701 \times 10 \times 0.972729 = 92.970498$

Two levels of sharing, and they are different. The number δ\delta is shared by

all seven parameters. The number gjg_j is shared by exactly the two parameters

inside unit jj and by nothing else. Neither is a per-parameter quantity: both

belong to a place in the network rather than to a knob, which is why there are

three of them and not seven.

The lesson stops here

6 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Measuring How Wrong a Straight Line Is, on Twelve Points
  2. 02The Two Equations That Pin Down the Best Lineopening only
  3. 03When the Derivative Equations Stop Being Solvableopening only
  4. 04The Largest Step This Data Allowsopening only
  5. 05Putting a Bend in the Model, and Differentiating Through Itopening only
  6. 06Every Derivative of a Two Unit Network, by Handopening only
  7. 07One Sweep Back Instead of Seven Chainsyou are here
  8. 08Running It, and Reading the Curve It Producesopening only

Read alongside