7. Training: Loss & Backpropagation

How models learn from mistakes

A language model starts out knowing nothing. Its weights are random numbers, so its predictions are essentially random guesses. Training is the process of systematically correcting those guesses, millions of times, until the model starts making sensible predictions about language.

The engine that drives this correction has three moving parts: a loss function that measures how wrong the model is, gradient descent that figures out which direction to adjust the weights, and backpropagation that efficiently computes those adjustments across every layer. Together they turn a pile of random numbers into something that can write code, summarize documents, and hold a conversation.

The Training Objective

Language models are trained with a deceptively simple goal: given a sequence of tokens, predict the next one. That's it. Feed the model "The cat sat on the", and it should assign high probability to "mat" (or "floor", or "chair") and low probability to "refrigerator" or "democracy".

This framing is powerful because it requires no labeled data. Every piece of text on the internet is already a training example — each word is simultaneously an input context and a target label. This is called self-supervised learning.

During training, the model outputs a probability distribution over its entire vocabulary — tens of thousands of tokens — for every position in the sequence. The question is: how do we measure how good (or bad) that distribution is?

Cross-Entropy Loss

The standard answer is cross-entropy loss. It measures the surprise of the correct answer given the model's prediction.

If the model assigns probability pp to the correct next token, the loss for that prediction is −log⁡(p)-\log(p). Think about what this means:

  • If p=1.0p = 1.0 (perfect confidence in the right answer): −log⁡(1.0)=0-\log(1.0) = 0. No surprise, no loss.
  • If p=0.5p = 0.5: −log⁡(0.5)≈0.69-\log(0.5) \approx 0.69. Some surprise.
  • If p=0.01p = 0.01 (model was almost certain about the wrong answer): −log⁡(0.01)≈4.6-\log(0.01) \approx 4.6. Very surprised. Big loss.

Worked example. The vocabulary has three tokens: "mat", "floor", "democracy". The true next token is "mat". The model predicts:

Token Probability
mat 0.70
floor 0.25
democracy 0.05

Cross-entropy loss = −log⁡(0.70)≈0.36-\log(0.70) \approx 0.36.

If instead the model predicted mat=0.02, floor=0.48, democracy=0.50, the loss would be −log⁡(0.02)≈3.91-\log(0.02) \approx 3.91 — much higher, reflecting how badly wrong the model was.

The total loss for a training batch is the average cross-entropy over every token position. The goal of training is to make this number as small as possible.

Gradient Descent: Walking Downhill in Fog

Imagine the loss as a landscape of hills and valleys. Every point on this landscape corresponds to a particular setting of the model's weights. High terrain means the model is performing poorly; valleys mean it's doing well. Training is the process of navigating this landscape to find a low valley.

The catch: the landscape has hundreds of billions of dimensions (one per weight), you can only see your immediate surroundings, and the whole thing is covered in fog. You can't see the valley from where you stand — you can only feel which direction is downhill beneath your feet.

That's gradient descent. The gradient of the loss with respect to each weight tells you the slope at your current position. Moving in the direction opposite to the gradient is guaranteed to decrease the loss, at least a little.

The update rule is simple:

w←w−η⋅∂L∂ww \leftarrow w - \eta \cdot \frac{\partial L}{\partial w}

Where ww is a weight, LL is the loss, and η\eta (eta) is the learning rate — your step size.

Learning Rate: Step Size Matters

The learning rate is one of the most important hyperparameters in training. Too large and you overshoot the valley; too small and you make agonizingly slow progress.

Diagram

Learning rate comparison

Training Steps Loss min Good LR (smooth descent) High LR (oscillates, may not converge)

In practice, most modern training runs use a learning rate schedule — starting high to make rapid early progress, then decreasing as the model approaches convergence.

Backpropagation: Assigning Blame

