GPT-2 · RoBERTa Lab · Part II — The GPT family
08

Pretraining, loss
and perplexity

What exactly the model learned — and how to measure it with a single number, without relying on anecdotes.

Chapter 08 of 15 Prerequisite: chapter 06

1 The task: predict the next word. That's all.

GPT-2 was never taught to answer questions, summarize texts, translate or write code. It was trained on a single task, repeated billions of times:

given everything that came before, what is the next token?

This is called causal language modeling (CLM). Formally, the model learns the probability of an entire sequence, decomposed by the chain rule:

P(x1, …, xn) = Πt=1n P(xt | x<t) The probability of a text is the product of the probabilities of each token given the previous ones. This factorization is exact — it is not an approximation. It is the reason the causal mask (chapter 04) is the right piece: it guarantees that xt only sees x<t.
Why such a simple task produces so much

To predict the next word well in arbitrary texts from the internet, the model is forced to learn, as a side effect: spelling, grammar, agreement, facts about the world, the structure of arguments, code formats, and even rudiments of reasoning.

Think of completing "The capital of Japan is ____". There is no statistical shortcut: to get it right, you have to know. The training objective is trivial; the pressure it exerts is not. That was the big bet of the GPT-2 paper — and it won.

The hidden efficiency

Each sequence of n tokens produces n simultaneous training examples, not one. A text of 1024 tokens generates 1024 predictions, all charged at once, in a single pass. That is why it is called self-supervision: the labels are the text itself, shifted by one position. No human annotated anything.

One sequence, many examples:

input    The  capital  of    France  is
target   capital  of   France  is    Paris
         ^       ^      ^      ^      ^
         5 predictions, 5 losses, 1 pass through the model

Alignment in code: logits[:, :-1]  against  labels[:, 1:]
(drops the last prediction, which has no target, and the first
token, which is predicted by nobody)

2 The loss: cross-entropy

How is a mistake punished? With the negative logarithm of the probability assigned to the correct token:

ℒ = − 1n Σt=1n log P(xt | x<t) Average, over all positions, of −log of the probability given to the word that actually appeared. Note: only the probability of the correct token enters the calculation. The other 50,256 matter only because they split the mass in the softmax.

Why the logarithm

Three reasons, and all three are worth understanding:

3 Perplexity: the loss in human units

The loss is useful but not very intuitive. Perplexity is simply its exponential:

PPL = exp(ℒ) If the loss is 3.2 nats, the perplexity is e3.2 ≈ 24.5.

And it has a direct and very useful reading:

How to read a perplexity

Perplexity is the effective number of options the model is hesitating between, at each word.

PPL = 1 → absolute certainty, always. PPL = 25 → it is as if, at each word, the model were choosing randomly among 25 equally likely candidates. PPL = 50,257 → the model learned nothing; it is guessing uniformly over the entire vocabulary.

Lower is better. Always.

References to calibrate your intuition

Typical perplexities in English. Be careful when comparing numbers across sources — see the warning below.
SituationApproximate PPL
Uniform guess over the vocabulary50,257
Trigram model (1990s)~150
GPT-2 small, English text~25–30
GPT-2 XL, English text~15–18
GPT-3 (175 B)~10
Frontier models, 2026< 8
Estimate for a human~10–12
Perplexity is only comparable within the same tokenizer

This is the most common trap, and it invalidates many published comparisons. Perplexity is measured per token. If model A uses a tokenizer that breaks the text into 100 tokens and model B uses one that breaks it into 150, model B looks better just because it is predicting smaller, easier pieces.

The whole GPT-2 family shares the same tokenizer, so comparing them against each other is legitimate. Comparing the PPL of a GPT-2 with that of a Llama is comparing different things — unless you normalize per character or per byte (the bits-per-character and bits-per-byte metrics exist precisely for that).

This also explains why GPT-2's PPL in Portuguese will look high: besides the model not knowing the language well, each word is being broken into more tokens.

4 How GPT-2 was trained, in practice

ItemDetail
CorpusWebText: ~8 million pages, ~40 GB of text. Built by scraping every outbound link posted on Reddit with at least 3 karma — a cheap and ingenious quality filter, using already-existing human curation.
FilteringWikipedia was removed on purpose, to avoid contaminating evaluations. Duplicates removed.
ObjectivePure CLM. No instructions, no RLHF, no chat. GPT-2 is not an assistant — it is a text completer.
Context1024 tokens. Documents separated by <|endoftext|>.
OptimizerAdam, with learning-rate warmup and cosine decay.
EpochsApproximately 1. The model saw each text essentially once.
"GPT-2 is not an assistant" — take this seriously

If you write "What is the capital of France?", GPT-2 will not answer. It will continue the text, and the most likely continuation of a question on the internet is… another question. It may return "What is the capital of Germany? What is the capital of Italy?".

This is not a failure of the model — it is the model working exactly as trained. The distance between GPT-2 and ChatGPT is not mainly size: it is instruction fine-tuning and RLHF, which came later and teach the model to treat the text as a request to be fulfilled rather than a passage to be continued.

Practical consequence: with GPT-2, write prompts in the form of a beginning of a text, not a question. "The capital of France is" works; "What is the capital of France?" does not.

5 The code

