Training

Two-panel comic titled 'Target Practice': 'Day 1' shows a robot archer missing the bullseye target labeled 'Correct Word', with the coach yelling 'Missed again! Adjust!'; 'Day 10,000,000' shows the same target packed with arrows hitting dead center, a mountain of missed arrows on the ground, and the coach giving a thumbs up saying 'Perfect!'
What You’ll Learn

You’ll learn how transformers train by predicting the next token at every position in parallel. You’ll also see how cross-entropy loss, teacher forcing, backpropagation, residual connections, and optimizer updates turn wrong guesses into better weights.

You’ve seen the architecture: attention gathers context, the FFN processes it, residual connections and layer norm keep things stable. But a freshly initialized transformer has random weights. Ask it to complete “The cat sat on the” and it might say “purple” or “seventeen.” Every weight is noise, and every prediction is a coin flip across a 50,000-word vocabulary. The model needs a signal telling it how wrong it is at every position, millions of times over, until the noise becomes language.

The Training Objective: Next-Token Prediction

The learning process boils down to one objective: given all previous tokens, predict the next one. Think of it as a fill-in-the-blank test where every position in the sentence is a blank, and the model is graded on all of them simultaneously. It turns out that mastering next-token prediction, at scale, teaches a model grammar, facts, reasoning, and even code. Why? Because to predict well across all domains, the model must learn what words mean and how they relate to each other.

Given all previous tokens, predict the next one.

The model decomposes the probability of a full sentence into a chain: the probability of each word given all the words before it.

Train Together, Generate One at a Time

Play the 3 rounds below to compare parallel training with one-by-one generation:

Round 1 · Parallel Training (1 Click) Click “⚡ Predict all 4 positions in parallel” below.
Known in the training text 5 / 5 tokens
Token 1 The
Token 2 code
Token 3 is
Token 4 blue
Token 5 .
Can read (Causal prefix) Top guess Target (loss check)
0 / 4 guesses

The Loss Function

How do we tell the model how wrong it is? We need a single number that says “your prediction was this far off.” Drag the dot on the curve below to see how that number behaves:

Cross-Entropy Loss

P(correct) = 50% loss = 0.69
1234500%25%50%75%100%Probability on correct tokenLossUntrained (8%): 2.53Trained (70%): 0.36

Drag anywhere on the chart. Low probability on the correct token (left) means huge loss. High probability (right) means tiny loss. The penalty grows explosively as probability drops toward zero.

What to look for: Drag the dot to 1% and watch the loss shoot past 4. Drag it to 90% and the loss is barely 0.1. The curve is not a straight line: the penalty for being almost certain and wrong is dramatically worse than being slightly uncertain. The two reference points show where an untrained model (8%, loss 2.53) and a trained model (70%, loss 0.36) sit on the curve.

This is cross-entropy loss: the negative log of the probability the model assigned to the correct next token. Here’s a concrete example. The model predicts after “The cat sat on the”:

CandidateModel’s probabilityCorrect?
“mat”8%✓
“floor”12%
“bed”10%
“table”7%

The loss at this position: −log⁡(0.08)=2.53-\log(0.08) = 2.53. That’s a strong penalty, because the model wasn’t very confident about the right answer. If it had assigned 70% to “mat,” the loss would be just −log⁡(0.70)=0.36-\log(0.70) = 0.36. You can verify both on the interactive curve above.

The total loss averages this across all positions in the sequence.

Quick Check
A model assigns 90% probability to the correct next token (loss = 0.11). After a bad weight update, it now assigns only 1% to the correct token. What is the new loss?
Your turn
Choose an answer to check your understanding.

Teacher Forcing

If we train the model on its own predictions and it makes a mistake early, that error cascades. Step through the comparison below to see how one wrong prediction at position 1 snowballs:

Teacher Forcing vs Free-Running

Position 1: predict "cat"
Target: Thecatsatonthemat
✓ Teacher Forcing
The
→ "dog" 15%
✗ Free-Running
The
→ "dog" 15%

Both modes see the same input and make the same (wrong) prediction: "dog" instead of "cat." With teacher forcing, this mistake won't affect the next position.

What to look for: At position 1, both tracks are identical. By position 3, the free-running track has drifted to a completely different sentence. By position 5, four of five context tokens are wrong (red), and the model is learning from garbage. Teacher forcing stays clean at every step because it always uses the ground-truth context.

Teacher forcing means: at each position, always feed the correct previous tokens as context, even if the model’s last prediction was wrong. Think of it as studying with an answer key: you see the correct answer to the previous problem before moving to the next one, so you never have to recover from your own mistakes. (Unlike a real answer key, the model never practices recovering from errors during training, so at inference time it encounters input patterns it never saw. This gap is called exposure bias.)

Context usedRisk
Teacher forcingAlways real tokensNone (clean signal)
Free-runningModel’s own predictionsErrors compound at every step

In practice, models trained with teacher forcing still generate well, because each position learned from a clean signal. The exposure bias gap turns out to be small enough that it rarely causes problems at scale.

