Syllabus Lesson 104 of 239 · Neural-Net Intuition, LLMs & AI Capstone
Neural-Net Intuition, LLMs & AI Capstone

Train a Model with Your Engine

You built the autograd engine. Now use it to actually learn, the loop that trains every neural network. The Value class is given (it now also wraps plain numbers, so w * x works when x is a float). Your job is the training loop.

Fit a single weight w so w * x predicts y. One step of gradient descent is always the same moves:

w = Value(0.0)
for step in range(steps):
    loss = Value(0.0)
    for x, y in zip(xs, ys):
        diff = w * x + (-y)          # prediction minus target
        loss = loss + diff * diff    # squared error, summed
    w.grad = 0.0                     # 1) reset the gradient
    loss.backward()                  # 2) backprop to fill w.grad
    w.data -= lr * w.grad            # 3) step downhill

Two easy mistakes: zero the gradient before each backward() or old gradients pile up (backward uses +=); and subtract lr * w.grad to go downhill, not add. Over enough steps w slides to the value that minimises the loss, the line of best fit.

Write fit(xs, ys, steps, lr) starting from w = Value(0.0), returning the final w.data. Press Run to watch it learn y = 2x.

Your turn

Write fit(xs, ys, steps, lr) that fits a weight w (start Value(0.0)) by gradient descent so w * x predicts y. Each step: build loss as the summed squared error with Value ops, set w.grad = 0.0, loss.backward(), then w.data -= lr * w.grad. Return the final w.data. The Value class is provided.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output