Pretraining
What does a model learn from reading half the internet?
Lesson complete
If you like:
In the last lesson we built a transformer, but its numbers start out random. Today we watch it learn by reading a mountain of text: that is pretraining.
No voice: the lesson advances on its own with subtitles.
The voice for this lesson is being recorded: for now it advances on its own with subtitles.
Review modeComment on this stepI finished the lesson
What you will achieve
- Explain why guessing the next piece of a text is self-supervised learning, and what pretraining is.
- Describe how a corpus is cleaned (language, quality, personal data, and duplicates) and calculate how alike two texts are from the five-word chunks they share.
- Recognize, with real measurements of Pythia, the order in which a model learns and why a base model is not an assistant and does not know what happened after its knowledge cutoff.
Key ideas
- Pretraining repeats a single exercise: cover the next piece of a text and guess it. Since the answer is already in the text, no human labels are needed: it is self-supervised learning, which is why it can use hundreds of billions of tokens (GPT-3 read about three hundred billion).
- The text is cleaned first: language, quality rules, personal data masked, and duplicates removed. A near-duplicate is detected by measuring what share of five-word chunks two texts have in common; the threshold is a choice (Pythia’s authors used 87%).
- Quality over quantity: GPT-3 went over its Wikipedia almost three and a half times and its filtered web less than half a time. And what is barely in the corpus is barely learned: Pythia, trained mostly on English, never answers “París” in Spanish.
- In Pythia, measured at 25 checkpoints: at about 34 million tokens it repeats the most frequent pieces; at about 2 billion it already writes correct sentences; facts (“Paris”) and patterns (“casa”) arrive around 34 billion and keep sharpening until the end.
- The result is a base model: it continues texts, it does not obey; it knows nothing after its knowledge cutoff and makes things up with confidence. What goes into the corpus can come out (memorization and privacy), and where the text comes from is an open legal and ethical question.
What many people believe
Common belief A pretrained model stores the internet like a database and looks up answers in it.
What is true It does not store the documents: it adjusts its parameters to better guess the next piece. It retains patterns and frequent facts, and sometimes heavily repeated texts word for word (which is why duplicates and privacy matter), but it cannot look up what it read, it can be confidently wrong, and it knows nothing after its knowledge cutoff.
Check yourself
Four questions. No grade, no account: they just show whether the idea stuck.
Under the hoodFor those who want the math and the code. You do not need it to follow the course.
The objective, in one formula
For a text of pieces , the model with parameters gives, at each place, a probability to the piece that really comes next. The error of a bet is , and pretraining minimizes its average, the cross-entropy:
With the causal mask from lesson 22, all predictions of a text are computed in one pass. If the right piece gets , the error is ; as , it grows without limit. Guessing at random among the pieces of Pythia’s tokenizer gives (nats per piece). The scene’s numbers: untrained, “five” gets , error ; after training, , error .
How we measured Pythia
Pythia (Biderman et al., 2023) is a family of 16 open models trained on the same data, in the same order, with 154 public checkpoints per model: step 0, steps 1, 2, 4, …, 512, and every 1,000 steps up to 143,000. Each step uses 1,024 sequences of 2,048 tokens ( tokens), so the total is billion. We used the 1.4-billion-parameter model (1,414,647,808 in total), trained on The Pile without deduplication, at 25 of its checkpoints. The script build-pythia.py (next to this lesson) loads each one, measures, and writes pythia-data.ts; nothing is edited by hand. Everything is deterministic: greedy decoding, no sampling.
The curve’s “average error” is on a 500-token English text written for this lesson in 2026 (The Pile is from 2020: it could not have read it). It is little text: good for seeing the shape of the curve, not for comparing models. Broken down by kind of piece, form clearly arrives before content:
| Step (tokens read) | Punctuation | Frequent words | Other words | Names and capitals | Average |
|---|---|---|---|---|---|
| 0 (0) | 11.40 | 10.86 | 11.02 | 11.32 | 11.01 |
| 16 (34M) | 2.94 | 7.34 | 11.09 | 11.32 | 8.65 |
| 128 (268M) | 2.40 | 4.40 | 9.70 | 10.42 | 6.77 |
| 1,000 (2.1B) | 1.46 | 2.82 | 5.94 | 7.64 | 4.21 |
| 16,000 (33.5B) | 0.94 | 2.00 | 3.84 | 6.33 | 2.82 |
| 143,000 (300B) | 0.80 | 1.84 | 3.39 | 6.14 | 2.52 |
Punctuation drops from 11.4 to 2.9 within the first 34 million tokens; proper names are still near 10 at 500 million and, at the end, remain the hardest (6.1). The answers: “Paris” first appears at step 16,000 (before that it says “the capital of France,” “the city of Lyon,” “Bordeaux”), with going from 0.30 to 0.61; “casa” at the same step, with going from 0.15 to 0.82. Counting (“1, 2, 3, 4, 5,” → “6”), on the other hand, already works at step 1,000: some simple patterns come early. In Spanish, “París” is never the most likely piece: at the end it gets . To compare languages regardless of the tokenizer one uses bits per byte: at the end, 0.78 in English and 0.99 on the same text translated into Spanish.
Near-duplicates
Each document becomes the set of its chunks of 5 words in a row (shingles). The similarity of two documents is the Jaccard index:
In the scene’s sentence, and : . Comparing everything with everything is impossible with billions of pages, so MinHash is used (Broder, 1997): with a hash function , the probability that the chunk with the lowest is the same in and in is exactly . With many hashes grouped into buckets, documents that match on a whole bucket are flagged as duplicates. FineWeb (Penedo et al., 2024) uses 5-grams and 112 hashes in 14 buckets of 8, targeting documents at least 75% similar; Pythia has a version trained on a deduplicated Pile, threshold 0.87, which went from about 300 to 207 billion tokens; GPT-3 shrank each dataset by 10% on average this way. Lee et al. (2022) found in C4 a 61-word sentence repeated more than 60,000 times, and with deduplicated data their models emitted memorized training text ten times less often.
Quality filters and the mix
The scene’s pipeline (crawl.ts) really applies three FineWeb rules: a page is dropped if at most 12% of its lines end in punctuation, if at least 10% of its characters are in repeated lines, or if at least 67% of its lines are shorter than 30 characters; and two from C4 (Raffel et al., 2020): “lorem ipsum” and curly brackets {. Emails and IP addresses are masked, as in the public release of FineWeb. GPT-3 filtered Common Crawl with a classifier that rewarded resemblance to high-quality reference texts: 45 TB compressed became 570 GB. The mix is not proportional to size: Wikipedia (3 billion tokens) weighs 3% and is read 3.4 times; filtered Common Crawl (410 billion) weighs 60% and is read 0.44 times. FineWeb-Edu, 1.3 trillion tokens selected for educational value, matches another web dataset on MMLU with almost 10 times fewer tokens.
In code
import math
def shingles(text, k=5):
w = text.lower().split()
return {' '.join(w[i:i + k]) for i in range(len(w) - k + 1)}
def jaccard(a, b):
return len(a & b) / len(a | b)
A = shingles('plant two thousand trees along the river path')
B = shingles('plant three thousand trees along the river path')
print(len(A), len(A | B), len(A & B), round(jaccard(A, B), 2))
# error of a bet: −ln p of the right piece
for p in (3.279e-05, 0.1175):
print(round(-math.log(p), 2))
print(round(math.log(50254), 2)) # guessing at random among 50,254 pieces
# 4 6 2 0.33
# 10.33
# 2.14
# 10.82
Rights, consent, and privacy
The Pile (Gao et al., 2020) marks in its table 5 which components have author consent; Books3 (books from a copy of the private site Bibliotik) does not. In Bartz v. Anthropic (Northern District of California), the order of June 23, 2025 found fair use in using the books for training and in digitizing purchased books, but not in downloading pirated copies for a permanent library; the $1.5 billion settlement, about $3,000 per work for some 482,000 works, was approved on July 20, 2026. It is one court’s decision in one country; other lawsuits are ongoing and other jurisdictions have different rules. Consent is shifting too: Longpre et al. (2024) estimate that in a single year (April 2023 to April 2024), a quarter or more of the tokens from C4’s most important sites became restricted for AI crawlers in their robots.txt. Carlini et al. (2021) extracted from GPT-2 604 memorized texts out of 1,800 candidates; among them were names, phone numbers, and emails, some present in a single training document.
What comes next
Pythia read 300 billion tokens with 1.4 billion parameters. Lesson 24 shows how the error falls as parameters, data, and compute grow, and what that costs. How a base model becomes an assistant is the subject of lessons 27 and 28.
Decision lens
What this idea means if your job is to decide, not to code.
- Ask where a model’s data comes from: which sources, under which licenses, and with what consent. A serious provider documents its corpus; if it cannot tell you, the legal and reputational risk is yours too.
- Every model has a knowledge cutoff: it does not know what came later and may make it up with confidence. Ask for the date and, if your use case depends on recent information, combine it with document search (lesson 31).
- A base model is not a product: it continues texts, it does not follow instructions. Open base models are useful for research or for tuning to your task; to hold a conversation you need the stage covered in lessons 27 and 28.
- What goes into the corpus can come out: a model can repeat memorized text, including personal data. Do not put sensitive data in training data, and ask how it was filtered and deduplicated.
Terms in this lesson
- Token A piece of text (a word, a word fragment, a symbol, or a basic code) taken from the vocabulary. It is the unit the model reads and generates, and the unit you are billed for.
- Tokenizer A program that cuts text into tokens and gives each one its number (and goes back the other way). It is built before the model is trained and stays fixed.
- Vocabulary The list of all the pieces (tokens) a tokenizer can produce, each with its number. Real ones have from about thirty-two thousand to about two hundred thousand; the lesson’s toy one, 656.
- Language model A model that, given some text (the context), gives a probability to every word or piece that can come next. It writes by adding one piece at a time.
- Transformer A neural-network architecture made of a stack of blocks with the same shape. Each block combines attention (between tokens) and a network applied to each token, with residual connections and normalization. It was introduced in “Attention Is All You Need” (2017).
- Parameter Each of the numbers a model adjusts while training and in which it stores what it learned.
- Loss function A single number that summarizes how wrong a model is on its data. Training is the search for the parameters that make it as small as possible.
- Gradient descent A method that adjusts the weights step by step, always against the slope of the error (the gradient), until it reaches a bottom.
- Training data Examples, each with its correct answer, from which a model adjusts its parameters.
- Machine learning An approach in which a computer obtains the rules from examples instead of someone writing them. The term is associated with Arthur Samuel (1959).
- Knowledge cutoff The date after which the training data includes no more text. The model does not know what came later unless it is given as context, and it does not always warn that it does not know.
- Hallucination A fluent, confident answer from a model that is false or backed by no source. It follows from predicting plausible text, not from checking facts.
- Instruction tuning Fine-tuning a base model on pairs of a prompt and an ideal answer written by people, so that it treats what it is asked as a request rather than as the start of a text.
- RLHF (reinforcement learning from human feedback) Tunes a model so that its answers earn more points from a reward model trained on human preferences, with a penalty (the KL “leash”) that keeps it close to the starting model.
Full transcript
Sources
- Biderman, S., Schoelkopf, H., Anthony, Q., Bradley, H., O’Brien, K., Hallahan, E., et al. (2023). Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling
- EleutherAI (2023). EleutherAI/pythia-1.4b (ficha del modelo y puntos de control)
- Gao, L., Biderman, S., Black, S., Golding, L., Hoppe, T., Foster, C., et al. (2020). The Pile: An 800GB Dataset of Diverse Text for Language Modeling
- Brown, T. B., Mann, B., Ryder, N., Subbiah, M., et al. (2020). Language Models are Few-Shot Learners (GPT-3)
- Common Crawl Foundation (2026). Common Crawl: corpus abierto de la web
- Penedo, G., Kydlíček, H., Ben Allal, L., Lozhkov, A., Mitchell, M., Raffel, C., Von Werra, L., Wolf, T. (2024). The FineWeb Datasets: Decanting the Web for the Finest Text Data at Scale
- Raffel, C., Shazeer, N., Roberts, A., Lee, K., Narang, S., et al. (2020). Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (T5, C4)
- Broder, A. Z. (1997). On the resemblance and containment of documents
- Lee, K., Ippolito, D., Nystrom, A., Zhang, C., Eck, D., Callison-Burch, C., Carlini, N. (2022). Deduplicating Training Data Makes Language Models Better
- Carlini, N., Tramèr, F., Wallace, E., Jagielski, M., Herbert-Voss, A., Lee, K., et al. (2021). Extracting Training Data from Large Language Models
- Longpre, S., Mahari, R., Lee, A., Lund, C., et al. (2024). Consent in Crisis: The Rapid Decline of the AI Data Commons
- U.S. District Court for the Northern District of California (juez W. Alsup) (2025). Bartz v. Anthropic PBC, n.º 3:24-cv-05417, Order on Fair Use (N. D. Cal., 23 de junio de 2025)
- U.S. District Court for the Northern District of California (jueza A. Martínez-Olguín) (2026). Bartz v. Anthropic PBC, Order Granting Final Approval of Class Action Settlement (N. D. Cal., 20 de julio de 2026)
- FIFA (2022). FIFA World Cup Qatar 2022, final: Argentina 3–3 France (Argentina won 4–2 on penalties), match centre
CommentsThis lesson’s public conversation: questions, facts to check, ideas.
Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub