Chapter 8 of 8
Gradient descent: how models learn
This is where it all comes together, and it is the exact loop that trains an LLM. Start somewhere. Compute the gradient (which way is uphill). Take a small step the opposite way. Repeat, and watch the value fall toward the minimum. In this lab you minimize a simple function and watch gradient descent DISCOVER the answer on its own. When you meet Chapter 8 of the LLM course, this same loop will be training a real model.
The lab: read it, then run it
#!/usr/bin/env python3
"""
LAB F8: Gradient descent. The algorithm that trains every model.
Now you put it together and MINIMIZE a function. Start somewhere, compute the
gradient (which way is uphill), step a little the opposite way, repeat. Watch the
value fall toward the minimum. This is the exact loop that trains an LLM, and it
is the bridge into Chapter 8 of the LLM course, where the same loop trains a real
model.
Run: python3 modules/academy-content/labs/foundations/f8-gradient-descent.py
"""
import sys
# minimize f(x) = (x - 3)^2. The minimum is obviously at x = 3, where f = 0.
# f'(x) = 2 * (x - 3). We will let gradient descent DISCOVER x = 3 on its own.
def f(x): return (x - 3) ** 2
def grad(x): return 2 * (x - 3)
x = 0.0 # start far from the answer
lr = 0.1 # learning rate: how big a step to take
history = []
for step in range(50):
loss = f(x)
history.append((step, x, loss))
x = x - lr * grad(x) # step downhill
print("STEP 1-2: gradient descent minimizing f(x) = (x - 3)^2, starting at x = 0")
for step, xv, loss in [history[i] for i in (0, 5, 10, 20, 49)]:
print(f" step {step:2d} x = {xv:6.4f} loss = {loss:8.5f}")
# STEP 3: the invariant. Descent must converge: x ends near 3 and loss near 0,
# and the loss must be lower at the end than at the start (it actually learned).
final_x, final_loss = x, f(x)
ok = (abs(final_x - 3.0) < 0.01) and (final_loss < history[0][2])
print("")
print(f"STEP 3: converged to x ~ 3 and loss fell from {history[0][2]:.3f} to {final_loss:.5f}: {'YES' if ok else 'NO'}")
if not ok:
sys.exit(1)
print("")
print("That is training. In the LLM course you run this exact loop on a real model.")
print("You now have the Python and the math. You are ready to build LLMs.")
Runnable lab
f8-gradient-descent.pyRun gradient descent to minimize a function and watch it converge.
Proves: converged to x ~ 3 and loss fell
Runs in your browser via Pyodide. First run loads the runtime once; no install, no server.
Gradient descent found the minimum on its own: the value walked to 3 and the loss fell to nearly zero, guided only by the gradient. That is learning, stripped to its essence. You now have the Python and the math that the LLM course assumes. You are ready to build language models. Head to Course 1 and start with the tokenizer.
Check your understanding
1. What is the core loop of gradient descent?
2. What is the learning rate?
3. How does this connect to training an LLM?