Lesson 5.1 · 1 of 7 · 4 min read · Reviewed 24 Nov 2026
Pretraining
By the end, you can
- Describe pretraining.
- Explain what a pretrained model can and can't do.
Next-token prediction at scale
text: "The capital of France is Paris."
task: given "The capital of France is", predict " Paris"
The model sees enormous numbers of examples like this from books, websites, code and other text. Each wrong guess nudges its parameters slightly. After enough examples, it gets very good at predicting what comes next.
What emerges
To predict well, the model has to learn a lot:
- Grammar and style
- Facts that appear often in text
- Patterns of reasoning and argument
- How code works
None of this is programmed directly; it’s learned because it helps prediction.
What a pretrained model is like
Ask a raw pretrained model “What is the capital of France?” and it might continue with more quiz questions, because that’s a plausible continuation. It doesn’t know it’s supposed to answer. That’s what the next stages fix (5.2, 5.3).
Scale
PretrainingPretraining: The first, largest training stage, where a model learns to predict the next token over a huge general text corpus. is the most expensive stage by far, using large clusters of specialised chips for weeks or months. That’s why only a few organisations train frontier models from scratch, and why most builders use them through APIs.
Key points
- Pretraining teaches a model to predict the next token on a very large amount of text.
- From that one objective, models pick up grammar, facts, reasoning patterns and coding ability.
- A pretrained model continues text; it isn't yet a helpful assistant that follows instructions.
Turn text into next-token training pairs
See how one objective, predicting the next word, turns plain text into many training examples and a crude model.
You will have: A script that builds (context, next word) pairs from a tiny corpus, counts them and completes a prompt with one word and two words of context.
Steps
- Run the starter: it prints zero training pairs.
- Fill in pairs(): at every position, the words so far are the context and the next word is the target.
- Count which word follows each one-word context, then each two-word context.
- Predict the word after 'the capital of france is' with both counts.
- Explain why two words of context beat one, and add a sentence that changes the answer.
Done when
- 4 sentences give 20 training pairs.
- With one word of context ('is') there is a four-way tie.
- With two words ('france is') the model predicts 'paris'.
Open in the Playground No API key needed.
Starter code (Python)
from collections import Counter, defaultdict
CORPUS = ["the capital of france is paris", "the capital of italy is rome",
"paris is the capital of france", "rome is a city in italy"]
def pairs(sentence):
words = sentence.split()
out = []
# TODO: for i in 1..len(words)-1, append((words[:i], words[i]))
return out
all_pairs = [p for s in CORPUS for p in pairs(s)]
print("training pairs:", len(all_pairs))
one = defaultdict(Counter) # last word -> next word counts
two = defaultdict(Counter) # last two words -> next word counts
# TODO: fill both counters from all_pairs (skip two-word contexts that are too short)
print("after 'is':", dict(one["is"]))
print("after 'france is':", dict(two["france is"]))
Show a solution
from collections import Counter, defaultdict
CORPUS = ["the capital of france is paris", "the capital of italy is rome",
"paris is the capital of france", "rome is a city in italy"]
def pairs(sentence):
words = sentence.split()
return [(words[:i], words[i]) for i in range(1, len(words))]
all_pairs = [p for s in CORPUS for p in pairs(s)]
print("training pairs:", len(all_pairs))
for ctx, nxt in all_pairs[:3]:
print(" ", " ".join(ctx), "->", nxt)
one = defaultdict(Counter)
two = defaultdict(Counter)
for ctx, nxt in all_pairs:
one[ctx[-1]][nxt] += 1
if len(ctx) >= 2:
two[" ".join(ctx[-2:])][nxt] += 1
print("after 'is':", dict(one["is"]), "->", len(one["is"]), "way tie")
best = two["france is"].most_common(1)[0][0]
print("after 'france is':", dict(two["france is"]), "-> predict", best)