Tokenization fundamentals
4.2.1· AI-ML › Tokenization & Language Modeling
Token Kya Hota Hai?
Words kyun nahi use karte?
- Rare words problem: "antidisestablishmentarianism" training data mein sirf ek baar aata hai → model ise properly kabhi nahi seekh paata
- Out-of-vocabulary (OOV): Naye words ("COVID-19") exist hi nahi karte the jab model train hua tha
- Morphology: "run", "running", "ran" ko bilkul alag-alag treat kiya jaata hai → koi shared understanding nahi
- Vocabulary explosion: English mein ~170K words hain, lekin proper nouns, typos, emoji ke saath infinite variations hain
Characters kyun nahi use karte?
- Sequence length explosion: "Hello" = 5 tokens vs 1 word token → 5× lambi sequences
- Computational cost: Transformer ki complexity sequence length mein O(n²) hai
- Long-range dependencies: Model ke liye "H" + "e" + "l" + "o" ko meaning mein connect karna mushkil ho jaata hai
Tokenization Process
Step 1: Normalization
Kya hota hai: Text ko split karne se pehle clean aur standardize karo
Original: " Hello, WORLD! 🌍 "
Normalized: "hello, world! 🌍"
Common operations:
- Lowercase conversion (optional)
- Unicode normalization (NFC, NFD, NFKC, NFKD)
- Whitespace trimming
- Accent removal (optional)
Yeh kyun matter karta hai: "café" vs "cafe" vs "café" (different unicode) ideally same tokens se map hone chahiye.
Step 2: Pre-tokenization
Kya hota hai: Text ko "words" mein split karo jo aage aur split nahi honge
Input: "Don't stop learning!"
Pre-tokenized: ["Don", "'", "t", "stop", "learning", "!"]
Common rules:
- Whitespace par split karo
- Punctuation alag karo
- Contractions ko saath rakho YA unhe split karo
Kyun zaroorat hai: Tokenizer algorithm ko kaam karne ke liye sensible boundaries milti hain.
Step 3: Token Splitting (Core Algorithm)
Yahi woh jagah hai jahan BPE, WordPiece, ya Unigram jaese subword algorithms operate karte hain. Chaliye Byte Pair Encoding (BPE) ko first principles se derive karte hain:
Step 4: Numericalization
Kya hota hai: Har token string ko ek unique integer ID se map karo
Vocabulary: {"<pad>": 0, "<unk>": 1, "hello": 100, "world": 250 ...}
Tokens: ["hello", "world"]
Token IDs: [100, 250]
Yeh ek simple dictionary lookup hai. Special tokens:
<pad>(ID 0): Batching ke liye padding<unk>(ID 1): Unknown/rare tokens (fallback)<bos>/<eos>: Sequence ki beginning/end<mask>: Masked language modeling ke liye
Encoding aur Decoding
Acchi Tokenization Ki Key Properties

