1 The pre-training paradigm
- Before 2018: every NLP task trained its own model from scratch. After 2018: pre-train once, fine-tune for any task. (As big a shift for NLP as ImageNet was for vision.)
- Pre-training: train a big model on massive unlabeled text (the internet, billions of words โ free) to learn grammar, word meanings, world facts.
- Fine-tuning: adapt that model to a specific task with a small labeled dataset (labels are expensive + scarce).
- Why it works: language knowledge transfers โ it's reusable across sentiment, NER, QA, translation, etc.
- Analogy: medical school (long, expensive, once) โ residency (short, cheap, per specialty).
- Key insight: "If you can fill in blanks correctly, you understand language." Google โ BERT, OpenAI โ GPT (same insight, two designs).
2 Two philosophies, one Transformer
- BERT (Google): see everything, understand deeply โ bidirectional. Like an open-book exam.
- GPT (OpenAI): see the past, predict the future โ left-to-right. Like writing an essay.
- Same Transformer โ different attention masks. BERT = better at understanding; GPT = better at generating.
3 BERT โ Bidirectional Encoder Representations
- Uses only the Transformer encoder (no decoder).
- Key idea: to understand a word you need both sides of context.
- Disambiguation example: "The bank was flooded" โ river bank (needs "flooded" on the right); "The bank approved the loan" โ financial (needs "loan" on the right). Without right context you cannot disambiguate.
4 Masked Language Modeling (MLM)
- Training task: randomly mask 15% of tokens, predict them from surrounding context. Forces bidirectional understanding (a fill-in-the-blank exam โ read the whole sentence to answer).
| Treatment of the 15% | Proportion | Reason |
|---|---|---|
Replace with [MASK] | 80% | Main training signal |
| Replace with random word | 10% | Prevent model ignoring non-[MASK] tokens |
| Keep unchanged | 10% | Teach that any word could be the target |
- Why not 100% [MASK]? At fine-tuning time there are no masks โ a 100%-mask model would see a train/test mismatch.
- Why 15%? Balance between enough training signal and enough visible context.
5 BERT architecture & fine-tuning
| BERT-base | BERT-large | |
|---|---|---|
| Layers | 12 | 24 |
| Hidden size | 768 | 1024 |
| Attention heads | 12 | 16 |
| Parameters | 110M | 340M |
- Pre-trained on Wikipedia + BookCorpus; ~4 days on 64 TPUs โ you do not train it yourself, you download it (HuggingFace).
- Also pre-trained with Next Sentence Prediction (NSP) โ later shown to be less important (RoBERTa dropped it).
- Fine-tuning: add a small task head (linear + softmax) on top of pre-trained BERT (frozen or fine-tuned).
- Classification:
[CLS]token โ linear โ softmax. - NER: each token โ label.
- QA: predict start & end positions of the answer span.
- Classification:
- Typical recipe: learning rate
2e-5, 3โ5 epochs, 1 GPU, a few hours. - The revolution: one expensive pre-train โ cheap adaptation. ~1000 labeled examples + 3 epochs โ 85%+ accuracy; from scratch would need ~100ร more data.
6 GPT โ Generative Pre-trained Transformer
- Uses only the Transformer decoder.
- Autoregressive: predict the next token given all previous tokens.
- Causal mask: position i can only attend to positions
1 โฆ i(no peeking ahead). "GPT wears blinders." - Core idea: like your phone's autocomplete โ predict the next word.
7 Autoregressive generation
- Training: predict next token at every position (teacher forcing).
- Generation loop: (1) predict next-token distribution โ (2) sample โ (3) append token โ (4) repeat until a stop token. Every ChatGPT word is produced this way โ one token at a time.
- Temperature
softmax(zแตข / T): low (0.1) = peaked, deterministic, safe; high (1.5) = flat, creative, risky. - Top-k / Top-p: sample only from the most likely candidates โ prevents nonsense.
8 Scaling & few-shot learning
| Model | Parameters | Key achievement |
|---|---|---|
| GPT-1 (2018) | 117M | Proved the concept |
| GPT-2 (2019) | 1.5B | "Too dangerous to release" |
| GPT-3 (2020) | 175B | Few-shot learning emerges |
| GPT-4 (2023) | ~1.7T (est.) | Multimodal, powers ChatGPT |
- Emergent abilities: capabilities appear at scale that weren't explicitly trained โ nobody programmed GPT-3 to do arithmetic or translate; it just learned.
- Few-shot / in-context learning (GPT-3): give examples in the prompt โ it follows the pattern, with no weight updates.
- Zero-shot: "Translate English to French: Hello โ"
- One-shot: "Hello โ Bonjour. Goodbye โ"
- Few-shot: 5 examples โ new query.
- This is why prompt engineering matters โ you're programming with examples, purely in context.
9 BERT vs GPT โ head to head
| BERT | GPT | |
|---|---|---|
| Architecture | Encoder-only | Decoder-only |
| Context | Bidirectional | Left-to-right (causal) |
| Training | MLM (fill blanks) | Next-token prediction |
| Strength | Understanding | Generation |
| Use cases | Classification, NER, QA | Chatbots, writing, code |
10 Tokenization โ text to numbers
- Models need numbers, not text. Three strategies:
- Word-level: "unhappiness" = 1 token. Problem: vocabulary explodes.
- Character-level: 11 separate tokens. Problem: sequences too long.
- Subword (BPE): "un" + "happi" + "ness" โ best of both worlds.
- Goldilocks rule: common words stay whole; rare words split into reusable pieces ("un-", "-ness" appear in hundreds of words).
Byte Pair Encoding (BPE):
1. Start with a character vocabulary 2. Count all adjacent character pairs in the corpus 3. Merge the most frequent pair into a new token 4. Repeat N times (N = desired vocab size - initial chars) Example merges: l,o,w,e,r -> "lo" -> "low" -> "lower"
- Result: common words stay whole, rare words split into frequent subwords. GPT-2/3/4 use BPE with ~50K vocab.
- WordPiece (BERT's tokenizer): similar to BPE but uses likelihood-based merging (BPE is frequency-based).
##prefix marks continuation tokens: "playing" โ ["play", "##ing"]. ~30K vocab for BERT. Different algorithm, same idea โ in practice, similar results.
โ Likely exam questions
Q1. Why can't BERT generate text, even though it understands language well?
BERT is bidirectional โ it sees all positions simultaneously. To generate token 5 it would peek at tokens 6+. No causal ordering means no sequential generation.
Q2. Why mask randomly (15%) instead of always masking the last word? And why not 100% [MASK]?
Random masking forces bidirectional context use; always-last would just be left-to-right (GPT). Of the 15%: 80% โ [MASK], 10% โ random word, 10% โ unchanged. Not 100% because at fine-tuning time there are no masks โ avoid the train/test mismatch.
Q3. You have 500 labeled emails โ train from scratch or fine-tune BERT?
Fine-tune BERT. 500 labels is far too few to train from scratch (would overfit). BERT already knows language; just add a task head and teach it the task. Expect 85%+ accuracy; lr โ 2e-5, 3โ5 epochs.
Q4. What is GPT-3's ability to learn from a few prompt examples called, and why is it surprising?
Few-shot / in-context learning. Surprising because there are no weight updates โ it's pure pattern recognition from the prompt examples. It's an emergent ability that appeared at scale.
Q5. A tokenizer splits "15213" into ["152", "13"]. How does this cause arithmetic errors?
The model sees "152" and "13" as separate units, not the digits 1-5-2-1-3. It operates on inconsistent chunks instead of individual digits, so digit-level arithmetic fails.
Q6. What is the single architectural difference between BERT and GPT?
The attention mask. BERT uses full (bidirectional) attention via the encoder; GPT uses a causal (left-to-right) mask via the decoder. Everything else is the same Transformer.
Q7. BPE vs WordPiece?
Both produce subwords. BPE merges the most frequent adjacent pair (frequency-based), ~50K vocab, used by GPT-2/3/4. WordPiece merges by likelihood, ~30K vocab, marks continuations with ##, used by BERT. Similar results in practice.