Computing the gradient of the loss with respect to a single weight deep in the network is not obvious. The weight influences some intermediate value, which influences another, which eventually influences the loss. How do you measure the effect of a tiny change to that weight on the final loss?

The answer is backpropagation — an efficient application of the chain rule from calculus. If LL depends on zz, and zz depends on ww, then:

∂L∂w=∂L∂z⋅∂z∂w\frac{\partial L}{\partial w} = \frac{\partial L}{\partial z} \cdot \frac{\partial z}{\partial w}

The key insight: if you already know how much LL changes when zz changes, and you know how much zz changes when ww changes, multiplying them gives you how much LL changes when ww changes.

This chains together across all layers. To find the gradient at layer 1, you need the gradient at layer 2, which needs the gradient at layer 3, and so on. So you:

  1. Forward pass — run the input through the network, computing and storing intermediate activations at each layer.
  2. Compute loss — compare the output to the target and calculate cross-entropy loss.
  3. Backward pass — starting from the loss, propagate gradients backwards through each layer using the chain rule, using the stored intermediate values from the forward pass.
  4. Update weights — nudge every weight in the direction that reduces the loss.

A Simple Computation Graph

Here's a toy example: a two-input network that multiplies, adds, and then computes loss.

Diagram

Computation graph

x = 2 w = 3 z = x·w b = 1 a = z+b Loss(a) forward pass backward pass (gradients)

In the forward pass, values flow left to right. In the backward pass, gradients flow right to left. At each node, the chain rule multiplies the incoming gradient by the local derivative. After the full backward pass, every weight has a gradient, and all weights are updated simultaneously.

Backpropagation is efficient because it computes all gradients in a single backward pass. Without it, you'd need to nudge each weight individually and measure the effect — which would take as many forward passes as there are weights. For a model with 70 billion parameters, that's... impractical.

Batches and Epochs

Two more practical concepts before we move on.

Mini-batch gradient descent. You could compute the gradient using a single training example and update immediately, but this is noisy — one example might send you in the wrong direction. You could also average the gradient over the entire dataset before updating, but that takes forever with billions of examples. The compromise is a mini-batch: compute the average gradient over, say, 32 or 512 examples, then update. This balances noise reduction against computational efficiency.

A key side benefit: the noise in mini-batch gradients actually helps. It prevents the optimizer from getting stuck in sharp, narrow minima and tends to push toward flatter, more generalizable solutions.

Epochs. One epoch is a single complete pass through the training dataset. Most large language model training runs never complete even a single epoch — the datasets are so enormous (trillions of tokens) that the model sees each example at most once. Smaller-scale training runs may go through the data multiple times, though with diminishing returns and eventual overfitting.

Putting It Together

Every training step looks like this:

  1. Sample a mini-batch of token sequences.
  2. Run the forward pass: predict next-token distributions for every position.
  3. Compute cross-entropy loss: measure surprise at the actual next tokens.
  4. Run the backward pass: compute gradients for every weight via backpropagation.
  5. Update weights: nudge each weight down its gradient by the learning rate.
  6. Repeat — millions of times.

Key takeaways

  • Cross-entropy loss measures how surprised the model is by the correct next token. Lower is better.
  • Gradient descent navigates the loss landscape by repeatedly stepping in the downhill direction.
  • The learning rate controls step size: too large causes oscillation, too small is slow.
  • Backpropagation uses the chain rule to compute gradients for all weights in a single backward pass.
  • Mini-batches balance efficiency against gradient noise; that noise is often beneficial.

The widget below lets you explore a simplified 2D loss surface. Try placing the point on a ridge and running gradient descent with different learning rates — you'll see exactly the overshooting and oscillation that makes learning rate choice so important in real training.

Loss Landscape Explorer

The next article, Training in Practice, moves from these mathematical foundations to the engineering reality: optimizers like Adam that go beyond plain gradient descent, gradient clipping, mixed precision training, and the distributed systems that make it possible to train on hundreds of GPUs at once.