Measuring how wrong: the loss
Before a model can improve, it needs a way to measure how badly it is doing. That measure is a single number called the loss. High loss means many mistakes; zero would mean none. All of learning comes down to one goal: change the weights so the loss goes down.
For a language model the idea is the same, with a different loss. At every position in its training text, the model gives a chance to each possible next token. The loss is high when it gave the token that really came next a low chance, and low when it saw it coming. You will meet this loss, called cross-entropy, in the next lesson.
Slopes: which way is downhill
The slope (mathematicians say derivative) answers one question: if I nudge this number up a tiny bit, does the loss go up or down, and by how much? A positive slope means “uphill to the right, so go left”. A negative slope means go right. A slope of zero means you are at the bottom of a valley (or the top of a hill).
loss(x) = x²A simple valley with its lowest point at x = 0.
slope(x) = 2xThe rule for its slope. You do not need to work these out yourself: software does it for every weight automatically.
at x = 3: slope = 6Positive and steep: the loss rises quickly to the right, so step left.
Gradient descent: step, check, repeat
Put the pieces together and you get the method that trains nearly every model in this course, gradient descent:
new x = x − learning rate × slopeStep against the slope. The learning rate sets how big the step is.
| Step | x | Slope (2x) | New x |
|---|---|---|---|
| 1 | 3 | 6 | 3 − 0. |
| 2 | 2. | 4. | 2. |
| 3 | 1. | 3. | 1. |
| 4 | 1. | 3. | 1. |
| 5 | 1. | 2. | 1. |
Each step shrinks x by a fifth. The steps get smaller by themselves as the slope flattens near the bottom.
This curve has two valleys: a deep one on the left and a shallower one on the right. The ball starts at −3. Run it with each learning rate, then move the starting point to the right-hand side and run it again.
This interactive part needs JavaScript.
What you should have seen:
- 0.01: far too timid. After 20 steps the ball has barely moved.
- 0.05 and 0.2: it settles neatly in the deep valley; the bigger rate gets there in about 3 steps.
- 0.6: too bold. It jumps right over the valley and bounces from side to side, never settling.
- Starting on the right: it settles in the shallow valley, a local minimum, and stays there. From where it stands, every direction is uphill, so it cannot know a deeper valley exists.
A team is training a model. What does each loss curve tell you about the learning rate?
-
After a day of training the loss has hardly changed
Answer: Too small
-
The loss jumps up and down and then shoots up to huge numbers
Answer: Too big
-
The loss falls fast at first, then keeps falling slowly and smoothly
Answer: About right
-
The loss falls, but so slowly that training would take years
Answer: Too small
-
The loss suddenly spikes upward in the middle of training
Answer: Too big
Billions of dials at once
The demo had one number to adjust. A real model has billions of weights, and the loss depends on all of them. The idea does not change: for every weight, work out its slope (how the loss would change if that one weight were nudged), and nudge every weight a little against its own slope, all at the same time.
All those slopes together form a vector with one entry per weight, called the gradient. That is where the name comes from: gradient descent means stepping down the gradient. You no longer have a curve but a landscape with billions of directions, and nobody can picture it, but the rule is the same simple one you just used.
How do you find billions of slopes quickly? With a clever bookkeeping method called backpropagation, which works out all of them in about twice the time it takes to run the model once. It has its own lesson in part 3.
Quick check
How it is done for real
Training a large language model is gradient descent with a few practical additions you will see in every paper.
- Batches. Computing the loss over all the training text before every step would take forever. Instead each step uses a small random sample, a batch (often millions of tokens for big models). The slope is a little noisy, but steps are cheap, so you can take many. This is stochastic gradient descent; stochastic means random.
- Learning-rate schedules. Training starts with a gentle warm-up (tiny steps while things settle), then uses its largest rate, then lowers it slowly towards the end so the model settles into a good valley.
- Optimizers. Smarter versions of the update rule. Momentum lets the steps build up speed like a ball rolling downhill. Adam (2014) and its variant AdamW give every weight its own step size, based on how its slopes have behaved; AdamW has been the standard for years.
- Muon. A newer optimizer that adjusts each weight matrix as a whole rather than weight by weight. Moonshot’s 2025 study found it about twice as compute-efficient as AdamW. Kimi K2 and DeepSeek-V4 were trained with it.
Remember
- The loss is one number measuring how wrong a model is. Learning means changing the weights to make it smaller.
- A slope (derivative) says how the loss changes when you nudge a number. Step against it to go downhill.
- Gradient descent: new weight = weight − learning rate × slope, repeated many times. All the slopes together are the gradient.
- Too small a learning rate is slow; too big overshoots or blows up. You can also get stuck in a local minimum.
- Real training uses random batches, a warm-up and decay schedule, and optimizers such as AdamW and Muon.
Words to know
- Loss
- One number that measures how wrong a model’s answers are. Training tries to make it smaller.
- Slope (derivative)
- How fast the loss changes when you nudge one number. Its sign tells you which way is downhill.
- Gradient
- The slopes for all the weights at once, as one long vector.
- Gradient descent
- Learning by repeatedly nudging every weight a little against its slope.
- Learning rate
- How big each step is. One of the most important settings in training.
- Local minimum
- A valley that is not the deepest one, but where every direction looks uphill.
- Batch
- The small random sample of training examples used for one step.
- Optimizer
- The exact rule for turning slopes into weight updates: plain gradient descent, momentum, AdamW, Muon.
- Loss spike
- A sudden jump in the loss during training, a sign that something went unstable.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
An overview of gradient descent optimization algorithms
A friendly tour of momentum, Adam and the rest, with pictures. The best next step after this lesson.
-
Adam: A Method for Stochastic Optimization
The optimizer behind most of the last decade of AI. Algorithm 1 is half a page.
-
Decoupled Weight Decay Regularization (AdamW)
The small change to Adam that became the standard for training language models.
-
Muon is Scalable for LLM Training
Moonshot’s report on the newer optimizer that trained Kimi K2. Read it after the matrices lesson has settled in.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonProbability: how models express doubt