# perplexity of a text, the right way
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tok = AutoTokenizer.from_pretrained("openai-community/gpt2")
mod = AutoModelForCausalLM.from_pretrained("openai-community/gpt2").eval()

def perplexidade(texto):
    ids = tok(texto, return_tensors="pt", truncation=True, max_length=1024)
    # by passing labels=input_ids, transformers already does the
    # 1-position shift and computes the mean cross-entropy.
    with torch.no_grad():
        perda = mod(**ids, labels=ids["input_ids"]).loss
    return torch.exp(perda).item()

print(perplexidade("The capital of France is Paris."))     # low
print(perplexidade("Colorless green ideas sleep furiously.")) # high

6 In the lab

.venv\Scripts\python.exe lab\08_perplexity.py
  1. Measures the perplexity of several kinds of text — natural English, Portuguese, shuffled text, random text, code, and Chomsky's famous sentence. The order of the results says a lot about what the model learned.
  2. Compares the five models on the same text. Here the scaling shows up clean and monotonic, unlike the anecdote in chapter 06. This is the honest measurement.
  3. Shows the surprise token by token, highlighting where the model was startled. It is the most direct view of what the model "knows".
  4. Demonstrates that GPT-2 is not an assistant, comparing a prompt in the form of a question with one in the form of a beginning of a text.
  5. Measures perplexity in Portuguese and separates the two effects that inflate it: not knowing the language and tokenizer fragmentation.

7 Two lab results worth the whole chapter

First: scaling behaves when measured properly

Perplexity on the same English paragraph, measured on this machine.
ModelParametersPPLCompared to the previous one
openai-gpt117 M46.07—
gpt2124 M21.70improved
gpt2-medium355 M16.04improved
gpt2-large774 M14.30improved
gpt2-xl1558 M12.93improved

Monotonic at every step. No inversion, no surprise. Compare with chapter 06, where large "missed" a question that medium "got right".

The models are the same in both experiments. What changed was the metric. A single sample measures noise; perplexity measures the distribution. Keep this pair of results — it is the best methodology lesson in this course.

Also note the jump GPT-1 → GPT-2: from 46 to 21.7 with practically the same number of parameters (117 M vs 124 M). That was not scale. It was the better corpus, the better tokenizer and Pre-LN. It is the subject of the next chapter.

Second: random garbage is easier than a grammatical sentence with no meaning

PPL of gpt2 on texts of different natures. Read the order carefully.
TextPPL
Famous cliché: "It was the best of times…"5.8
Python code8.6
Natural English21.7
Natural Portuguese413.1
Random characters469.4
English with shuffled words3,874.0
Chomsky: "Colorless green ideas sleep furiously"6,413.3

Pure noise comes out eight times cheaper than shuffled English, and thirteen times cheaper than Chomsky's sentence. This seems strange until you understand what perplexity really measures.

Perplexity measures frustrated expectation, not disorder

After the first few random characters, the model enters a "this is noise" regime and starts expecting more noise — which it does reasonably well. The distribution becomes flat, but correct.

Shuffled text, on the other hand, activates all of the model's grammatical expectations and then violates them, one by one. Each violation is expensive. Chomsky's sentence does the same at the semantic level: impeccable syntax, impossible meaning.

Useful corollary: perplexity is not a measure of text quality. A text can have a very low PPL because it is generic and memorized (see the cliché, at 5.8). Using PPL as a proxy for "quality" is a common mistake.

Methodological caveat: Chomsky's sentence has only 7 tokens. Averages over tiny samples are unstable, and a single improbable token dominates the result. Treat that number as illustrative. General rule: distrust perplexity computed on short texts.

Exercise 8 — predict before running

  1. Order from lowest to highest PPL, before running: (a) an English Wikipedia paragraph; (b) the same paragraph with the words shuffled; (c) Python code; (d) a paragraph in Portuguese; (e) random characters.
  2. If one model has PPL 20 and another has 10, is the second one "twice as good"? What does the ratio between them actually mean? (Hint: think in bits.)
  3. Why was Wikipedia removed from WebText? What would happen to the evaluations if it had not been?
  4. Hard: the mean loss per token hides enormous variation. A text can have PPL 25 with every token at ~25, or with half at 2 and half at 300. How would you distinguish the two cases, and why would it matter?
References

The primary sources for the concepts in this chapter. The numbers in the text were measured in the lab; the ideas come from here.

Technical terms here carry their machine-learning meaning. The course glossary shows that meaning next to the others the same words have in IT.

  1. Radford, A., Narasimhan, K., Salimans, T. and Sutskever, I. (2018). Improving Language Understanding by Generative Pre-Training. Technical report, OpenAI. cdn.openai.com
  2. Radford, A., Wu, J., Child, R., Luan, D., Amodei, D. and Sutskever, I. (2019). Language Models are Unsupervised Multitask Learners. Technical report, OpenAI. cdn.openai.com
  3. Ouyang, L. et al. (2022). Training Language Models to Follow Instructions with Human Feedback. NeurIPS 2022. arXiv:2203.02155

End of Part II. You understand the entire GPT family: how it is assembled, how it chooses words, and how it is measured.
Part III crosses over to the other branch of the tree — and chapter 09 first settles the account with GPT-1.