Project

Teach a line the penguins

You build a notebook that teaches a line to guess a penguin's mass from its flipper: the loss, its arrow and the steps written by hand in NumPy, then the same line with PyTorch's `backward()` and its optimizers, on batches and in a long valley, and last a line for a table of your own.

About 2.5 hours. You need your own computer, with uv, or Colab, which needs a Google account; an internet connection, to fetch the penguins; a table of your own, as a CSV with two columns of numbers.

You build it in the starter, one step at a time. The finished project is there to read when you are stuck.

Get the starterRead the finished project

  1. Part 1

    The loss and its arrow, by hand

    A line guesses a penguin's mass from its flipper: `w` kilograms for each centimetre past 20, plus `b`. Here you write how wrong the line is, and the arrow that says which way is uphill, in plain NumPy.

    UsesFunctions and lossThe derivativePartial derivatives and the gradientThe chain ruleStochastic gradient descent

    1. The penguins as numbersRun it
    2. How wrong the line isYour turn
    3. One penguin's pullYour turn
    4. The full arrowYour turn
    5. Uphill, not downhillRun it

    Open this part in Colab

    When you have run it

    How long is the full arrow at (0, 0), to two decimal places?

  2. Part 2

    The line learns

    Now the line takes its steps: against the arrow, by a share of it, again and again. You watch it find the penguins, and you make it stop by itself when it reaches the bottom.

    UsesGradient descent and the learning rateThe derivativePartial derivatives and the gradientLoss landscapes

    1. One stepYour turn
    2. Twenty stepsYour turn
    3. Watch it learnRun it
    4. Stop at the bottomYour turn
    5. Three starts, one bottomOn your own

    Open this part in Colab

    When you have run it

    At a rate of 0.3, how many steps does train take from (0, 0)?

  3. Part 3

    PyTorch works out the slopes

    Real code doesn't write the arrow by hand. PyTorch follows the chain back from the loss with `backward()`, and an optimizer takes the step. Here the same line learns that way, then on small random batches, as real training does.

    UsesThe derivativeThe chain ruleGradient descent and the learning rateStochastic gradient descent

    1. The arrow from `backward()`Your turn
    2. Slopes add upRun it
    3. The loopYour turn
    4. Batches of 32On your own

    Open this part in Colab

    When you have run it

    Take optimizer.zero_grad() out of the loop of step 3.3, and run its twenty steps again from (0, 0). What is the line's loss on all the penguins at the end, to one decimal place?

  4. Part 4

    The flipper as it comes, and a line of your own

    Measure the flipper from 0 instead of from 20 cm, and the bowl turns into a long, narrow valley, where plain steps crawl. Momentum and Adam take it on. Then you teach a line a table of your own.

    UsesMomentum and AdamGradient descent and the learning rateFunctions and loss

    1. Plain steps in the valleyRun it
    2. Three ways of steppingYour turn
    3. Adam has a learning rate tooRun it
    4. Your tableOn your own
    5. Your lineOn your own

    Open this part in Colab

    When you have run it

    On the flipper as it comes, what loss did Adam at 0.1 end on after 1,000 steps, to two decimal places?

Run on Python 3.13, NumPy 2.5.5, pandas 3.1.1, PyTorch 2.15.0, scikit-learn 1.9.1, matplotlib 3.11.2. Checked on .