N
NexusDigitalLabs
← Back to Academy

Phase 1 — Foundations · Lesson 18 · 20 XP

Gradient descent and a neural net by hand

A loss function measures how wrong a model's predictions are. Its gradient with respect to a parameter tells you which direction increases the loss — so training moves the parameter the opposite way, a little at a time: param -= learning_rate * gradient. Repeat that over many examples and the loss goes down.

# fit y = w*x + b by gradient descent, no framework
w, b, lr = 0.0, 0.0, 0.01
for epoch in range(200):
    dw = sum(2 * (w * x + b - y) * x for x, y in data) / len(data)
    db = sum(2 * (w * x + b - y) for x, y in data) / len(data)
    w -= lr * dw
    b -= lr * db

This is literally a one-neuron network: it learns a line, y = wx + b, entirely by repeated gradient steps, no ML framework involved. Every larger neural net is the same idea — more parameters, more layers, the same core update rule. If you want to go deeper on this, Andrej Karpathy's "Neural Networks: Zero to Hero" builds up from exactly this point.

Exercise

Implement gradient descent from scratch in plain Python to fit a line to a small synthetic dataset. Print the loss every 20 epochs and confirm it decreases toward zero.

Check yourself

1. What does the gradient of the loss with respect to a weight tell you, in plain terms?

2. What happens to training if the learning rate is set far too high?

← Previous lesson

Math: vectors, dot products, probability

Answer the check-yourself questions to unlock this