Pretraining
July 11, 2026
Pretraining teaches the model what the world and language look like.
Suppose the training text is “The capital of France is Paris.” This gets preprocessed into training examples:
- Input: “The” → Target: “capital”
- Input: “The capital” → Target: “of”
- Input: “The capital of” → Target: “France”
The loss is
To see where this comes from, suppose the training text is “The cat sat.” The model sees:
"The" → predict "cat"
"The cat" → predict "sat"
At each position, the model outputs a probability distribution over all possible next tokens. For example:
| Context | True next token | Model probability |
|---|---|---|
| ”The” | cat | 0.9 |
| ”The cat” | sat | 0.8 |
The probability the model assigns to the entire sequence is
Using the chain rule,
Pretraining tries to make the training text as probable as possible: maximize .
Why we take logs
Since the probability of an entire sequence is a product,
this is inconvenient: derivatives are messy, and probabilities become tiny very quickly. Instead we take the logarithm. Since ,
and we optimize a sum instead of a product. This helps for two reasons:
- Numerical stability: products of many probabilities quickly underflow to zero in floating-point arithmetic, while sums of logs remain well-behaved.
- Optimization: gradients of the log likelihood are much cleaner.
Taking the log doesn’t change the objective, because the logarithm is monotonically increasing: if then , so the sequence with the highest probability also has the highest log probability.
From log-likelihood to loss
Maximizing is mathematically identical to minimizing , and frameworks like PyTorch, TensorFlow, and JAX expect you to provide a scalar loss and then compute its gradient to minimize it. So instead of “maximize log likelihood” we say “minimize negative log likelihood”; they are the same optimization problem. So instead of
we define
Suppose the true next token is “cat”:
- Good prediction: the model says cat: 0.99. Loss contribution: , a tiny penalty.
- Decent prediction: the model says cat: 0.6. Loss: , a bigger penalty.