The one-line idea
Every AI model is a calculation with adjustable numbers inside. Training means changing those numbers until the model's mistakes get smaller. The method that changes them is gradient descent.
An LLM is trained with the same method. It's this loop, repeated an enormous number of times.
| Example 1 | Example 2 | |
|---|---|---|
| Goal | Find the best value of one number w | Guess the next word |
| Why | Simplest case, every step is clear | Same job an LLM does |
Part 1: What is "loss"?
Loss is one number that says how bad the model is right now. Lower is better, and 0 is perfect.
Example 1. We want w to be 3 (the model doesn't know this). Loss = .
| w | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Loss | 9 | 4 | 1 | 0 | 1 |
Example 2. The model sees "the cat" and must pick the next word from cat, dog, sat, mat. The correct word is sat.
The model gives each word a score. Softmax turns scores into probabilities (between 0 and 1, adding up to 1). Higher score means higher probability.
| Scores (cat, dog, sat, mat) | Probabilities |
|---|---|
| 0, 0, 0, 0 | 0.25, 0.25, 0.25, 0.25 |
| -0.25, -0.25, 0.75, -0.25 | 0.175, 0.175, 0.475, 0.175 |
Loss only looks at the probability of the correct word:
| P("sat") | 0.99 | 0.9 | 0.25 | 0.1 | 0.01 |
|---|---|---|---|---|---|
| Loss | 0.010 | 0.105 | 1.386 | 2.303 | 4.605 |
High probability on the right word means low loss. At the start, loss = .
Part 2: What is the gradient?
The gradient is the slope. If I increase this number a tiny bit, how much does the loss change?
Example 1, checked with real numbers. At w = 0 the loss is 9. At w = 0.01 it's . The loss changed by -0.0599 for a +0.01 move, so the slope is about -6. The exact formula gives . Same.
- Negative gradient: increasing the number lowers the loss, so increase it.
- Positive gradient: decrease the number.
Example 2. For softmax with this loss, the gradient of each score is:
| Word | cat | dog | sat | mat |
|---|---|---|---|---|
| Gradient | 0.25 | 0.25 | 0.25 − 1 = -0.75 | 0.25 |
sat has a negative gradient, so its score goes up. The wrong words have positive gradients, so their scores go down.
Part 3: The update
Move each number against its gradient:
lr is the learning rate, the step size we choose.
Example 1 (lr = 0.1)
| Value | |
|---|---|
| Old w | 0 |
| Gradient | -6 |
| New w | 0 − 0.1 × (−6) = 0.6 |
| New loss | 5.76 (was 9) |
Example 2 (lr = 1)
| Word | cat | dog | sat | mat |
|---|---|---|---|---|
| Old score | 0 | 0 | 0 | 0 |
| Gradient | 0.25 | 0.25 | -0.75 | 0.25 |
| New score | -0.25 | -0.25 | 0.75 | -0.25 |
| New probability | 0.175 | 0.175 | 0.475 | 0.175 |
New loss = (was 1.386). Both examples improved after one step.
Part 4: Repeat
Training is this loop:
- Measure the loss
- Compute the gradient
- Update the numbers
- Go back to 1
Example 1 (lr = 0.1)
| Step | w | Loss |
|---|---|---|
| 0 | 0 | 9 |
| 1 | 0.600 | 5.76 |
| 3 | 1.464 | 2.36 |
| 10 | 2.678 | 0.104 |
| 30 | 2.996 | 0.00 |
Example 2 (lr = 1)
| Step | P("sat") | Loss |
|---|---|---|
| 0 | 0.25 | 1.386 |
| 1 | 0.475 | 0.744 |
| 5 | 0.842 | 0.172 |
| 20 | 0.961 | 0.040 |
| 100 | 0.992 | 0.008 |
In both, the loss falls fast at first and slowly later. As the model improves, the gradient shrinks, so the steps shrink. In Example 2, going from 0.96 to 0.99 takes 80 more steps.
Part 5: The learning rate matters
Example 1, same start (w = 0), after 30 steps:
| lr | w | Loss | What happened |
|---|---|---|---|
| 0.01 | 1.364 | 2.678 | Too slow, still far from 3 |
| 0.1 | 2.996 | 0.00 | Good |
| 0.9 | 2.996 | 0.00 | Jumps back and forth over 3, still converges |
| 1.1 | -709.1 | 507,128 | Each jump overshoots more than the last, loss explodes |
Too small wastes time. Too big breaks training.
Part 6: How is an LLM different?
An LLM does Example 2, scaled up.
| Example 2 (ours) | An LLM | |
|---|---|---|
| Job | Next word from 4 options | Next token from tens of thousands of options |
| Input | "the cat" | A long stretch of text |
| Where scores come from | 4 free numbers we adjust directly | A huge calculation with billions of adjustable numbers |
| Loss | −ln(P(correct)) | Same |
| Data | 1 example | An enormous amount of text |
| Learning loop | Loss, gradient, update, repeat | Same |
A token is a small chunk of text, often a word or part of a word.
Where the examples come from: take a piece of text, hide the next word, ask the model to guess it, measure the loss, update. Every position in every text is a free example, because the answer is already in the text.
The one real addition: the scores come from a long chain of calculations, so finding each number's gradient uses the chain rule again and again. That's called backpropagation, and it's still just "find each number's slope."
What this article does not cover
- How the model computes its scores (in modern LLMs, a transformer). That's a separate topic.
- Training tricks. Real training uses small batches and improved versions of gradient descent, but the core step is still "move against the gradient."
- What happens after this stage. Models are often trained further.
Glossary
| Term | Meaning |
|---|---|
| Loss | One number: how bad the model is right now |
| Gradient | Slope of the loss for one number inside the model |
| Gradient descent | Moving numbers against their gradient to lower the loss |
| Learning rate | Size of each step |
| Softmax | Turns scores into probabilities that add up to 1 |
| Token | A small chunk of text |
| Backpropagation | Computing the gradient for every number in a multi-step calculation |
Full code
import numpy as np
# Example 1: find w that minimises (w-3)^2
w = 0.; lr = 0.1
for step in range(1, 31):
grad = 2*(w - 3)
w -= lr*grad
if step in (1, 3, 10, 30):
print(step, round(w, 4), round((w-3)**2, 4))
# Example 2: guess the next word (cat, dog, sat, mat), correct = sat
softmax = lambda z: np.exp(z)/np.exp(z).sum()
scores = np.zeros(4); correct = 2; lr = 1.0
for step in range(101):
p = softmax(scores)
loss = -np.log(p[correct])
if step in (0, 1, 5, 20, 100):
print(step, p.round(3), round(loss, 4))
grad = p.copy(); grad[correct] -= 1 # probability minus (1 for correct word)
scores -= lr*gradTry it: change lr in Example 1 to 1.1 and watch the loss explode, like the table in Part 5.
Tags: gradient descent, LLM, loss, learning rate, next word prediction, beginner


