Hands on ML with pytorch

fit sin(x) · 6 steps up the abstraction ladder
0 / 6 explored

Workshop · read here, build in a notebook

Six ways to fit a sine wave

One problem — approximate y = sin(x) with a third-order polynomial — solved six times. Each step hands one more job to PyTorch, and the code you write gets shorter.

This is based on tutorials in docs.pytorch.org. You’ll watch each abstraction replace something you had to write by hand.

step 0 · loss
target · sin(x) model · a+bx+cx²+dx³
tap the plot to run it again
01 · READ

Study each step here

Every step shows the exact code first, with tappable annotations for each new feature, operator and number.

02 · TYPE

Rebuild it in a notebook

The code on this page can’t be copied on purpose. Type it yourself in Colab — that’s where it sticks.

03 · RUN

Run and experiment

Run each step, watch the loss fall, then try the small challenge at the bottom of the page.

Where you’ll write and run the code

You won’t type into this page — the code panels are read-only. Open a PyTorch-ready notebook, type each step yourself, and run it there. Google Colab already has torch and numpy installed, so there’s nothing to set up.

Any environment with PyTorch works too (local Jupyter, VS Code, Kaggle). The point is to type the code, not paste it.

Open a new Google Colab notebook

Reference

The abstraction ladder

The same fit, six times. Reading top to bottom, each step hands one more job to PyTorch.

1
NumPy
You write the forward pass, the backward pass, and the update — all by hand.
2
Tensors
Same code, on PyTorch tensors that can run on a GPU. Manual backward stays.
3
Autograd
The hand-written backward pass is goneloss.backward() fills every gradient.
4
nn
The hand-written model and loss are gone — layers and MSELoss replace them.
5
optim
The manual weight-update loop is gone — an optimizer does step().
6
nn.Module
You package the whole model as your own reusable class.

Feature glossary

Every annotation introduced in the workshop. The ones you opened are marked.