Projectabout 2.5 hours
Project: fit a line by gradient descent
After this project you can write the loss, its gradient and the steps yourself, and watch a line learn its data.
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.
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
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
- The penguins as numbersRun it
- How wrong the line isYour turn
- One penguin's pullYour turn
- The full arrowYour turn
- Uphill, not downhillRun it
When you have run it
How long is the full arrow at (0, 0), to two decimal places?
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
- One stepYour turn
- Twenty stepsYour turn
- Watch it learnRun it
- Stop at the bottomYour turn
- Three starts, one bottomOn your own
When you have run it
At a rate of 0.3, how many steps does
traintake from (0, 0)?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
- The arrow from `backward()`Your turn
- Slopes add upRun it
- The loopYour turn
- Batches of 32On your own
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?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
- Plain steps in the valleyRun it
- Three ways of steppingYour turn
- Adam has a learning rate tooRun it
- Your tableOn your own
- Your lineOn your own
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 .
