Train a tiny model on four data points and watch it discover a hidden rule by predicting, measuring its error and correcting itself, one line at a time.
Machine Learning · beginner · 15 min · 32 steps · free
w and b)# Hours studied -> exam score. The hidden rule is score = 2 * hours + 1.
xs = [1, 2, 3, 4]
ys = [3, 5, 7, 9]
w, b = 0.0, 0.0 # the model starts knowing nothing
lr = 0.05 # learning rate: how big each correction is
for epoch in range(3):
preds = [w * x + b for x in xs]
errors = [p - y for p, y in zip(preds, ys)]
loss = sum([e * e for e in errors]) / len(xs)
dw = 2 * sum([e * x for e, x in zip(errors, xs)]) / len(xs)
db = 2 * sum(errors) / len(xs)
w = w - lr * dw
b = b - lr * db
print(f"epoch {epoch}: loss {loss:.2f}, w {w:.2f}, b {b:.2f}")xs holds how many hours each one studied. The model will try to predict their scores from this.ys holds the scores they actually got. The hidden rule is score = 2 × hours + 1, but the model is never told that. It has to discover it from these four examples.w × hours + b. Both start at 0, so right now it predicts 0 for everyone. On the chart, the blue line lies flat on the floor, far below the green points. Below it, the loss map shows how wrong every possible (w, b) would be: darker is worse, the blue dot is the model now and the star is the best it could ever do.lr decides how big each correction will be. 0.05 means "move 5% of the way the gradient suggests". Too small and learning is slow; too big and it overshoots (try the other version above to see).w and b, then nudge them.w * x + b. With w and b both 0, every prediction is 0: the hollow dots on the chart all sit on the flat line.dw is the gradient for w: how the loss changes if w grows a little. It is −35. Negative means "if you increase w, the loss goes down".db is the gradient for b: the average of 2 × error, which is −12. Also negative, so raising b will lower the loss too. On the loss map, the amber arrow is the gradient (−35, −12): it points uphill, towards a worse model. The green dashed arrow is where the update will actually go: the opposite way, downhill.w against its gradient: 0 − 0.05 × (−35) = 1.75. The line tilts up sharply towards the points, and on the loss map the dot moves right along the w axis.b the same way: 0 − 0.05 × (−12) = 0.6. The line also lifts a little (the dashed grey line is where it was a moment ago), and the dot on the loss map moves up, landing where the green arrow pointed.w = 1.75 and b = 0.6.dw is now −5.75: still negative, but much smaller. When the model is nearly right, the gradient gets small, so the steps get small too.db is −2.05, so b will rise a bit more.w becomes 1.75 − 0.05 × (−5.75) = 2.0375. That is already very close to the true slope of 2.b becomes 0.6 − 0.05 × (−2.05) = 0.7025, creeping towards the true value of 1.w = 2.04 and b = 0.70.b.dw is −0.925: a gentle nudge.db is −0.41.w becomes 2.084: it has slightly overshot 2. w and b are learned together, so while b is still too low, w leans a little high to make up for it. More epochs would let both settle.b becomes 0.723, still climbing towards 1. The bias usually learns more slowly than the slope here, because its gradient does not get multiplied by x.range(3) is used up, so training stops. Nobody told the model the rule: it found it by repeating predict, measure, nudge. Every neural network, however big, learns with this same loop.w × hours + b.db is +4. What does b = b - lr * db do to b?Answer: Makes it smaller. Subtracting a positive amount makes b smaller. A positive gradient means increasing b would raise the loss, so we decrease it.
Answer: Use a smaller learning rate. A growing loss means each step overshoots. Smaller steps let the model settle instead of bouncing away.
Answer: So positive and negative errors do not cancel, and big errors count more. An error of −5 and +5 would add up to 0 without squaring. Squaring also makes a miss of 9 cost 81, far more than a miss of 3.
← Putting Features on the Same Scale · k-Nearest Neighbours: Judge by the Company →