Fill in the Blanks
During training, transformers use forcing, which means each position receives the previous tokens as context instead of the model's own predictions. This prevents errors from .
Fill in every blank, then check your answer.

The Training Loop

Training follows the standard deep learning cycle. Step through the visualization below to see each phase with concrete numbers:

Training Step Walkthrough

The cat sat on the mat
Step 1: Forward Pass

Predicted probability distributions at all 5 positions.

Position 4 "The cat sat on the"
predict: mat
mat
13.1%
floor
17.7%
bed
16.8%
cat
13.8%
dog
12.5%
All 5 positions predicted in parallel, not one at a time. Click any position to see its full distribution.
1 / 4

What to look for: Step through all four phases. Each position predicts its next token, and the loss measures how much probability went to the correct answers. Backpropagation computes how each parameter affects that loss. The animation illustrates an update that improves every position; in a real batch, shared parameters can help some predictions and hurt others. Track the average training loss and a separate validation loss over many updates.

The four steps in every training iteration:

StepWhat Happens
Forward passRun all tokens through the model in parallel, get predictions at every position
Compute lossCross-entropy between each prediction and its target, then average
Backward passCompute gradients via backpropagation (how much each weight contributed to the error)
Update weightsOptimizer nudges parameters to reduce the loss

Backpropagation: How Error Becomes Learning

The loss tells us how wrong the model is. Backpropagation tells us who’s responsible. For each of the model’s millions of weights, we need to answer one question: “If this weight were slightly larger, would the loss go up or down, and by how much?” That answer is the gradient, and computing it for every weight is what backpropagation does.

Passing Blame Backward

The model is a chain of operations: embedding → attention → FFN → layer norm → attention → FFN → … → output projection. To find each weight’s gradient, backpropagation works backward from the loss, asking each layer: “How much did your output change when your input changed?” Then it multiplies these local effects together. This is the chain rule of calculus applied one layer at a time.

A concrete example: in the Training Loop visualization above (Step 3), position 4 has the highest loss because the model predicted “floor” instead of “mat.” Backpropagation traces this error backward:

  1. Output layer: “Increase the logit for ‘mat,’ decrease it for ‘floor’”
  2. Final FFN: Adjust the weights that would have boosted the “mat” representation in this context
  3. Final attention layer: The attention weights that chose which earlier positions to focus on get adjusted, so the model attends more to “sat on” (which strongly predicts “mat”)
  4. Earlier layers: Smaller adjustments ripple back, each refining how the model represents this input pattern
  5. Embeddings: Even the token embeddings get a tiny nudge, adjusting what “on” and “the” mean to the model

Step through the full forward and backward pass for this example:

Forward & Backward Pass

Forward →
EmbedAttention(+res)FFN(+res)OutputLoss
1/8 Embedding
The →
0.1 -0.5 0.8
cat →
0.2 -0.3 0.7
sat →
0.4 -0.1 0.6
on →
0.5 0.1 0.5
the →
0.7 0.3 0.4
d=768 in real models, shown as 3 for illustration

Each token is converted to a dense vector. These vectors are the model's starting representation of each word.

What to look for: In the forward phase (steps 1-4), watch data flow left to right through the pipeline, ending at a loss of 1.77. Then in the backward phase (steps 5-8), gradients flow right to left. At steps 6 and 7, notice how the residual connection splits the gradient into two paths: one through the layer (updating weights) and one through the skip connection (preserving signal strength). By step 8, even the earliest embeddings receive a gradient.

All five positions do this simultaneously. Their gradients are summed to produce one set of weight updates for the whole model.

Why Transformers Don’t Have Vanishing Gradients

If you’ve seen RNN backpropagation, you know the problem: gradients flow through every timestep sequentially, getting multiplied by the same weight matrix at each step. After enough multiplications, the signal shrinks to near zero. The model can’t learn long-range dependencies. (See Vanishing Gradients for the full story.)

Transformers avoid this in two ways:

  • No temporal unrolling. Every position computes its prediction independently, so there’s no chain of timesteps to propagate through. Position 1’s gradient doesn’t need to flow through positions 2, 3, 4, 5.
  • Residual connections create gradient highways. The residual connection output=layer(x)+x\text{output} = \text{layer}(x) + x has a gradient of just 1 through the addition. Even if a layer’s internal gradients are small, the skip-connection path carries the error signal to earlier layers at full strength. This is why transformers can stack 96+ layers without gradients vanishing.

Toggle between the two modes below to see the difference. Then try increasing the layer count:

Gradient Flow Through Layers

Loss
1.00
Layer 6
1.05
Layer 5
1.00
Layer 4
1.02
Layer 3
1.00
Layer 2
1.02
Layer 1
0.94
Embed.
1.01
Healthy gradients: Each layer's residual path (+1) keeps the signal at full strength. Embedding gradient: 1.01. Every layer learns effectively.

Toggle modes and drag the slider to add layers. Real transformers stack 12 to 96+ layers, making residual connections essential.

