How models learn: the journey from numbers to intelligence
We have seen how a transformer is built, how text is tokenized, and how the data is prepared. But there is a fundamental question we still have not answered: how does a model go from being a pile of random numbers to generating coherent text?
The answer combines three ingredients: a loss function that quantifies how wrong the model is, an optimization algorithm (gradient descent) that adjusts the parameters to reduce that loss, and a computation rule (backpropagation) that assigns “blame” across every parameter efficiently.
In this chapter we open this black box and watch, step by step, how a model learns. Spoiler: it is not magic, it is differential calculus applied at massive scale.
A model is, literally, a list of numbers
A language model is not a program with hand-written rules. It is a list of numbers — hundreds of billions of numbers — called parameters or weights.
“Training a model” means changing those numbers. The architecture (the code that defines how the layers connect) stays exactly the same. Training never rewrites the code; it only turns the knobs.
Analogy: imagine an old radio with many knobs — volume, treble, bass, balance. The model is the radio; the parameters are the knob positions. At the start, the knobs are in random positions and the radio plays static. Training is the process of turning the knobs again and again until the radio sounds right. We do not change the circuit (the architecture); we only adjust the knobs.
A checkpoint is a “snapshot” of all those numbers at a given moment. The trainer writes to disk: the weights, the optimizer state (so it can resume exactly where it left off), and metadata (current step, model shape, tokenizer, data source).
The loss function: how do we measure “how wrong” the model is?
To know which way to turn the knobs, we need an error metric: cross-entropy loss.
Intuition: loss as surprise. When the model makes a prediction, the loss measures how surprised it was by what actually came next. If it put a lot of probability on the correct word, its surprise is low (small loss). If it put little, its surprise is high. If it put probability zero on the correct word, the loss would be infinite — that is why models never assign exactly zero probability to any token.
A concrete example: after “the little cat”, the model predicts:
| Word | Probability |
|---|---|
| ran | 32% |
| saw | 24% |
| found | 20% |
| slept | 16% |
| banana | 8% |
The real word was “found” (20% probability). The loss:
Why a logarithm? Because the loss must be additive (the total loss of a sequence is the sum of the losses at each position) and sensitive to changes near zero (going from 1% to 2% is a large relative change — you double the probability — and the loss must reflect that). The logarithm does both things naturally.
Comparing changes:
| Correct probability | Loss | Change |
|---|---|---|
| 90% → 95% | 0.105 → 0.051 | -0.054 (small improvement) |
| 50% → 55% | 0.693 → 0.598 | -0.095 (moderate improvement) |
| 10% → 15% | 2.303 → 1.897 | -0.406 (large improvement) |
| 2% → 0.5% | 3.912 → 5.298 | +1.386 (large worsening) |
The curve is asymmetric: making a serious error (giving very little probability to the correct word) is much more costly than getting slightly better at being right. That asymmetry is deliberate — we want the model to avoid, at all costs, assigning near-zero probability to the correct word.
The formula, for the whole sequence:
Where N is the total number of tokens in the batch, and p_θ is the probability the model assigns to the correct token given the context.
Connection to information theory: this loss is exactly Shannon’s “surprise”, averaged. That is why minimizing the loss is equivalent to compressing the text — a model that predicts text well also compresses it well. This is not a metaphor: information theory and machine learning are joined by this same equation.
An important consequence: because the loss is an average over your text, the model becomes good at predicting exactly what you showed it. Show it junk, and it will learn, with the same diligence, to predict junk.
Rolling downhill: gradient descent
Imagine the loss as a mountain landscape. Each point corresponds to a combination of all the parameters (millions of dimensions); the height at each point is the value of the loss. The goal: find the lowest point — the valley where the loss is minimal.
We cannot see the whole landscape, but we can feel its slope at the point where we are standing — the gradient tells us, for each parameter, whether increasing its value increases or decreases the loss, and by how much.
The algorithm: compute the gradient (the partial derivative of the loss with respect to each parameter), take a step in the opposite direction (subtract a fraction of the gradient from each parameter), and repeat.
Where η (eta) is the learning rate: the size of the step.
A manual exercise: learning with 5 words
Back to the example of predicting the word after “the little ___”. Data:
| Next word | Frequency |
|---|---|
| cat | 6 |
| dog | 3 |
| bird | 1 |
| ball | 1 |
| star | 1 |
The model has 5 logits (unnormalized scores), one per word. The best possible model (the one that reproduces the frequencies exactly) would be: cat → 6/12 = 50%, dog → 3/12 = 25%, and the other three → 1/12 ≈ 8.33% each.
The lowest possible loss (the entropy of the empirical distribution):
This is the fundamental limit: even if the model were perfect, it could not go below 1.314 — there is real uncertainty in the data, we do not know for sure whether “cat” or “dog” will come next.
Gradient descent adjusts the 5 knobs (logits) so the probabilities approach these proportions. With only 5 knobs it is already tedious to do by hand; imagine doing it with the 9 million parameters of our small model, with no answer key.
Historical note: gradient descent was not invented for AI — Augustin-Louis Cauchy proposed it in 1847 to solve astronomy problems. What is new is applying it to millions of parameters at once, in parallel, on specialized hardware.
The learning rate: the art of the right step
The learning rate (η) is one of the most important training parameters — it determines the size of the step at each iteration.
- Too small: the model learns slowly, exploring the landscape with ant-sized steps — it can take far longer than necessary to converge.
- Too large: the model takes giant steps, skipping over valleys and possibly diverging (the loss explodes).
- Just right: it descends in a stable way, reasonably fast.
In practice, the learning rate warms up at the start — it begins small and grows gradually — and then decays toward the end of training. That lets you explore the landscape broadly at the beginning, and refine the solution with precision at the end.
Backpropagation: computing the gradient for millions of knobs at once
If we had to compute the gradient with respect to each parameter one by one, the cost would be astronomical — for a 9-million-parameter model, each step would require 9 million evaluations. Impossible.
Backpropagation solves this using the chain rule from differential calculus:
- Forward pass: we compute the loss, saving every intermediate operation (activations, attention scores, and so on).
- Backward pass: we walk the sequence in reverse, computing the derivatives of the loss with respect to each intermediate operation, and then with respect to each parameter — like a wave of “blame” that propagates from the output back to the input, distributing responsibility for the error across every parameter.
Efficiency: the backward pass costs roughly twice a forward pass — a full training step (forward + backward + update) costs around 3 times a forward pass. A reasonable price for getting the gradient of millions of parameters at once.
Backpropagation was formulated in its modern form by Rumelhart, Hinton, and Williams in 1986. Today, frameworks like PyTorch or MLX compute it automatically — the programmer only defines the model and the loss.
The full training loop, step by step
1. Select a batch of tokens from the training set.
2. Forward pass: feed the batch to the model → get logits.
3. Compute the loss (cross-entropy) by comparing the logits with the real tokens.
4. Backward pass: compute the gradients of the loss with respect to every parameter.
5. Update the parameters: w ← w - η × gradient (or with an optimizer like AdamW).
6. Repeat from step 1.
Each iteration is called a training step. The batching trick: instead of processing one example at a time, you process batches of 32, 64, or 512 examples simultaneously — this takes advantage of hardware parallelism, and gradients averaged over the batch are more stable than those from a single example.
Watching a tiny model learn, letter by letter
To get a feel for what learning looks like, imagine a microscopic model: a 27×27 table (729 parameters) that predicts the next letter. It is not a transformer, but the learning process is the same.
At the start, the weights are random — the loss opens at 3.296 (the surprise of guessing at random among 27 letters). As training proceeds:
| Step | Generated sample |
|---|---|
| 0 | "ux vsmiorzl gepbhi wffwddgbjwcqvuldrajujbqwdkrrsbfyghl sqvfx skkyzgzem" |
| 40 | "t s kintu wathe cjjlbjuman tbcwqaux t wnvces wibibo he i ithe sarjmy i" |
| 200 | "t gpplan o bke med athe id f thaill pre jbathkil alisaronighitrfomkqse" |
| 800 | "t py igr thand a d f t gry are n tom ckumomaly a pld bi s llaindotap l" |
| 2,400 (best possible) | "tlay i so ppllamist s rrind t sanist s may ge ilo pillo t d nghicharm" |
The loss falls from 3.296 until it settles on a floor of 2.073. Watch the progression: first the model discovers spaces and approximate letter frequencies (the static starts arriving in bursts the size of words); then strong pairs emerge — th, he, an — and the bursts start to sound pronounceable. Even at its best point (step 2,400), it is still basically gibberish — it never generates real words, let alone a coherent sentence. What it does achieve is junk with the unmistakable statistical texture of English.
For a human-made version of this same effect, there is the Italian song “Prisencolinensinainciusol” by Adriano Celentano — whose lyrics, with no real meaning, were written deliberately to sound like spoken English. That is exactly what this little model produces: the shape without the substance.
The final floor (2.073) is the irreducible uncertainty of predicting the next letter with only 1 letter of context — with more context (for example, a transformer with a window of hundreds of tokens), the model can push its loss floor much lower, because it has far more information available for each prediction.
Key observation: the model does not “understand” what a word is. It simply learned, statistically, that after “th” there usually comes “e” or “a”, and that certain letters form recurring patterns. Meaning — when it appears, in much larger models — emerges from the combination of a great many patterns like this, not from real understanding.
Learning, memorization, and validation: the overfitting trap
A crucial question: when the loss falls, is the model learning general patterns, or simply memorizing the training examples?
The answer is in the validation curve. You hold out a small set of data (5-10% of the total) that is never used to update parameters — the validation set — and you evaluate on it periodically.
- If training and validation fall together: the model is learning general patterns that apply to unseen text.
- If training keeps falling but validation stalls or rises: the model is overfitting — memorizing specific details of the training set instead of learning general rules.
The optimal stopping point is usually when validation loss reaches its minimum — from there, training loss can keep falling (because the model memorizes more), but validation gets worse.
In practice, with large models trained on massive datasets, immediate overfitting is less common (there is so much data that memorizing becomes hard, and regularization techniques like weight decay and dropout are used) — but the gap between training and validation is still an important metric to watch.
Modern optimizers: AdamW
Classic gradient descent, with a fixed learning rate for every parameter, has limitations. In practice we use adaptive optimizers — AdamW is the current standard:
- It keeps a moving average of the gradient (the “momentum”) — it smooths oscillations and speeds progress in consistent directions.
- It keeps a moving average of the square of the gradient — it adjusts the learning rate per parameter: if a parameter has large, variable gradients, it shrinks its step; if they are small and stable, it increases it.
- It adds weight decay: a small penalty on large parameters, acting as regularization.
Under the hood (simplified):
# AdamW pseudocode
m = 0 # gradient average
v = 0 # squared-gradient average
for step in 1..num_steps:
g = compute_gradient(loss, w)
m = beta1 * m + (1 - beta1) * g
v = beta2 * v + (1 - beta2) * g**2
m_hat = m / (1 - beta1**step) # bias correction
v_hat = v / (1 - beta2**step)
w = w - eta * (m_hat / (sqrt(v_hat) + epsilon) + lambda_ * w)
Where beta1, beta2, epsilon, and lambda_ are hyperparameters (typically 0.9, 0.999, 1e-8, and 0.01-0.1 respectively).
The loss landscape: local minima, saddle points, and flats
A common question: “doesn’t gradient descent get stuck in local minima?”
In modern neural networks, local minima are usually not the main problem. In high-dimensional spaces (millions of parameters), you are more likely to find saddle points (where the slope is zero in some directions but not others) or plateaus (regions where the loss is almost flat).
The combination of random initialization (the model starts at a different point each time), optimizers with momentum like AdamW (which can “push through” plateaus and saddle points), and the massive scale of current models (whose loss landscape tends to be smoother, with fewer sharp local minima), means the model almost always finds reasonably good solutions — with no guarantee of global optimality, but enough in practice.
Chapter summary
Training a language model is a cyclical process:
- Random initial parameters — the model knows nothing.
- Forward pass — it computes the loss, how surprised it is by the real text.
- Backward pass — it computes the gradient of the loss with respect to each parameter.
- Update — with an optimizer (AdamW), it adjusts the parameters to reduce the loss.
- Repeat — the model refines its statistical understanding of language, step by step.
The loss measures the model’s “surprise”. Gradient descent minimizes that surprise by adjusting millions of knobs. Backpropagation makes this process computationally viable. The modern optimizer (AdamW) makes it stable and fast.
What the model learns is not “meaning” in the human sense, but a probability distribution over token sequences that resembles that of the training data. Meaning, coherence, reasoning — all of that emerges from the model’s ability to predict text well, not from a separate “understanding” mechanism.
In the How models learn behavior chapter we will see how this purely statistical learning can be shaped so the model behaves like a conversational assistant. The difference between a base model (which only continues text) and an assistant like ChatGPT is not in the architecture or the learning algorithm — it is in post-training: instruction fine-tuning and preference tuning. That is what turns a token predictor into a useful assistant.
If you ever feel overwhelmed by the math, remember the radio-and-knobs analogy: training is turning knobs millions of times, using the gradient as a guide. There is no “intelligence” in the process itself — only a well-defined optimization algorithm. The apparent intelligence is a byproduct of scale and data quality.
LinkedIn nelson.zepeda@simov.io SIMOV LABS
Found an error? Suggest a correction