4.2.9 · AI-ML › Tokenization & Language Modeling
Intuition Ek-sentence ka idea
Perplexity measure karta hai ki ek language model real text se kitna "surprised" hota hai. Agar model ne jo actually next aaya usse high probability di, toh woh surprised nahi hua, aur perplexity low hogi. Isse socho ek effective number of equally-likely choices ki tarah — jitne options ke beech model har step pe fansa hota hai. Lower = zyada confident aur accurate.
Ek language model jo N tokens ki sequence ko probability P ( w 1 , w 2 , … , w N ) deta hai, uske liye perplexity hai
PPL = P ( w 1 , … , w N ) − N 1
Yeh geometric mean of the inverse per-token probabilities hai. K ki perplexity ka matlab hai ki model utna hi uncertain hai jaise woh har position pe ==K == equally likely tokens mein se uniformly choose kar raha ho.
− 1/ N ki yeh ajeeb power kyun?
Raw sequence probability P ( w 1 , … , w N ) jaise-jaise N badhta hai, 0 ki taraf shrink hoti jaati hai (aap bahut se numbers < 1 multiply karte ho). Toh yeh alag-alag length ke texts ko compare nahi kar sakti.
==N -th root== lene se product ek per-token average (geometric mean) ban jaata hai.
Negative exponent "probability" (bada = better) ko "perplexity" (bada = worse) mein flip karta hai, toh yeh ek cost ki tarah behave karta hai.
Hum perplexity ko information theory se step by step build karte hain.
Step 1 — Ek event ki Surprise.
p probability wale event ka information content (surprisal) − log 2 p bits hota hai.
Kyun? Rare events (chhota p ) zyada information carry karte hain; − log surprise ko bada banata hai jab p chhota ho aur 0 jab p = 1 ho.
Step 2 — Average surprise = cross-entropy.
Chain rule use karke, P ( w 1 , … , w N ) = ∏ i = 1 N P ( w i ∣ w < i ) .
Average per-token cross-entropy (bits mein) hai
H = − N 1 ∑ i = 1 N log 2 P ( w i ∣ w < i )
Average kyun? Hum ek length-independent "typical surprise per token" chahte hain.
Step 3 — Perplexity bas 2 ko us entropy par raise karna hai.
PPL = 2 H = 2 − N 1 ∑ i l o g 2 P ( w i ∣ w < i )
Step 4 — Dikhao ki yeh definition ke barabar hai.
2 − N 1 ∑ i l o g 2 P i = 2 l o g 2 ( ∏ i P i ) − 1/ N = ( i ∏ P i ) − 1/ N = P ( w 1 , … , w N ) − 1/ N
Toh dono definitions identical hain. ✅
Intuition "Effective number of choices" kyun
Agar model V tokens pe perfectly uniform hota, toh har P i = 1/ V hoga, toh H = log 2 V aur PPL = 2 l o g 2 V = V . Us worst case mein perplexity literally vocabulary size recover karta hai — yeh woh branching factor hai jiska model saamna karta hai.
Worked example 2) Teen-token sentence, concrete numbers
Model P ( w 1 ) = 0.5 , P ( w 2 ∣ w 1 ) = 0.25 , P ( w 3 ∣ w < 3 ) = 0.1 deta hai.
Product P = 0.5 ⋅ 0.25 ⋅ 0.1 = 0.0125 . Kyun? Chain rule.
N = 3 , toh PPL = 0.012 5 − 1/3 .
log : ln ( 0.0125 ) ≈ − 4.382 ; 3 se divide karo → − 1.4608 ; negate karo → 1.4608 ; e 1.4608 ≈ 4.31 .
PPL ≈ 4.31 . Yeh step kyun? { 0.5 , 0.25 , 0.1 } ka geometric mean ≈ 0.232 hai, aur 1/0.232 ≈ 4.31 — perplexity typical per-token probability ka reciprocal hai.
Recall Forecast-then-Verify
Aage padhne se pehle: ek model kisi aise token ko probability 0 assign karta hai jo actually appear hota hai. Uski perplexity predict karo.
...
− log ( 0 ) = + ∞ ⇒ PPL = ∞ . Ek impossible-but-real token poora score barbad kar deta hai. Yahi reason hai ki hum smooth karte hain / kabhi zero probabilities allow nahi karte .
Common mistake "Lower entropy → higher perplexity"
Kyun sahi lagta hai: entropy aur perplexity sound mein opposites lagte hain, toh log direction flip kar dete hain. Fix: PPL = b H H mein monotonically increasing hai. Low entropy (confident model) → low perplexity. Same direction, opposite nahi.
Common mistake "Alag tokenizers/vocabularies mein perplexities compare kar sakte hain"
Kyun sahi lagta hai: yeh bas ek number hai, compare kyun nahi? Fix: perplexity per token hoti hai. Fine sub-word tokens wale model ke paas sentence per zyada tokens hote hain, N aur per-token distribution change ho jaata hai. Perplexities sirf same tokenization aur same text pe compare karo. Warna bits-per-byte/character use karo.
Common mistake "PPL natural log use karta hai, toh answer nats mein hai" isliye bases mix karna
Kyun sahi lagta hai: libraries ln use karti hain. Fix: base tab cancel hota hai jab aap same base se exponentiate karo: e H nats = 2 H bits . ln lo phir 2 ko uspe raise mat karo.
Common mistake "Perplexity directly text quality / fluency measure karta hai"
Kyun sahi lagta hai: better models mein aksar lower PPL hoti hai. Fix: PPL sirf held-out data pe probability calibration measure karta hai. Ek model low PPL rakh ke bhi boring text produce kar sakta hai (yeh safe, frequent token ko reward karta hai). Yeh ek proxy hai, ground truth nahi.
Intuition Agar sirf teen cheezein yaad rakhni hain
PPL = exp ( average negative log-likelihood ) — surprise, exponentiated.
Lower is better ; yeh effective branching factor hai (choices ki number).
Sirf same data + tokenizer pe compare karo; zero-probability tokens → ∞ .
"PPL = Puzzle Per Line." Perplexity = model har step pe kitne equally-good options ke beech puzzle kar raha hai. Zyada puzzle = worse. Aur yeh hai P erfect P roduct's L oosening: 1/ p ka geometric mean.
Recall Feynman: 12-saal ke bachche ko samjhao
Socho tum ek story mein agla word guess kar rahe ho. Agar tum ek great guesser ho, real word usually wahi hota hai jo tumhe likely laga tha — tum rarely shocked hote ho. Perplexity yeh count karna jaisa hai ki tum har baar kitne words ke beech "torn" the. Agar tum 2 words ke beech torn the, perplexity 2 hai. Agar tum sure the aur hamesha sahi rahe, toh 1 hai (koi confusion nahi). Ek bura guesser hazaaron words ke beech torn hota hai, toh unki perplexity bahut badi hoti hai. Chhota number = smarter guesser.
Ek line mein perplexity kya hai? Exponentiated average per-token negative log-likelihood; equally-likely tokens ki effective number jinmein se model choose kar raha hota hai.
Sequence probability se perplexity ka formula? PPL = P ( w 1 , … , w N ) − 1/ N .
Perplexity aur cross-entropy H ka relation? PPL = b H jahan H base-b logs mein average per-token cross-entropy hai.
− 1/ N power kyun lete hain?N -th root isse per-token geometric mean banata hai (length-independent); negative probability ko cost mein flip karta hai.
Lower ya higher perplexity better hai? Lower better hai — model real text se kam surprised hota hai.
Vocab size V pe uniform model ki perplexity? Exactly V (worst case; branching factor recover karta hai).
Agar model ek real token ko probability 0 deta hai toh kya hoga? Perplexity ∞ ho jaati hai; yahi smoothing / zero probabilities avoid karne ki motivation hai.
Alag tokenizers mein perplexities compare kar sakte hain? Nahi — PPL per-token hai, toh N aur distribution change ho jaata hai; sirf identical text aur tokenization pe compare karo.
Code mein natural log se PPL kaise compute hoti hai? PPL = exp ( N 1 ∑ i − ln P ( w i ∣ w < i ) ) .
Kya log base final perplexity affect karta hai? Nahi, jab tak same base se exponentiate karo (e H na t s = 2 H bi t s ).
Sequence probability P w1..wN
Chain rule product of P wi
Master relation PPL equals b^H
Log base b matches exponent
Effective number of choices
Uniform case PPL equals V