What to look for: With residuals, every bar stays near 1.0, even at the bottom. Switch to “Without” and watch the bars shrink layer by layer. Increase to 12 layers: with residuals the bars barely change, but without them the embedding gradient drops to near zero. This is why deep transformers require residual connections.

RNNTransformer
Gradient flowSequential through time (BPTT)Parallel across positions
Vanishing riskHigh (same matrix reused at each step)Low (residual connections)
Practical depth~5-10 layers96+ layers
Quick Check
A 48-layer transformer has residual connections around every sublayer. If we removed all residual connections, what would most likely happen?
Your turn
Choose an answer to check your understanding.

Optimization

Gradients point each weight in the right direction, but how far should it move? Three problems arise with a naive approach (plain gradient descent with a fixed step size):

  1. One size doesn’t fit all. Some parameters need large updates, others need tiny nudges. A single learning rate can’t serve both.
  2. Gradients are noisy. Each batch is a small sample of the data. One batch might push a weight left; the next pushes it right. Following every fluctuation wastes steps.
  3. Weights can grow unchecked. Without a constraint, some weights balloon to extreme values, and the model memorizes the training data instead of learning general patterns.

AdamW solves all three. It’s the standard optimizer for transformer training:

  • Adaptive learning rates per parameter. Parameters with historically large, volatile gradients get smaller steps (to avoid overshooting). Parameters with consistently small gradients get larger steps (to make faster progress). Each of the model’s millions of parameters gets its own effective step size.
  • Momentum smooths out noise by averaging gradients over recent steps. One bad batch can’t throw the optimizer off course, just like averaging poll results is more reliable than any single poll.
  • Weight decay adds a gentle pull toward smaller weights at every step. This prevents overfitting by discouraging any single weight from growing too large. (Unlike classic L2 regularization, AdamW applies decay directly to the weights, not to the gradients, which turns out to generalize better.)

Learning Rate Schedule

Even with AdamW’s per-parameter adaptation, the global learning rate follows a planned schedule. Drag across the chart to see how it changes over 100,000 training steps:

Learning Rate Schedule

Step 30k  LR 5.0e-4  Cosine Decay
0246025k50k75k100kTraining stepLR (×10⁻⁴)Peak: 6×10⁻⁴Cosine Decay

Drag anywhere on the chart. The warmup ramp (first 4k steps) prevents wild updates from noisy early gradients. Cosine decay smoothly reduces the rate for fine-grained convergence.

What to look for: Drag to the far left and watch the learning rate ramp from zero during warmup (the first ~4,000 steps). Then drag right and watch cosine decay smoothly bring it back down. At step 50k, the rate is about half its peak. By 100k, it’s near zero. The shape is the key: ramp up fast, come down slow.

PhaseWhat HappensWhy
Warmup (~4,000 steps)Linearly ramp LR from 0 to peakEarly gradients are noisy and AdamW’s momentum estimates haven’t stabilized; large steps would destabilize training
PeakMaximum learning rate (e.g., 6×10⁻⁴)Fastest learning, once gradient statistics are reliable
Cosine decaySmoothly decrease following cos(x) to near 0Fine-grained convergence as the model approaches optimal weights
Quick Check
Why does the learning rate start at 0 and gradually increase (warmup) rather than starting at the peak value?
Your turn
Choose an answer to check your understanding.

The Full Training Pipeline

This table describes training at a large scale. Your first experiment in Train Your Own GPT uses a small bundled corpus, a character vocabulary, and a short training run. It also holds out validation text before making training windows, so you can measure performance on text the optimizer never sees.

StepAction
1Collect massive text corpus (books, web, code…)
2Tokenize entire corpus (BPE with ~50K vocabulary)
3Split into batches of sequences (e.g., 2048 tokens each)
4For each batch: forward, loss, backward, update
5Repeat for millions of steps
6Result: a model that can predict next tokens

Quick Summary

ConceptWhat HappensIntuition
Next-token predictionAt every position, predict the next tokenOne simple objective that teaches everything
Cross-entropy loss−log⁡P(correct token)-\log P(\text{correct token})Confident and wrong = huge penalty
Teacher forcingAlways use real tokens as contextStudy with the answer key, not your own mistakes
BackpropagationChain rule traces error backward through every layerBlame assignment: who caused the mistake?
Training loopForward, loss, backward, updateStandard gradient descent, at enormous scale
LR scheduleWarmup, peak, cosine decayStart gentle, go fast, then fine-tune

Learning Checklist
  • I can explain the next-token prediction objective and why all positions train in parallel
  • I understand cross-entropy loss and why confident-but-wrong predictions are penalized most
  • I understand teacher forcing and why it prevents error compounding
  • I can describe the four steps of the training loop: forward, loss, backward, update
  • I can explain how backpropagation computes gradients and why residual connections are essential for deep transformers
  • I know what exposure bias is (the mismatch between teacher forcing and free-running inference)

Next: Inference

The model is trained. How does it actually generate text, one token at a time? Inference →