borch

Learn · 0

Fix the bug

Two training loops that do not learn. Each has one wrong line. Find it, change it, press Run — the page says whether the loss came down. Python, the way you would write it for torch; nothing to install.

The loop that stands still

The loss is printed every fifteen steps. Run it as it is first, and read the numbers: they do not move. One argument on one line is the reason.

Stuck? The answer.

lr=0.0 is a learning rate of zero: opt.step() moves each weight by lr × gradient, and zero times anything is nothing. Try lr=0.1. Too large a rate has its own failure — try 1.0 after, and watch.

The loop that runs away

This one has a real learning rate and still does not land. Run it: the loss swings and never settles — after sixty steps it is still where it started. One call is missing from the loop.

Stuck? The answer.

Gradients accumulate: every backward() adds to .grad rather than replacing it, so without opt.zero_grad() before it each step moves by the sum of every gradient so far. Put opt.zero_grad() back at the start of the loop body.

What you have now

A model that learned y = 3x + 1, in a browser tab, from code that is torch's except for its import. It can leave as the file every serving runtime reads:

That is torch.onnx.export, written to the page's own filesystem. The Python page lists everything that differs from torch; it is short.

Where next. Lesson 1 starts the TypeScript side from tensors up. The Playground runs your own code, Python or TypeScript. The Models page runs pretrained networks here, from safetensors.