Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Learning from text by playing fill-in-the-blank
Nobody labels the internet, so language models teach themselves with guessing games. GPT-style models guess the next word, over and over, which also makes them good at writing. BERT-style models cover up random words and guess them from both sides, which makes them good at understanding but not at writing. T5 blanks out whole phrases and writes them back. The demo shows the one-way mask a next-word guesser uses.
Pretraining on in-domain text (manuals, reports, code) before fine-tuning is one of the most reliable ways to get a model that understands specialised vocabulary.
- 1. GPT is pretrained by…
- 2. BERT is pretrained by…
- 3. T5's span corruption hides…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Causal LM: inputs x₁…x_(T−1), targets x₂…x_T; loss −Σ log p(x_(t+1) | x_≤t). Every position is a target (512 per 512-token sequence) and the model generates naturally.
- Masked LM: select about 15% of positions (77 per 512); replace 80% with [MASK], 10% with a random token, 10% unchanged; predict the originals from both sides. Only the selected positions give loss.
- Span corruption (T5): about 15% of tokens in spans averaging 3 tokens (about 26 spans per 512) are replaced by sentinels
<X0>,<X1>, …; the decoder outputs each sentinel followed by its span (short targets, about 104 tokens). - Other objectives: prefix-LM (bidirectional on a prefix, causal after), UL2’s mixture of denoisers, fill-in-the-middle (move a middle span to the end so a causal model learns infilling), ELECTRA’s replaced-token detection.
- Which to use: decoder-only causal LM dominates for general LLMs; masked objectives remain best for compact encoders (embeddings, classification).
- 1. Targets per 512-token sequence for masked LM at 15%:
- 2. BERT's replacement split for selected tokens:
- 3. Fill-in-the-middle teaches a causal model to…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Objectives in code
Exercise 1.
- Build causal-LM input/target pairs.
- Apply BERT 80/10/10 masking with ignored (−100) targets.
- Apply T5 span corruption with sentinel tokens.
Practice.
- Hugging Face collators:
DataCollatorForLanguageModeling(mlm=False)for causal LM,mlm=True, mlm_probability=0.15for masked LM; T5’s span corruption is intransformersexamples andt5/seqio. - Pack documents into fixed-length sequences with end-of-document tokens to avoid padding.
- For continued pretraining on a domain, keep the original objective of the model.
- Mask whole words (whole-word masking) rather than single subword pieces for better masked-LM representations.
from transformers import DataCollatorForLanguageModeling, AutoTokenizer
tok = AutoTokenizer.from_pretrained("bert-base-uncased")
mlm = DataCollatorForLanguageModeling(tok, mlm=True, mlm_probability=0.15)
clm = DataCollatorForLanguageModeling(tok, mlm=False) # labels = inputs; the model shifts internally
batch = mlm([tok("the left engine was shut down in flight")])
print(batch["input_ids"], batch["labels"]) # labels are -100 except at masked positions- 1. Hugging Face collator option for causal LM:
- 2. Ignored target positions are marked…
- 3. Continued domain pretraining should use…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
What each objective can learn. A causal model represents each position using only the past, which is exactly what generation requires, but it cannot condition on future context. Masked and span objectives condition on both sides, which suits understanding and infilling but creates a mismatch for left-to-right generation (no [MASK] tokens or future context at inference).
Signal density. Causal LM trains on every token, masked LM on 15%; for equal compute, causal pretraining extracts more signal per sequence. ELECTRA recovers density for encoders by predicting for every token whether it was replaced.
The trend. Raffel and colleagues (2020) found span corruption strongest for their encoder–decoder setting, and Tay and colleagues (2023) mixed objectives (UL2). But large decoder-only models trained with plain causal LM generalise best zero-shot (Wang and colleagues, 2022), and with prompting they also handle understanding tasks.
- 1. A causal model cannot condition on…
- 2. ELECTRA trains encoders by…
- 3. Masked LM's density is about…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.What does each pretraining objective teach the same model to do?
History.
- A. Radford et al., GPT (2018) and GPT-2 (2019).
- J. Devlin et al., “BERT” (NAACL 2019).
- C. Raffel et al., “T5” (JMLR 2020).
- K. Clark et al., “ELECTRA” (ICLR 2020).
- Y. Tay et al., “UL2: unifying language learning paradigms” (ICLR 2023).
Exercise 2.
- Data: ±1 random walks modulo 10 (16 tokens). The next token has two equally likely values, so the next-token loss floor is ln 2 = 0.693. A masked middle token is determined by its neighbours half the time, so the infill ceiling is 0.75 from both sides and 0.5 from the left alone.
- Model: the one-layer attention network from F4 (with a [MASK] token), trained for 500 steps with each objective:
- causal LM;
- masked LM (15%);
- span corruption (spans of 3, about 15%).
- Evaluation:
- next-token loss from a prefix (masked models get the prefix plus a [MASK] in the next slot);
- accuracy at filling one masked middle token (the causal model predicts it from the left).
| objective | next-token loss | infill accuracy |
|---|---|---|
| causal LM | 0.708 | 0.523 |
| masked LM | 0.796 | 0.758 |
| span corruption | 0.901 | 0.754 |
| limits | 0.693 | 0.75 (both sides) / 0.5 (left only) |
- Causal LM is the generator: it nearly reaches the floor for next-token prediction.
- But it cannot use the right side: infill stays at the left-only ceiling.
- Bidirectional objectives reach the infill ceiling (0.758 is within sampling noise of 0.75) but generate worse. Their training never matched the “nothing to the right” situation of generation.
- Seeds: seeds 1 and 2 gave causal 0.716/0.712 against masked 0.833/0.807 for next-token loss, and infill 0.502/0.508 against 0.766/0.738.
- Speed: 800 training steps gave the same pattern, so I used 500 to keep the exercise fast.
Pretraining objectives in AI, robotics and aerospace
- Domain encoders: masked-LM pretraining on technical corpora for search and classification.
- Domain generators: continued causal-LM pretraining on manuals and reports for assistants.
- Code tools: fill-in-the-middle objectives for in-editor completion of flight software.
- Design lesson: the objective decides what the model can do; match it to how the model will be used.
References
- J. Devlin, M.-W. Chang, K. Lee and K. Toutanova (2019). “BERT: pre-training of deep bidirectional transformers for language understanding.” Proc. NAACL.
- C. Raffel et al. (2020). “Exploring the limits of transfer learning with a unified text-to-text transformer.” JMLR 21.
- K. Clark, M.-T. Luong, Q. V. Le and C. D. Manning (2020). “ELECTRA: pre-training text encoders as discriminators rather than generators.” Proc. ICLR.
- M. Bavarian et al. (2022). “Efficient training of language models to fill in the middle.” arXiv:2207.14255.