Chapter 11: Training the Transformer (The Learning Process)

By ·

Chapter 11: Training the Transformer (The Learning Process)

Now for the magic: how do those random weights become intelligent?

The Training Data

Imagine we have a massive text corpus:

"The cat sat on the mat. The dog ran in the park..."
(Billions of words from books, websites, conversations)

We chunk this into training examples:

Input: "I love" → Target: "pizza"
Input: "The cat sat on the" → Target: "mat"
Input: "Machine learning is" → Target: "fascinating"

Hyperparameters for training:

  • Batch size: 32 (process 32 examples at once)
  • Learning rate: \eta = 0.0001 (how big our weight updates are)
  • Epochs: 10 (how many times we see the entire dataset)

The Loss Function (Cross-Entropy)

After the model predicts probabilities, we compare to the actual next word using cross-entropy loss.

The "Hot and Cold" Game Analogy

Remember playing "Hot and Cold" as a kid?

Your friend hides a toy somewhere in the room. You walk around searching:

  • Walk toward the closet → "COLD! ❄️" (you're far away)
  • Walk toward the window → "GETTING WARMER! 🌡️" (getting closer)
  • Walk toward the desk → "HOT! 🔥" (very close!)
  • Reach under the desk → "BURNING! 🔥🔥🔥" (almost there!)
  • Grab the toy → "YOU FOUND IT! 🎉" (perfect!)

The feedback tells you how far you are from the goal:

  • "Cold" = large distance = needs big changes
  • "Hot" = small distance = needs small adjustments
  • "Found it" = zero distance = done!

The loss function works EXACTLY like this!

  • Model guesses: "The next word is probably 'the'"

  • Correct answer: "pizza"

  • Loss function: "COLD! You're way off!" (high loss number like 4.5)

  • Model guesses: "The next word is probably 'pasta'" (closer!)

  • Correct answer: "pizza"

  • Loss function: "WARMER! Getting better!" (medium loss like 1.8)

  • Model guesses: "The next word is probably 'pizza'" (correct!)

  • Correct answer: "pizza"

  • Loss function: "HOT! Almost perfect!" (low loss like 0.1)

The loss number tells the model how far it is from the correct answer!

The "Are We There Yet?" Road Trip Analogy

Another way to think about loss: You're driving to grandma's house 500 miles away.

Beginning of trip (mile 10):

  • Distance remaining: 490 miles (HUGE!)
  • You: "Are we there yet?"
  • Parent: "No! We just started! Still very far!" (high loss like 6.9)

Middle of trip (mile 250):

  • Distance remaining: 250 miles (medium)
  • You: "Are we there yet?"
  • Parent: "We're halfway! Getting there..." (medium loss like 2.3)

Near the end (mile 495):

  • Distance remaining: 5 miles (small!)
  • You: "Are we there yet?"
  • Parent: "Almost! Just a few more minutes!" (low loss like 0.2)

Arrived (mile 500):

  • Distance remaining: 0 miles
  • You: "Are we there yet?"
  • Parent: "Yes! We're here!" (zero loss = perfect!)

The loss measures how far you are from your destination (the correct answer).

During training:

  • Start: Loss = 8.5 (very far from good predictions)
  • After 1000 steps: Loss = 5.2 (getting warmer!)
  • After 10,000 steps: Loss = 2.1 (much closer!)
  • After 100,000 steps: Loss = 0.8 (almost there!)
  • After 1,000,000 steps: Loss = 0.3 (very good!)

You can track training progress by watching loss decrease—like watching the mile markers get closer to your destination!

The Mathematics of Loss

Now that we understand the intuition, let's see the actual formula:

Formula:

L = -\log(P(\text{correct token}))

Breaking it down:

Why logarithm? The logarithm creates a perfect "distance" metric:

When predictions are good (high probability):

  • P(\text{correct}) = 0.9 → L = -\log(0.9) = 0.105 ✓ Small loss!
  • P(\text{correct}) = 0.99 → L = -\log(0.99) = 0.010 ✓ Even smaller!
  • P(\text{correct}) = 0.999 → L = -\log(0.999) = 0.001 ✓ Tiny loss!

When predictions are bad (low probability):

  • P(\text{correct}) = 0.1 → L = -\log(0.1) = 2.303 ✗ Large loss!
  • P(\text{correct}) = 0.01 → L = -\log(0.01) = 4.605 ✗ Huge loss!
  • P(\text{correct}) = 0.001 → L = -\log(0.001) = 6.908 ✗ Massive loss!

The logarithm naturally creates this scaling:

Probability:  0.999  0.99  0.9  0.5  0.1  0.01  0.001
Loss:         0.001  0.01  0.1  0.7  2.3  4.6   6.9

See the pattern? As probability drops, loss SHOOTS UP! This harshly penalizes bad predictions.

Why the negative sign?

Logarithms of numbers between 0 and 1 are negative:

  • \log(0.9) = -0.105 (negative!)
  • \log(0.01) = -4.605 (very negative!)

We add the negative sign to make loss positive (easier to interpret):

  • -\log(0.9) = -(-0.105) = +0.105 ✓
  • -\log(0.01) = -(-4.605) = +4.605 ✓

Loss going down = getting better! (Like "Hot and Cold" - getting hotter means getting closer!)

Concrete example:

Our sentence: "I love pizza" Model's task: Predict the word after "love" Correct answer: "pizza" (token 999)

Model's predicted probabilities:

P(token 999 "pizza") = 0.012 (1.2%)
P(token 1234 "the") = 0.234 (23.4%)
P(token 3456 "is") = 0.089 (8.9%)
... (sum of all 50,000 = 1.0)

Loss calculation:

\begin{align} L &= -\log(P(\text{pizza})) \\ &= -\log(0.012) \\ &= -(-4.423) \\ &= 4.423 \end{align}

High loss = bad prediction! The model was very uncertain about "pizza".

If the model predicted correctly: L = -\log(0.85) = 0.163 \quad \text{(much lower!)}

Why Cross-Entropy?

Cross-entropy has several mathematical properties that make it perfect for training:

  1. Heavily penalizes confident wrong predictions: If the model says "purple" has 90% probability (very confident) but the correct answer is "pizza," the loss is enormous. This strongly discourages confident mistakes.

  2. Encourages high probability on correct tokens: The only way to minimize loss is to put as much probability as possible on the correct answer. The model is rewarded for being confidently correct.

  3. Differentiable everywhere: We can compute gradients at any point, which is essential for backpropagation. The gradient gives us a direction to improve.

  4. Well-behaved at the prediction layer: For a fixed set of logits, cross-entropy gives clean gradients that push probability mass toward the correct answer. That's exactly what we want at the prediction step.

Important nuance: Once this loss is composed with a deep transformer full of learned weights, the overall training problem is still non-convex. So training a real transformer is NOT a simple single-bowl optimization problem. Cross-entropy helps, but it doesn't magically make deep learning easy.

Think of it like a grading system that's tough but fair. If you guess "The cat eats cars" (nonsensical), you get a huge penalty. If you guess "The cat eats mice" (reasonable but not the exact answer "fish"), you get a smaller penalty. If you guess "The cat eats fish" (exact match), you get almost no penalty. The scoring system naturally teaches you to make sensible predictions.

Batch Loss

What is a batch? Instead of processing one sentence at a time, we process multiple sentences simultaneously. This is more efficient and provides more stable gradient estimates.

Batch size = 32 means we process 32 different training examples together:

Batch example:
1. "I love" → target: "pizza"
2. "The cat sat on the" → target: "mat"
3. "Machine learning is" → target: "fascinating"
4. "Python is a" → target: "language"
...
32. "Transformers revolutionized" → target: "AI"

We compute loss for each example separately, then average them:

L_{\text{batch}} = \frac{1}{32} \sum_{i=1}^{32} L_i

Concrete calculation:

If our batch contains:

  • Example 1: Loss = 4.423 (model very uncertain)
  • Example 2: Loss = 2.156 (model somewhat confident)
  • Example 3: Loss = 3.891
  • Example 4: Loss = 1.245
  • ... (28 more examples)
  • Example 32: Loss = 1.987
\begin{align} L_{\text{batch}} &= \frac{4.423 + 2.156 + 3.891 + 1.245 + \ldots + 1.987}{32} \\ &\approx 3.127 \end{align}

Why batch instead of one-by-one?

  1. Faster: GPUs excel at parallel computation—32 examples at once is much faster than 32 sequential
  2. More stable gradients: Averaging over 32 examples gives a better estimate of the true gradient direction
  3. Better generalization: Model sees more variety before updating weights

Backpropagation (The Learning Engine)

Now we need to answer the critical question: "How do we know which weights to adjust and by how much?"

This is done through backpropagation—the learning engine of neural networks!

The Blame Game Analogy

Imagine a restaurant with a bad review:

Customer review: "The food was terrible!" (high loss!)

Who's responsible? Let's trace backward:

Step 5 (Final): Waiter served the food

  • Waiter's fault: 5% (just delivered it)

Step 4: Chef cooked the food

  • Used the Recipe ingredient amounts
  • Chef's fault: 25% (followed recipe but technique was off)

Step 3: Recipe had wrong proportions

  • Said "10 tablespoons salt" (way too much!)
  • Recipe's fault: 60% (main culprit!)

Step 2: Supplier provided ingredients

  • Ingredients were fine quality
  • Supplier's fault: 5%

Step 1: Restaurant owner hired everyone

  • Made some poor choices
  • Owner's fault: 5%

Backpropagation does EXACTLY this! It traces backward through all the layers to figure out "which weights contributed most to the error?"

The Chain Rule (Understanding Cascading Effects)

What is the chain rule? It's about understanding cascading effects.

Simple example: You're driving a car:

  • Action: Press gas pedal harder
  • Effect 1: Engine RPM increases
  • Effect 2: Car speed increases
  • Effect 3: Distance traveled increases

Question: "If I press the gas pedal, how much does distance traveled change?"

Answer: Multiply all the effects together!

\frac{\text{distance change}}{\text{pedal change}} = \frac{\text{distance}}{\text{speed}} \times \frac{\text{speed}}{\text{RPM}} \times \frac{\text{RPM}}{\text{pedal}}

Another example: A factory assembly line:

  • Worker A makes parts (produces 10/hour)
  • Worker B assembles parts into widgets (5 parts = 1 widget)
  • Worker C packages widgets (2 widgets = 1 box)

Question: "If Worker A speeds up and makes 12 parts/hour instead of 10, how many more boxes do we get?"

Parts increase: +2/hour
Widgets increase: +2/5 = +0.4 widgets/hour
Boxes increase: +0.4/2 = +0.2 boxes/hour

We multiplied through the chain: (+2) \times (1/5) \times (1/2) = +0.2

In math notation: If y = f(g(x)), then \frac{dy}{dx} = \frac{dy}{dg} \times \frac{dg}{dx}

This is the chain rule! Effects multiply through the chain of operations.

The chain rule in neural networks:

We need: \frac{\partial L}{\partial \mathbf{W}} (how much does weight W affect loss?)

This asks: "If I nudge weight W by a tiny amount, how much does the loss change?"

We work backward from loss through each layer:

\frac{\partial L}{\partial \mathbf{W}_{\text{vocab}}} = \frac{\partial L}{\partial \text{logits}} \cdot \frac{\partial \text{logits}}{\partial \mathbf{W}_{\text{vocab}}}

Visualizing the chain:

Weight W_vocab
    ↓ (affects)
Logits
    ↓ (affects)
Probabilities (via softmax)
    ↓ (affects)
Loss

To find how W affects Loss, we multiply all the "affects" together!

Concrete calculation for one weight:

Let's trace W_{\text{vocab}}[0,999] (affects "pizza" prediction):

Step 1: Loss gradient w.r.t. probabilities \frac{\partial L}{\partial P(\text{pizza})} = -\frac{1}{P(\text{pizza})} = -\frac{1}{0.012} = -83.33

Step 2: Softmax gradient

\frac{\partial P(i)}{\partial \text{logit}_j} = \begin{cases} P(i)(1-P(i)) & \text{if } i=j \\ -P(i)P(j) & \text{if } i \neq j \end{cases}

For "pizza": \frac{\partial P(\text{pizza})}{\partial \text{logit}_{\text{pizza}}} = 0.012 \times (1-0.012) = 0.01186

Step 3: Logit gradient w.r.t. weight

\frac{\partial \text{logit}_{\text{pizza}}}{\partial W_{\text{vocab}}[0,999]} = \text{input}[0] = 0.71

Step 4: Chain them together

\begin{align} \frac{\partial L}{\partial W_{\text{vocab}}[0,999]} &= -83.33 \times 0.01186 \times 0.71 \\ &= -0.702 \end{align}

This gradient tells us: "Decrease this weight by 0.702 (scaled by learning rate) to reduce loss."

The Complete Gradient Flow

Backpropagation flows through every layer:

Loss (4.423)
  ↓ gradient: -83.33
Softmax
  ↓ gradient: 0.988
Vocabulary Linear Layer  
  ↓ gradient: varies per neuron
Transformer Block N
  ↓ gradient: flows through residuals (thankfully!)
LayerNorm
  ↓ gradient: scaled and shifted
Feed-Forward Network
  ↓ gradient: ReLU masks (0 for negative inputs)
Residual connection
  ↓ gradient: splits into two paths
LayerNorm
  ↓ gradient: normalized
Multi-Head Attention
  ↓ gradient: complex but manageable
...
Transformer Block 1
  ↓ gradient: still strong thanks to residuals!
Positional Encoding (skipped - frozen)
  ↓ gradient: full strength
Embeddings
  ↓ gradient: UPDATE!

Key insight: Residual connections ensure gradients don't vanish. Without them, gradient might shrink to 0.0001 by block 1!

Gradient Descent (Updating Weights)

Once we have all gradients, we update every weight:

\mathbf{W}_{\text{new}} = \mathbf{W}_{\text{old}} - \eta \frac{\partial L}{\partial \mathbf{W}}

What does this mean intuitively?

Imagine you're hiking down a mountain in fog (you can only see your feet):

  • Loss is your altitude—you want to get to the lowest point (valley)
  • Gradient is the slope under your feet—which direction goes down?
  • Learning rate is your step size—how far you walk

If gradient is positive (+0.702), you're on an upward slope, so step backward (subtract) If gradient is negative (-0.702), you're on a downward slope, but subtraction makes it: -(-0.702) = +0.702, so step forward!

The formula automatically steps "downhill":

High loss (bad) 
      ↓ follow gradient
Low loss (good)

Concrete example:

Old weight: W_{\text{vocab}}[0,999] = 0.5 Gradient: \frac{\partial L}{\partial W} = -0.702 Learning rate: \eta = 0.0001

\begin{align} W_{\text{new}} &= 0.5 - 0.0001 \times (-0.702) \\ &= 0.5 - (-0.00007) \\ &= 0.5 + 0.00007 \\ &= 0.50007 \end{align}

Interpretation: The gradient is negative, meaning increasing this weight will reduce loss. So we increase it (by a tiny amount controlled by learning rate).

Tiny change! But after millions of examples, these accumulate:

  • After 10,000 updates: might change by 0.7
  • After 1,000,000 updates: might change by 70.0

The weights slowly "learn" their optimal values!

Adam Optimizer (Better Than Plain Gradient Descent)

Hyperparameters:

  • \beta_1 = 0.9 (momentum decay)
  • \beta_2 = 0.999 (variance decay)
  • \epsilon = 10^{-8} (numerical stability, prevents division by zero)

Why Adam instead of plain gradient descent?

Plain gradient descent has problems:

  1. All weights use same learning rate: Some parameters need big steps, others need tiny steps
  2. No momentum: Gets stuck in valleys, zigzags inefficiently
  3. Sensitive to scale: If one gradient is 1000× larger than others, chaos!

Adam solves all three by keeping track of:

  1. First moment m (moving average of gradients) - "Which direction am I usually going?"
  2. Second moment v (moving average of squared gradients) - "How much do my gradients typically vary?"

Think of it like driving:

  • Momentum (m): "I've been going straight, keep going straight" (don't jerk the wheel)
  • Adaptive learning (v): "This road is bumpy (high variance), slow down. That road is smooth, speed up."

Iteration 1:

\begin{align} g_1 &= -0.702 \quad \text{(gradient)} \\ m_1 &= 0.9 \times 0 + 0.1 \times (-0.702) = -0.0702 \\ v_1 &= 0.999 \times 0 + 0.001 \times (-0.702)^2 = 0.000493 \\ \hat{m}_1 &= \frac{m_1}{1-0.9^1} = \frac{-0.0702}{0.1} = -0.702 \\ \hat{v}_1 &= \frac{v_1}{1-0.999^1} = \frac{0.000493}{0.001} = 0.493 \\ W_{\text{new}} &= W - \eta \frac{\hat{m}_1}{\sqrt{\hat{v}_1} + \epsilon} \\ &= 0.5 - 0.0001 \times \frac{-0.702}{\sqrt{0.493} + 0.00000001} \\ &= 0.5 - 0.0001 \times \frac{-0.702}{0.702} \\ &= 0.5 + 0.0001 \\ &= 0.5001 \end{align}

Why do we divide by (1-\beta_1^t) and (1-\beta_2^t)? Because Adam starts both moving averages at zero. That makes the early m and v values artificially too small. The bias-correction terms "warm them up" so the first few optimization steps aren't misleadingly tiny.

Why Adam?

  • Adapts learning rate per parameter
  • Handles sparse gradients well
  • Momentum helps escape local minima
  • Industry standard for transformers

Training Vocabulary

Step: One batch processed + weights updated Epoch: One complete pass through all training data Training run: Multiple epochs until convergence

Example training timeline:

Epoch 1:
  Step 1: Batch loss = 6.234, update weights
  Step 2: Batch loss = 6.102, update weights
  ...
  Step 100,000: Batch loss = 4.891
  Average epoch loss: 5.123

Epoch 2:
  Step 1: Batch loss = 4.456
  ...
  Average epoch loss: 4.234

Epoch 3:
  Average epoch loss: 3.678

...

Epoch 10:
  Average epoch loss: 2.103 ← Good predictions!

Real-world scale:

  • GPT-3: Trained on 300 billion tokens
  • Training time: Several months on thousands of GPUs
  • Cost: Estimated $4-12 million
  • Dataset: Common Crawl, WebText, Books, Wikipedia

Course navigation: Previous: Chapter 10 - The Output Head | Next: Chapter 12 - Causal Masking