Common Tokenization Algorithms
| Algorithm | Strategy | Used By |
|---|---|---|
| BPE (Byte-Pair Encoding) | Sabse zyada frequent pairs ko iteratively merge karo | GPT-2, GPT-3, RoBERTa |
| WordPiece | BPE jaisa, lekin likelihood maximize karta hai | BERT, DistilBERT |
| Unigram | Badi vocab se shuru karo, low-probability tokens prune karo | T5, ALBERT |
| SentencePiece | Text ko raw byte stream treat karta hai, language-agnostic | T5, XLNet, bahut se multilingual models |
Alag-alag algorithms kyun?
- BPE: Simple, greedy, deterministic
- WordPiece: Zyada principled (likelihood-based), BERT-style models ke liye thoda better
- Unigram: Probabilistic tokenization allow karta hai (multiple valid splits), Japanese/Chinese mein use hota hai
- SentencePiece: Aise languages handle karta hai jahan clear word boundaries nahi hote, koi pre-tokenization ki zaroorat nahi
Recall Ek 12-Saal-Ke Bachche Ko Samjhao
Socho tum ek robot ko padhna sikha rahe ho, lekin robot sirf numbers samajhta hai, letters ya words nahi.
Problem: Tum robot ko sentence dikhate ho "I love pizza," lekin woh sirf tedhe-medhe shapes dekhta hai. Usse ek translator chahiye.
Buri solution 1: Sab kuch letters mein todo: I → 9, (space) → 0, l → 12, o → 15, v → 22, e → 5... Ab "love" hai [12, 15, 22, 5]. Poora sentence 13 numbers ki list ban jaata hai! Yeh bahut lamba hai, aur robot ko yaad rakhna hoga ki "l + o + v + e" feelings ke baare mein kuch kehta hai—super mushkil!
Buri solution 2: Har possible word ko ek number do: "I" → 1, "love" → 2, "pizza" → 3. Great! Choti list: [1, 2, 3]. Lekin kal agar tum kaho "I adore pizza"? Robot "adore" nahi jaanta kyunki humne kabhi woh number nahi sikhaya. Aur English mein 170,000 words hain—bahut zyada numbers yaad rakhne ke liye!
Smart solution (tokenization): Words ko common PIECES mein todo. Jaise Lego blocks!
- Common words saath rehte hain: "I" → 1, "love" → 2, "pizza" → 3
- Rare words pieces mein toote hain: "adore" → "ad" (17) + "ore" (18)
- Ab agar robot ne "ad" "advent" se seekha aur "ore" "explore" se, toh woh "adore" samajh sakta hai chahe yeh naya ho!
Pieces kaise chunte hain: Hum bahut saare sentences dekhte hain aur woh letter pairs dhoondh te hain jo HAMESHA saath aate hain. Jaise "th," "ing," "ed." Woh blocks ban jaate hain. Hum combine karte rehte hain jab tak hamare paas roughly 50,000 alag blocks na ho jaayein—koi bhi word build karne ke liye kaafi, lekin yaad rakhne ke liye bahut zyada nahi.
Magic moment: Jab robot "I'm running to eat pizza" padhta hai, woh jaanta hai "running" = "run" + "ing" aur "eating" = "eat" + "ing," toh woh pattern samajhta hai chahe usne pehle sirf "running" dekha ho, "eating" nahi!
Connections
- 4.2.02-BPE-algorithm - Byte Pair Encoding ka deep dive
- 4.2.03-WordPiece-and-SentencePiece - Alternative tokenization algorithms
- 4.3.01-Word-embedings - Tokens → vectors (agla step)
- 4.1.05-Vocabulary-and-OV - Vocabulary management kyun matter karta hai
- 5.1.02-Transformer-architecture - Jahaan tokenized sequences process hote hain
- 4.2.08-Multilingual-tokenization - Non-English text handle karna
- 3.4.03-Sequence-length-and-padding - Variable-length tokenized sequences se deal karna
#flashcards/ai-ml
NLP mein token kya hota hai? :: Woh basic unit of text jo ek language model process karta hai. Ek complete word, subword, character, ya byte ho sakta hai. Model ki vocabulary saare possible tokens ka set hoti hai.
Word-level ki jagah subword tokenization kyun use karte hain?
Tokenization ke 4 steps kya hain? :: (1) Normalization (text clean karo) (2) Pre-tokenization (words mein split karo) (3) Token splitting (subword algorithm jaise BPE apply karo) (4) Numericalization (integer IDs se map karo)
BPE ka core idea kya hai?
Modern LMs ke liye typical vocabulary size kya hoti hai?
<unk> tokens) aur efficiency (chhhota embedding matrix, faster softmax) ke beech balance.GPT-2 Ġ character kyun use karta hai?
Token fertility kya hoti hai?
Vocabulary bahut badi ho toh kya hota hai?
BPE aur WordPiece mein kya fark hai?
Model performance ke liye tokenization kyun important hai?
<unk> → model information kho deta hai (4) Domain mismatch hurt karta hai (jaise code vs natural language) (5) Multilingual capability affect hoti hai