Tokenization & BPE Explained: How AI Reads Words | LLM Course #15
A model only understands numbers, so how does text become numbers? In Lecture 15 of the LLM Masterclass: characters vs words vs subwords, where byte-pair encoding came from (Gage 1994, Sennrich et al. 2016, GPT-2's byte-level BPE), the BPE algorithm merge by merge, ChotuGPT's own tiny BPE tokenizer in Python, our sentence as token IDs, and why some languages cost more tokens than others. 💻 Code for this lecture (branch lecture-15): https://github.com/programmingline/chotugpt/tree/lecture-15 Lecture 15 of 32 — LLM Masterclass · Module 3: Text to numbers ⚠️ ChotuGPT is a simplified teaching model. Its tokenizer is trained on 8 short sentences with 30 merges, so its merges and token IDs are for teaching only. In this lecture: • Characters (long sequences), words (huge vocabulary, unknown words) and subwords (the middle ground) • 1994: Philip Gage describes BPE for data compression • 2016: Sennrich, Haddow & Birch use BPE subword units for rare words in machine translation • 2019: GPT-2's byte-level BPE: 256 base bytes, a vocabulary of 50,257 tokens • The algorithm: count neighbouring pairs, merge the most frequent, repeat • ChotuGPT's tokenizer: 256 bytes + 30 merges = 286 tokens; 'After my morning chai, I feel' = 7 tokens • Unseen words fall back to smaller pieces; 'chai' in Hindi script costs 10 tokens here vs 1 in English • Vocabulary size is a trade-off: shorter sequences vs a bigger model Chapters: 00:00 Lecture 14 quiz answer 00:14 Tokenization and BPE 00:28 Characters, words, subwords 00:58 Like & subscribe 01:03 Where BPE came from: 1994, 2016, GPT-2 01:27 The BPE algorithm 01:49 Training ChotuGPT's tokenizer 02:20 Our sentence as token IDs 02:43 BPE in Python 03:07 Quiz 03:29 Unseen words and other languages 03:58 Choosing the vocabulary size 04:09 Recap & next lecture ✅ Lecture 14 quiz answer: A, zero. Minus the log of 1 is 0: the model was not surprised at all. ❓ Quiz: Byte-pair encoding builds its vocabulary by repeatedly merging… A) The most frequent pair of tokens B) Random letters C) Whole sentences. Comment your answer. I reveal it in Lecture 16! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 14 — Softmax, probability and cross-entropy ⏭ Next: Lecture 16 — Embeddings: meaning as geometry Sources: P. Gage, "A New Algorithm for Data Compression", The C Users Journal, 1994. R. Sennrich, B. Haddow and A. Birch, "Neural Machine Translation of Rare Words with Subword Units", ACL 2016 (arXiv:1508.07909). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (sections 2.2–2.3). A. Petrov et al., "Language Model Tokenizers Introduce Unfairness Between Languages", NeurIPS 2023. #LLM #Tokenization #BPE…more
A model only understands numbers, so how does text become numbers? In Lecture 15 of the LLM Masterclass: characters vs words vs subwords, where byte-pair encoding came from (Gage 1994, Sennrich et al. 2016, GPT-2's byte-level BPE), the BPE algorithm merge by merge, ChotuGPT's own tiny BPE tokenizer in Python, our sentence as token IDs, and why some languages cost more tokens than others. 💻 Code for this lecture (branch lecture-15): https://github.com/programmingline/chotugpt/tree/lecture-15 Lecture 15 of 32 — LLM Masterclass · Module 3: Text to numbers ⚠️ ChotuGPT is a simplified teaching model. Its tokenizer is trained on 8 short sentences with 30 merges, so its merges and token IDs are for teaching only. In this lecture: • Characters (long sequences), words (huge vocabulary, unknown words) and subwords (the middle ground) • 1994: Philip Gage describes BPE for data compression • 2016: Sennrich, Haddow & Birch use BPE subword units for rare words in machine translation • 2019: GPT-2's byte-level BPE: 256 base bytes, a vocabulary of 50,257 tokens • The algorithm: count neighbouring pairs, merge the most frequent, repeat • ChotuGPT's tokenizer: 256 bytes + 30 merges = 286 tokens; 'After my morning chai, I feel' = 7 tokens • Unseen words fall back to smaller pieces; 'chai' in Hindi script costs 10 tokens here vs 1 in English • Vocabulary size is a trade-off: shorter sequences vs a bigger model Chapters: 00:00 Lecture 14 quiz answer 00:14 Tokenization and BPE 00:28 Characters, words, subwords 00:58 Like & subscribe 01:03 Where BPE came from: 1994, 2016, GPT-2 01:27 The BPE algorithm 01:49 Training ChotuGPT's tokenizer 02:20 Our sentence as token IDs 02:43 BPE in Python 03:07 Quiz 03:29 Unseen words and other languages 03:58 Choosing the vocabulary size 04:09 Recap & next lecture ✅ Lecture 14 quiz answer: A, zero. Minus the log of 1 is 0: the model was not surprised at all. ❓ Quiz: Byte-pair encoding builds its vocabulary by repeatedly merging… A) The most frequent pair of tokens B) Random letters C) Whole sentences. Comment your answer. I reveal it in Lecture 16! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 14 — Softmax, probability and cross-entropy ⏭ Next: Lecture 16 — Embeddings: meaning as geometry Sources: P. Gage, "A New Algorithm for Data Compression", The C Users Journal, 1994. R. Sennrich, B. Haddow and A. Birch, "Neural Machine Translation of Rare Words with Subword Units", ACL 2016 (arXiv:1508.07909). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (sections 2.2–2.3). A. Petrov et al., "Language Model Tokenizers Introduce Unfairness Between Languages", NeurIPS 2023. #LLM #Tokenization #BPE