Measure the slope, take a small step, repeat. In 30 steps the loss falls from 20.8 to almost zero, and the same idea trains neural networks and large language models.
python3 --version.pip install "numpy>=1.26"git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
pip install -r requirements.txt # or just this lesson: pip install "numpy>=1.26"
cd machine-learning/01-gradient-descent python3 src/descent.py
import numpy as np
loss = lambda w: .5 * (w[0]**2 + 10 * w[1]**2)
grad = lambda w: np.array([w[0], 10 * w[1]])
w = np.array([-4.0, 1.6]) # start high
lr, start = 0.17, loss(w) # step size
for step in range(30):
w = w - lr * grad(w) # step downhill
print(f"{start:.1f} -> {loss(w):.4f}")This is how every AI on the planet learns. Picture it standing on a hill. Blindfolded. The hill is its mistakes.
Up high? Way off. Down at the bottom? Basically perfect.
It can't see the bottom, so it feels the slope under its feet, and takes one small step downhill. Then again. And again. Thirty tiny steps.
Its mistakes drop from 20.8 to almost zero. That's gradient descent. Feel the slope, step down, repeat.
Read the lesson on GitHub →