AI Notes
Master tokenization. Word tokens, BPE, WordPiece. NLP fundamentals 2024.
Introduction
Tokenization is the foundational step in any Natural Language Processing pipeline. It is the process of breaking raw text into individual meaningful units called tokens. These tokens might be words, subwords, characters, or sentences depending on the tokenization strategy. Every NLP model, from simple bag-of-words classifiers to sophisticated transformers like GPT and BERT, begins by tokenizing input text. The quality of tokenization directly affects downstream task performance.
Why Tokenization Matters
Raw text is just a sequence of characters. Computers need structured input. Tokenization creates that structure by identifying boundaries between meaningful units. Consider the sentence: "I can't believe it's raining!" A naive split on spaces gives ["I", "can't", "believe", "it's", "raining!"] but a good tokenizer might produce ["I", "ca", "n't", "believe", "it", "'s", "raining", "!"] preserving linguistic meaning (contractions split into components).
Types of Tokenization
Word-Level Tokenization
| Input | "The quick brown fox jumps over the lazy dog" |
| Output | ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"] |
| - Split on whitespace | Simple but fails on punctuation |
| - Regex-based | r"\w+" captures word characters |
| - Language-specific rules | Handle contractions, hyphens |
| - Vocabulary explosion | English has 170,000+ words |
| - Unknown words: "cryptocurrency" not in old vocabulary | UNK token |
| - Morphological variants | "run", "runs", "running", "ran" are 4 tokens |
| - Compound words | "ice cream" — one concept, two tokens |
Subword Tokenization (BPE - Byte Pair Encoding)
| Algorithm | Byte Pair Encoding (BPE) |
| 1. Start with character-level vocabulary | {a, b, c, ..., z, space} |
| Corpus | "low lower lowest new newer newest" |
| Initial | [l,o,w] [l,o,w,e,r] [l,o,w,e,s,t] [n,e,w] [n,e,w,e,r] [n,e,w,e,s,t] |
| Most frequent pair: (e,w) | merge to "ew" |
| After | [l,o,w] [l,o,w,e,r] [l,o,w,e,s,t] [n,ew] [n,ew,e,r] [n,ew,e,s,t] |
| Next frequent: (l,o) | merge to "lo" |
| After | [lo,w] [lo,w,e,r] [lo,w,e,s,t] [n,ew] [n,ew,e,r] [n,ew,e,s,t] |
| Result | Common words are single tokens, rare words split into subwords |
| "unhappiness" | ["un", "happiness"] or ["un", "happ", "iness"] |
WordPiece (used by BERT)
| "playing" | ["play", "##ing"] |
| "unhappy" | ["un", "##happy"] |
| "embeddings" | ["em", "##bed", "##ding", "##s"] |
| Vocabulary size | BERT uses 30,522 tokens |
SentencePiece (used by T5, LLaMA)
Tokenization in Practice
GPT-4 tokenization examples
"Hello world" → [15496, 1917] (2 tokens)
"Artificial Intelligence" → [8210, 318, 11571, 12] (varies by model)
"don't" → ["don", "'t"] (handles contractions)
"indistinguishable" → ["ind", "ist", "inguish", "able"] (subwords)
Token count matters for
- Context window limits (GPT-4: 128K tokens)
- API pricing (charged per token)
- Processing speed (more tokens = slower)
Rule of thumb: 1 token ≈ 4 characters ≈ 0.75 words in English
100 words ≈ 130 tokens
Handling Special Cases
| Numbers | "2024" might be one token or split ["20", "24"] |
| Code: "def calculate_total():" | multiple subword tokens |
| URLs: "https://example.com" | many tokens (expensive!) |
| Emojis: "😀" | special unicode token |
| Multilingual | Non-Latin scripts often require more tokens per word |
| [CLS] - Classification token (BERT | start of sequence) |
Impact on Model Performance
Summary
Tokenization bridges the gap between human-readable text and machine-processable numbers. Modern subword tokenization (BPE, WordPiece, SentencePiece) elegantly solves the open-vocabulary problem by decomposing rare words into frequent subword units while keeping common words as single tokens. Understanding tokenization is essential because it directly affects model vocabulary size, sequence length, computational cost, and the ability to handle diverse languages and domains.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Tokenization - Text Processing.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, natural, language, processing, tokenization
Related Artificial Intelligence Topics