Lab 2 of 3about 40 minutes
Get a two-layer line off the saddle
It uses what you learned in The chain rule and Loss landscapes.
Start here
Run itThe penguins from Python for AI, as this course has them: x is each flipper in centimetres past 20 cm, and mass is each weight in kilograms. loss is the two-layer line's mean squared error for a pair of weights. nudged finds the slopes of any loss by a tiny nudge, as in lab 1.
1The landscape
Run itFrom Loss landscapes
The line with two layers, as in calc-loss-landscape: the flipper times w1, times w2, plus 4.2. Run the cell. It draws the loss over w1 and w2, darker where it's lower, and prints the loss at the middle and in each valley.
Two dark valleys, where w1 times w2 is about a half, and between them the flat line's 0.641.
2One penguin's chain
Your turnFrom The chain rule
The penguin from calc-chain-rule, 3 cm past the mark and 5.7 kg, on the flat line: w1 at 0, w2 at 1. Multiplying by a number has that number as its rate, adding has a rate of 1, and the square's rate is twice the miss. Write the two missing rates, then the chain's rate.
Now a nudge, and the rates added.
The nudge says -9 too. Added, the rates say plus 2: the wrong way.
Hint
The second layer multiplies by w2. np.prod multiplies the numbers of a list.
One way to do it
w1, w2 = 0.0, 1.0
layer = 3 * w1
guess = layer * w2 + 4.2
miss = guess - 5.7
rates = [3, w2, 1, 2 * miss]
slope = np.prod(rates)
print(round(float(slope), 3))3Both slopes, for all 342
Your turnFrom The chain rule and Loss landscapes
Every penguin has the same chain, with its own flipper and its own miss, and the loss averages them. So each weight's slope is the average of its chain's rate over the penguins. Write both. The chain for w1 passes through w2, and the chain for w2 passes through w1.
Hint
A penguin's chain for w1 has the rates x, w2, 1 and 2 * miss. Multiply them, then take .mean() over the penguins.
One way to do it
def slopes(point):
w1, w2 = point
miss = x * w1 * w2 + 4.2 - mass
slope_w1 = (x * w2 * 2 * miss).mean()
slope_w2 = (x * w1 * 2 * miss).mean()
return np.array([slope_w1, slope_w2])
print(slopes([0.7, -0.69]).round(3), nudged(loss, [0.7, -0.69]).round(3))4A walker on the saddle
Your turnFrom Loss landscapes
Write the step into the walk: against the arrow, at the learning rate. Then start a walker at (0, 0), as lessons 3 and 4 started theirs.
Hint
The step is the point, minus the rate times slopes(point).
One way to do it
def walk(start, rate=0.1, steps=40):
point = np.array(start, dtype=float)
losses = [loss(point)]
for _ in range(steps):
point = point - rate * slopes(point)
losses.append(loss(point))
return point, losses
point, losses = walk([0.0, 0.0])
print(point, round(float(losses[-1]), 3))5Off the saddle
Your turnFrom Loss landscapes
Two walkers start on the ridge, 0.02 apart: (0.70, -0.69) and (0.70, -0.71). Walk the second one too, and draw both loss curves: they lie on top of each other. Then count in plateau the first walker's steps whose loss is within 0.05 of the saddle's 0.641.
Hint
np.array(first_losses) - 0.641 is each loss's distance from the saddle. Make a mask of the ones under 0.05 with np.abs, then .sum() counts them.
One way to do it
first, first_losses = walk([0.70, -0.69])
second, second_losses = walk([0.70, -0.71])
plt.plot(first_losses, label="from (0.70, -0.69)")
plt.plot(second_losses, label="from (0.70, -0.71)")
plt.legend()
plateau = (np.abs(np.array(first_losses) - 0.641) < 0.05).sum()
print(first.round(2), second.round(2), plateau)6A third layer
On your ownFrom The chain rule and Loss landscapes
On your own now. Give the line a third layer: x * w1 * w2 * w3 + 4.2. Write loss3(point), and slopes3(point) by the chain: each weight's chain passes through the other two. Start from small random weights, np.random.default_rng(42).normal(0, 0.1, size=3), walk 300 steps at a rate of 0.1, and keep where it lands in end3 and its losses in losses3.
Hint
Steps 3 and 4 again, with a third weight. The chain for w1 now has the rates x, w2, w3, 1 and 2 * miss.
One way to do it
def loss3(point):
w1, w2, w3 = point
return ((x * w1 * w2 * w3 + 4.2 - mass) ** 2).mean()
def slopes3(point):
w1, w2, w3 = point
miss = x * w1 * w2 * w3 + 4.2 - mass
return np.array([(x * w2 * w3 * 2 * miss).mean(), (x * w1 * w3 * 2 * miss).mean(), (x * w1 * w2 * 2 * miss).mean()])
end3 = np.random.default_rng(42).normal(0, 0.1, size=3)
losses3 = [loss3(end3)]
for _ in range(300):
end3 = end3 - 0.1 * slopes3(end3)
losses3.append(loss3(end3))
print(end3.round(2), round(float(losses3[-1]), 3))Run on Pyodide 314, Python 3.14, NumPy 2.4, pandas 3.0 and matplotlib 3.10. Checked on .
