Lab 1 of 3about 35 minutes
Find the Gentoos' middle, by walking downhill
It uses what you learned in Functions and loss, The derivative, Partial derivatives and the gradient and Gradient descent and the learning rate.
Start here
Run itThe penguins from Python for AI. Here only the Gentoos, 123 of them, each one a point: its bill length across and its bill depth up, in millimetres.
1One point among the Gentoos
Run itFrom Functions and loss
The model is one point, with two weights: a bill length and a bill depth. It starts at (40, 20), away from the Gentoos. Run the cell: it prints the shape and the first Gentoo, and draws them with the point.
2How wrong the point is
Your turnFrom Functions and loss
Each Gentoo gives the point a miss along each direction: the point minus the Gentoo. Square every miss, add each Gentoo's two, and average over all 123. That's the loss: the mean squared distance from the point to the Gentoos.
Hint
Square miss, add along each row with .sum(axis=1), then take .mean().
One way to do it
def loss(point):
miss = point - gentoos
return (miss ** 2).sum(axis=1).mean()3A slope by a tiny nudge
Your turnFrom The derivative and Partial derivatives and the gradient
Nudge one weight and hold the other still: the change in the loss, divided by the nudge, is the slope along that weight. which is 0 for the bill length and 1 for the depth.
Now run the same slope with bigger nudges.
A nudge of 1 is off by exactly 1. This loss bends, so a big nudge measures the wrong thing. As the nudge shrinks, the slopes settle.
Hint
Take the loss at moved, take away the loss at point, and divide by nudge.
One way to do it
def nudge_slope(point, which, nudge=0.000001):
moved = point.copy()
moved[which] += nudge
return (loss(moved) - loss(point)) / nudge4The uphill arrow
Your turnFrom Partial derivatives and the gradient
There's also a formula for this loss: the slopes are twice the point minus the Gentoos' average, both weights at once. Write it, and the two slopes come out as one arrow, the gradient. np.allclose holds it against the nudges.
Hint
gentoos.mean(axis=0) is the average Gentoo. Take it from point, then double.
One way to do it
def gradient(point):
return 2 * (point - gentoos.mean(axis=0))5One step against the arrow
Your turnFrom Partial derivatives and the gradient and Gradient descent and the learning rate
The step: the new point is the old one, minus the learning rate times the arrow.
Now take two steps at a rate of 1.0.
The first step jumps across the middle, as far out as it started. The second lands back on (40, 20). The loss never falls.
Hint
Go against the arrow: take a share of it away from the point.
One way to do it
def step(point, rate):
return point - rate * gradient(point)6Three walkers
Your turnFrom Gradient descent and the learning rate
Write the line of the loop: each pass takes one step. walk keeps the loss before the first step and after each one, and the cell draws each walk's loss curve. Then keep in best_rate the largest rate whose loss falls steadily.
Hint
The loop's line is one call to step. For best_rate, read the three curves: which ones fall, and which of those is largest?
One way to do it
def walk(rate, steps=20):
point = start
losses = [loss(point)]
for _ in range(steps):
point = step(point, rate)
losses.append(loss(point))
return point, losses
for rate in (0.01, 0.1, 1.1):
point, losses = walk(rate)
plt.plot(losses, label=f"rate {rate}")
plt.ylim(0, 100)
plt.legend()
best_rate = 0.17The Adelies' middle
On your ownFrom Partial derivatives and the gradient and Gradient descent and the learning rate
On your own now. Find the Adelies' middle the same way, with a third weight: bill length, bill depth and flipper length, all in millimetres. Keep the Adelies in adelies, then walk fifty steps at a rate of 0.1 from (40, 20, 200), and keep where the walker lands in middle.
Hint
Pick the Adelies as the Setup picks the Gentoos, with three columns. gradient reads gentoos, so write one for adelies: the same formula, now with three weights.
One way to do it
adelie = table[table.species == "Adelie"].dropna(subset=["bill_length_mm", "bill_depth_mm", "flipper_length_mm"])
adelies = adelie[["bill_length_mm", "bill_depth_mm", "flipper_length_mm"]].to_numpy()
def adelie_gradient(point):
return 2 * (point - adelies.mean(axis=0))
middle = np.array([40.0, 20.0, 200.0])
for _ in range(50):
middle = middle - 0.1 * adelie_gradient(middle)
print(middle.round(2))Run on Pyodide 314, Python 3.14, NumPy 2.4, pandas 3.0 and matplotlib 3.10. Checked on .
