Lecture 03 · N-grams
المحاضرة 03 · نماذج N-grams
Perplexity
الحيرة (Perplexity)
P1 MUSTTwo steps, one symbol, one sanity check. Learn the recipe before the notation and the exponent stops being scary.
خطوتان، ورمز واحد، وفحص تحقّق واحد. تعلّم الوصفة قبل الرموز، وسيتوقف الأُس عن كونه مخيفاً.
Forget the formula for sixty seconds
انسَ المعادلة لمدة ستين ثانية
Perplexity answers one question: on average, how many words was the model effectively choosing between at each step?
يجيب perplexity على سؤال واحد: في المتوسط، بين كم كلمة كان النموذج يختار فعلياً في كل خطوة؟
If a model is choosing between 10 things, perplexity is 10. If it is choosing between 2 things, perplexity is 2. If it is certain, perplexity is 1. That is the whole intuition, and the formula is just the arithmetic that produces that number from probabilities.
إذا كان النموذج يختار بين 10 احتمالات، فإن perplexity يساوي 10. وإذا كان يختار بين احتمالين، فهو 2. وإذا كان متأكداً تماماً، فهو 1. هذا هو كل الحدس المطلوب، والمعادلة ليست إلا العمليات الحسابية التي تُخرج هذا الرقم من الاحتمالات.
The two-step recipe
الوصفة ذات الخطوتين
You start with P(W), the probability the model assigned to the test text. You want a per-word "how many choices" number. Two steps get you there.
تبدأ من P(W)، أي الاحتمال الذي أعطاه النموذج لنص الاختبار. وما تريده هو رقم "كم خيار" لكل كلمة. خطوتان تُوصلانك إليه.
- Flip it. Compute 1 / P(W). Probability goes up, this goes down. Now a better model gets a smaller number, which is the direction we want for a "confusion" score.
اقلبه. احسب 1 / P(W). فكلما ارتفع الاحتمال، نزل هذا الرقم. وبهذا يحصل النموذج الأفضل على رقم أصغر، وهذا هو الاتجاه الذي نريده لمقياس يعبّر عن "الحيرة".
- Take the Nth root, where N is the number of words. This cancels sentence length, turning a whole-text number into a per-word number.
خُذ الجذر النوني (Nth root)، حيث N هو عدد الكلمات. هذه الخطوة تُلغي أثر طول الجملة، فتحوّل رقماً خاصاً بالنص كله إلى رقم لكل كلمة.
Step 1 by itself: flipping
الخطوة 1 وحدها: القلب
P(W) = 0.001 becomes 1 / 0.001 = 1000. A model that gave the text probability 0.01 instead would get 100. Higher probability, lower score. Good.
قيمة P(W) = 0.001 تصبح 1 / 0.001 = 1000. ولو أن نموذجاً آخر أعطى النص احتمالاً 0.01 لحصل على 100. أي احتمال أعلى يعني درجة أقل، وهذا هو المطلوب.
But 1000 is not a per-word number. It grows as the text gets longer, exactly the problem we were trying to escape.
لكن 1000 ليس رقماً لكل كلمة، لأنه يتضخّم كلما طال النص، وهذه هي بالضبط المشكلة التي كنا نحاول التخلص منها.
Step 2 by itself: the Nth root
الخطوة 2 وحدها: الجذر النوني (Nth root)
If the flipped number was built by multiplying N per-word factors together, taking the Nth root undoes the multiplication and hands back one average factor. That is a geometric mean.
إذا كان الرقم المقلوب قد تكوّن من ضرب N عاملاً، عامل لكل كلمة، فإن أخذ الجذر النوني يُلغي هذا الضرب ويُعيد لك عاملاً متوسطاً واحداً. وهذا هو بالتحديد geometric mean، أي المتوسط الهندسي.
a model that gives every word probability 1/10,
on a sentence of 3 words
P(W) = (1/10) x (1/10) x (1/10) = 1/1000 = 0.001
step 1 FLIP: 1 / 0.001 = 1000
step 2 Nth ROOT: cube root of 1000, since N = 3
1000^(1/3) = 10
PP = 10
and 10 is exactly how many words it was choosing
between at every step. the recipe recovered the
branching factor.
a 6-sided die, rolled twice
P = (1/6) x (1/6) = 1/36
step 1 FLIP: 1 / (1/36) = 36
step 2 Nth ROOT: square root of 36, since N = 2
36^(1/2) = 6
PP = 6 and the die had 6 faces.
Notice the root matched the number of rolls, not the number of faces. N counts the tokens.
لاحظ أن درجة الجذر تطابقت مع عدد الرميات، لا مع عدد أوجه حجر النرد. فـ N يَعُدّ tokens، أي الرموز في النص.
Now the formula, and why it is the same thing
الآن المعادلة، ولماذا هي الشيء نفسه
Read the exponent in two pieces.
اقرأ الأُس على جزئين منفصلين.
The minus sign is the flip
إشارة السالب هي القلب
A negative exponent means reciprocal. That is just the exponent rule:
الأُس السالب يعني المقلوب، وهذه ليست إلا قاعدة الأُسس المعروفة:
The 1/N is the Nth root
الـ 1/N هي الجذر النوني
A fractional exponent of 1/N means the Nth root. Also just an exponent rule:
الأُس الكسري 1/N يعني الجذر النوني، وهذه أيضاً مجرد قاعدة أُسس:
Put them back together
أعِد جمعهما معاً
P(W) = 0.001, N = 3 reading it as "flip then root": 1 / 0.001 = 1000 1000^(1/3) = 10 reading it as "root then flip": 0.001^(1/3) = 0.1 1 / 0.1 = 10 reading it as one exponent: 0.001^(-1/3) = 10 all three are the same arithmetic. the order of the flip and the root does not matter.
"Minus flips, one-over-N roots." Every time you see P(W)−1/N, say those four words and the notation decodes itself. The exponent is not a new idea, it is two high-school exponent rules glued together.
"Minus flips, one-over-N roots." أي: السالب يَقلِب، و1/N يُجذِّر. في كل مرة ترى P(W)−1/N، ردِّد هذه العبارة وسيفكّ الرمز نفسه من تلقاء ذاته. فالأُس ليس فكرة جديدة، بل قاعدتان من قواعد الأُسس المدرسية مُلتصقتان معاً.
The sanity check to memorize
اختبار المعقولية الذي يجب حفظه
If the model gives every word probability 1/k, then PP = k. Always. No exceptions, any N.
إذا أعطى النموذج كل كلمة احتمالاً يساوي 1/k، فإن PP = k. دائماً، بلا استثناء، ومهما كانت قيمة N.
Use it on every perplexity answer you produce. If the question implies uniform choice among k options and your answer is not k, you made an arithmetic slip.
استخدم هذه القاعدة للتحقق من كل جواب perplexity تكتبه. فإذا كان السؤال يشير إلى اختيار متساوٍ (uniform) بين k خيارات وجاء جوابك مخالفاً لـ k، فأنت أخطأت في الحساب.
Here is why it always holds, for any sentence length N:
وهذا هو سبب صحتها دائماً، لأي طول جملة N:
The N in the exponent and the N in the root cancel exactly. That is the whole reason the Nth root is there.
قيمة N في الأُس وقيمة N في الجذر تُلغيان إحداهما الأخرى تماماً، وهذا هو السبب الكامل لوجود الجذر النوني من الأساس.
| Uniform over kمنتظم على k | N | P(W) | PP |
|---|---|---|---|
| coin, k = 2عملة، k = 2 | 5 | (1/2)⁵ = 0.03125 | 2 |
| die, k = 6حجر نرد، k = 6 | 2 | (1/6)² = 0.0278 | 6 |
| digits, k = 10أرقام، k = 10 | 7 | (1/10)⁷ = 1e-7 | 10 |
| colors, k = 3ألوان، k = 3 | anyأي قيمة | (1/3)ᴺ | 3 |
| whole BERP vocab, k = 1446مفردات BERP كاملة، k = 1446 | anyأي قيمة | (1/1446)ᴺ | 1446 |
يختبر الجدول قاعدة التحقق على حالات متنوعة: عملة بـ k = 2، ونرد بـ k = 6، وأرقام بـ k = 10، وألوان بـ k = 3، وكامل vocabulary في BERP بـ k = 1446. والذي يجب أن تلاحظه: عمود PP يساوي k في كل صف، مع أن عمود N يتغير وعمود P(W) يتغير بشكل هائل. أي أن N لا يؤثر على الناتج إطلاقاً، وهذا هو جوهر القاعدة.
The last row is the worst possible language model: uniform over the vocabulary. Its perplexity equals V. Any real model should beat that, and that is the upper bound you are improving on.
الصف الأخير هو أسوأ language model ممكن: توزيع متساوٍ على كامل vocabulary. وperplexity الخاص به يساوي V. أي نموذج حقيقي يجب أن يتفوق على هذا الرقم، فهو الحد الأعلى (upper bound) الذي تعمل على تحسين النموذج مقابله.
The chain-rule form
صيغة قاعدة السلسلة
In practice you never have P(W) as one number. You have the per-word conditional probabilities, so substitute the chain rule into the formula.
عملياً لن تجد P(W) أبداً كرقم واحد جاهز، بل ستكون لديك الاحتمالات الشرطية لكل كلمة، فتُعوّض chain rule داخل المعادلة.
Read it as: take the inverse of each next-word probability, multiply them all, take the Nth root. That is the geometric mean of the inverse next-word probabilities.
اقرأها هكذا: خُذ مقلوب احتمال كل كلمة تالية، واضربها كلها، ثم خُذ الجذر النوني. وهذا هو geometric mean لمقلوبات احتمالات الكلمة التالية، أي المتوسط الهندسي لها.
Why geometric and not arithmetic? Because the probabilities were multiplied to make P(W), so the matching average undoes multiplication, not addition.
ولماذا متوسط هندسي وليس حسابياً؟ لأن الاحتمالات ضُربت في بعضها لتكوين P(W)، فالمتوسط المناسب هو الذي يَفُك الضرب، لا الذي يَفُك الجمع.
Unigram and bigram, written out
Unigram وbigram مكتوبتين بالتفصيل
Unigram
Unigram
No conditioning. Each factor depends only on the word itself.
لا يوجد شرط ولا سياق. كل عامل يعتمد على الكلمة نفسها فقط.
Bigram
Bigram
Replace the full history w<i with just the previous word.
استبدل التاريخ الكامل w<i بالكلمة السابقة وحدها.
The only change between n-gram orders is what goes after the bar. The flip and the Nth root never change.
الشيء الوحيد الذي يتغير بين رتب n-gram هو ما يأتي بعد الشرطة العمودية. أما القلب والجذر النوني فلا يتغيران أبداً.
The log-space version you actually compute with
نسخة فضاء اللوغاريتم التي تحسب بها فعلاً
Same reason as sentence probability: products of thousands of small numbers underflow. So compute the average log-probability per word and exponentiate once.
السبب هو نفس سبب احتمال الجملة: ضرب آلاف الأعداد الصغيرة يؤدي إلى underflow، أي انهيار القيمة إلى صفر في الحاسوب. لذلك نحسب متوسط log-probability لكل كلمة، ثم نأخذ الأُس مرة واحدة في النهاية.
a 4-word test sentence, bigram probabilities 0.1, 0.2, 0.5, 0.1 direct route P(W) = 0.1 x 0.2 x 0.5 x 0.1 = 0.001 flip: 1 / 0.001 = 1000 4th root (N = 4): 1000^(1/4) = 5.6234 PP = 5.62 log route ln 0.1 = -2.3026 ln 0.2 = -1.6094 ln 0.5 = -0.6931 ln 0.1 = -2.3026 sum = -6.9078 avg = -6.9078 / 4 = -1.7269 PP = exp(1.7269) = 5.6234 identical. the log route never underflows.
In that example, N = 4, so it is a fourth root. Taking a square root instead gives 10001/2 = 31.6, which is wrong by more than 5x.
في ذلك المثال N = 4، فالجذر مطلوب من الدرجة الرابعة. ولو أخذت الجذر التربيعي بدلاً منه لحصلت على 10001/2 = 31.6، وهو خطأ بأكثر من 5x، أي خمسة أضعاف.
N determines which root. N is the number of tokens in the test text, not the n in "n-gram", not the vocabulary size, not 2. A bigram model on a 12-word sentence uses a 12th root.
N هو الذي يُحدد درجة الجذر. وN هو عدد tokens في نص الاختبار، وليس الـ n الموجود في كلمة "n-gram"، ولا حجم vocabulary، ولا الرقم 2. فنموذج bigram على جملة من 12 كلمة يستخدم جذراً من الدرجة الثانية عشرة.
Boundary tokens and consistency
رموز الحدود (boundary tokens) والاتساق
A test corpus is many sentences, so the probability computation crosses sentence boundaries. Three rules:
نص الاختبار يتكوّن من جمل كثيرة، لذلك فإن حساب الاحتمال يعبر حدود الجمل. وهنا ثلاث قواعد:
- If you include
</s>(and optionally<s>) in the probability, include them in N too. Pick a convention and apply it to both the numerator and the exponent.إذا أدخلت
</s>(ومعه<s>اختيارياً) في حساب الاحتمال، فيجب أن تَعُدّها في N أيضاً. اختر اصطلاحاً واحداً وطبّقه على البسط وعلى الأُس معاً. </s>is usually counted as a token, since predicting where a sentence ends is real work the model must do.<s>is usually not counted, since it is a given context rather than something predicted.عادةً تُحسب
</s>كـ token، لأن التنبؤ بمكان انتهاء الجملة عمل حقيقي يجب على النموذج القيام به. أما<s>فلا تُحسب عادةً، لأنها سياق مُعطى سلفاً وليست شيئاً يُتنبأ به.- Perplexity comparisons require identical vocabulary and identical tokenization. Otherwise you are comparing different quantities.
مقارنة قيم perplexity تتطلب تطابق vocabulary وتطابق tokenization. وبغير ذلك تكون تقارن بين كميّتين مختلفتين أصلاً.
Perplexity is not a universal, model-free number. Two models are only comparable if they share:
قيمة perplexity ليست رقماً عالمياً مستقلاً عن النموذج. ولا يمكن مقارنة نموذجين إلا إذا اتفقا في:
- the same vocabulary, including the same
<UNK>handling (a model with a tiny vocabulary dumps rare words into<UNK>, which is easy to predict, so its perplexity looks artificially low),نفس vocabulary، بما في ذلك نفس طريقة التعامل مع
<UNK>. فالنموذج ذو vocabulary الصغير يرمي الكلمات النادرة كلها في<UNK>، والتنبؤ بها سهل، فيظهر perplexity الخاص به منخفضاً بشكل مُصطنع. - the same tokenization (word-level versus subword changes N, which changes the root),
نفس tokenization، لأن التقسيم على مستوى الكلمة مقابل التقسيم إلى subword يُغيّر N، وتغيُّر N يُغيّر درجة الجذر.
- the same boundary-token convention,
نفس اصطلاح رموز حدود الجملة (boundary tokens).
- the same test set.
نفس test set.
"Model A has perplexity 40 and model B has 170, so A is better" is only valid if all four match.
عبارة "model A له perplexity يساوي 40 وmodel B له 170، إذن A أفضل" لا تصحّ إلا إذا تطابقت الشروط الأربعة كلها.
Perplexity drops as context grows
الـ perplexity تنخفض كلما كبر السياق
Trained on Wall Street Journal, evaluated on WSJ test (slide 37):
مُدرَّب على Wall Street Journal، ومُقيَّم على test set الخاص بـ WSJ (الشريحة 37):
| Modelالنموذج | Perplexity | Effective choices per wordالخيارات الفعلية لكل كلمة | Readingالقراءة |
|---|---|---|---|
| Unigram | 962 | ≈ 962 | context ignored, nearly as lost as uniform guessingالسياق مُهمَل، وهو تائه تقريباً كالتخمين المنتظم |
| Bigram | 170 | ≈ 170 | one word of context cuts confusion by about 5.7xكلمة واحدة من السياق تخفّض الحيرة بنحو 5.7x |
| Trigram | 109 | ≈ 109 | two words of context, a further 1.6xكلمتان من السياق، تخفيض إضافي بمقدار 1.6x |
يقارن الجدول ثلاثة نماذج على نفس البيانات: unigram بـ 962، وbigram بـ 170، وtrigram بـ 109. والذي يجب أن تلاحظه: الرقم ينزل كلما زاد السياق، والرقم نفسه يُقرأ مباشرة كعدد الخيارات الفعّالة لكل كلمة. وأهم ملاحظة أن النزول غير متساوٍ: القفزة الأولى ضخمة والثانية أصغر بكثير.
Two things to take from this table.
نقطتان يجب أخذهما من هذا الجدول.
- More context means better prediction means lower perplexity.
سياق أكثر يعني تنبؤاً أفضل، والتنبؤ الأفضل يعني perplexity أقل.
- The gains shrink. 962 → 170 is a big jump, 170 → 109 is much smaller, and going to 4-grams and 5-grams needs far more training data to estimate reliably. Longer n-grams buy fluency at the cost of sparsity and parameter count.
المكاسب تتقلّص. فالانتقال من 962 إلى 170 قفزة كبيرة، أما من 170 إلى 109 فهو أصغر بكثير، والانتقال إلى 4-grams و5-grams يحتاج بيانات تدريب أكثر بكثير حتى تُقدَّر الاحتمالات بشكل موثوق. أي أن n-grams الأطول تشتري لك سلاسة في اللغة، لكن ثمنها هو sparsity، أي تَبَعْثُر البيانات، وتضخّم عدد الـ parameters.
Lower perplexity is better. Say it out loud. Perplexity means confusion, and you want a model that is less confused. Minimizing perplexity is exactly equivalent to maximizing the probability the model assigns to the test set, because the flip inverted the direction. If a question asks which model is best and the perplexities are 962, 170 and 109, the answer is 109.
perplexity الأقل هو الأفضل. قُلها بصوت مرتفع. فكلمة perplexity تعني الحيرة أو الارتباك، وأنت تريد نموذجاً أقل حيرة. وتصغير perplexity مُكافئ تماماً لتعظيم الاحتمال الذي يُعطيه النموذج لـ test set، لأن عملية القلب هي التي عكست الاتجاه. فإذا سألك سؤال عن أفضل نموذج وكانت القيم 962 و170 و109، فالجواب هو 109.
Perplexity as a weighted branching factor
الـ perplexity بوصفها عامل تفرّع موزوناً (weighted branching factor)
Branching factor means the number of possible next words. If any of V words could come next, the branching factor is V.
مصطلح branching factor يعني عدد الكلمات التالية الممكنة، أي معامل التفرّع. فإذا كان أي من V كلمة قد يأتي تالياً، فإن branching factor يساوي V.
But not all of them are equally likely. Perplexity is the effective branching factor: the number of choices the model is really weighing, after its confidence is taken into account. Concentrating probability on a few outcomes lowers the effective branching factor below V.
لكنها ليست كلها متساوية في الترجيح. وperplexity هو branching factor الفعّال: عدد الخيارات التي يوازن بينها النموذج حقيقةً، بعد أخذ درجة ثقته في الحسبان. فتركيز الاحتمال على عدد قليل من المخارج يُنزل branching factor الفعّال إلى ما دون V.
The 3-color toy example
المثال الصغير ذو الألوان الثلاثة
Slide 40. Vocabulary L = {red, blue, green}, so V = 3.
الشريحة 40. حيث vocabulary هو L = {red, blue, green}، أي أن V = 3.
Case 1: uniform, P = 1/3 each
الحالة 1: منتظم، P = 1/3 لكل لون
every token has probability 1/3, so by the sanity check PP = 3. the long way, on a 4-token sample: P(W) = (1/3)^4 = 1/81 = 0.012346 flip: 81 4th root: 81^(1/4) = 3.0 PP = 3 equal to V, the worst case
Case 2: skewed, P(red) = 0.8, P(blue) = P(green) = 0.1
الحالة 2: منحاز، P(red) = 0.8، P(blue) = P(green) = 0.1
Red is now much more predictable, so perplexity must drop below 3. Compute it two ways and confirm they agree.
أصبح red أسهل في التنبؤ بكثير، فلا بد أن ينزل perplexity إلى ما دون 3. احسبه بطريقتين وتأكد أنهما تتفقان.
route A: a representative 10-token sample
(8 reds, 1 blue, 1 green, matching the
distribution)
P(W) = 0.8^8 x 0.1 x 0.1
= 0.16777216 x 0.01
= 0.0016777216
flip: 1 / 0.0016777216 = 596.05
10th root: 596.05^(1/10) = 1.8946
PP = 1.89
route B: entropy, PP = 2^H (H in bits)
H = -(0.8 log2 0.8 + 0.1 log2 0.1 + 0.1 log2 0.1)
log2 0.8 = -0.32193 -> 0.8 x 0.32193 = 0.25754
log2 0.1 = -3.32193 -> 0.1 x 3.32193 = 0.33219
0.1 x 3.32193 = 0.33219
H = 0.25754 + 0.33219 + 0.33219 = 0.92193 bits
PP = 2^0.92193 = 1.8946
PP = 1.89
both routes: PP drops from 3.0 to about 1.9.
the model is now effectively choosing between
fewer than two options, not three.
Comparison of the two cases
مقارنة الحالتين
| Caseالحالة | P(red) | P(blue) | P(green) | Entropy (bits)الإنتروبيا (بالبتات) | PP |
|---|---|---|---|---|---|
| Uniformمنتظم | 0.333 | 0.333 | 0.333 | 1.585 | 3.00 |
| Skewedمنحاز | 0.8 | 0.1 | 0.1 | 0.922 | 1.89 |
| Certainيقيني | 1.0 | 0 | 0 | 0.000 | 1.00 |
يقارن الجدول الحالات الثلاث للألوان: متساوية، ومنحازة، ومؤكدة. والذي يجب أن تلاحظه: عمود entropy وعمود PP يتحركان في نفس الاتجاه دائماً، فكلما نزل entropy نزل PP معه. فعند 1.585 بت يكون PP = 3.00، وعند 0.922 بت يكون 1.89، وعند صفر بت يكون 1.00. والعلاقة بينهما هي PP = 2H.
Perplexity ranges from 1 (perfectly certain) up to V (no idea at all). It can never be below 1 and, for a model that uses its probabilities sensibly, never above V.
يتراوح perplexity من 1 (تأكد تام) إلى V (لا فكرة على الإطلاق). ولا يمكن أن يقل عن 1 أبداً، ولا أن يزيد على V في نموذج يستخدم احتمالاته بشكل سليم.
The zero problem, again
مشكلة الصفر، مرة أخرى
If the model assigns probability 0 to any single token in the test set, then P(W) = 0, and PP = 0−1/N = 1/0 = infinity. One unseen bigram destroys the perplexity of a million-word test set.
إذا أعطى النموذج احتمالاً يساوي 0 لأي token واحد في test set، فإن P(W) = 0، ويصبح PP = 0−1/N = 1/0 = ما لا نهاية. أي أن bigram واحداً لم يُشاهَد في التدريب يُدمّر قيمة perplexity لنص اختبار فيه مليون كلمة.
And to be completely clear about the two separate problems:
ولكي تكون المسألة واضحة تماماً، هاتان مشكلتان منفصلتان:
- Underflow is a floating-point problem. Fix: log space. Compute the average log-prob and exponentiate at the end.
Underflow مشكلة في تمثيل الأعداد العشرية في الحاسوب. وحلّها: log space. احسب متوسط log-prob ثم خُذ الأُس في النهاية.
- Zeros are a modelling problem. Fix: smoothing. Give unseen events some probability mass.
الأصفار مشكلة في النمذجة نفسها. وحلّها: smoothing. أعطِ الأحداث التي لم تُشاهَد نصيباً من كتلة الاحتمال.
Log space does not fix zeros: ln 0 = −∞, the average is −∞, and exp(+∞) is still infinite perplexity. If an exam option says "use log probabilities to avoid infinite perplexity", it is wrong.
وlog space لا يحل مشكلة الأصفار إطلاقاً: فـ ln 0 = −∞، والمتوسط يصبح −∞، وexp(+∞) يبقى perplexity لا نهائياً. فإذا رأيت خياراً في الامتحان يقول "استخدم log probabilities لتجنب perplexity اللانهائي"، فهو خيار خاطئ.
Perplexity is not everything
الـ perplexity ليست كل شيء
Perplexity is an intrinsic metric and it has limits.
إن perplexity مقياس داخلي (intrinsic)، وله حدود لا يتجاوزها.
- Lower perplexity often correlates with better downstream performance.
perplexity الأقل يرتبط غالباً بأداء أفضل في المهمة النهائية.
- It does not guarantee it. Intrinsic improvements need not transfer extrinsically.
لكنه لا يضمن ذلك. فالتحسّن في intrinsic evaluation ليس بالضرورة أن ينتقل إلى extrinsic evaluation.
- When possible, confirm with a task evaluation: WER for speech recognition, BLEU or COMET for translation.
وعند الإمكان، أكّد النتيجة بتقييم على المهمة نفسها: WER لـ speech recognition، وBLEU أو COMET للترجمة.
- And it says nothing about truthfulness, usefulness, or safety of generated text. It only measures predicted probability of held-out tokens.
وهو لا يقول شيئاً عن صِدق النص المُولَّد ولا فائدته ولا سلامته. فهو يقيس فقط الاحتمال المتوقع لـ tokens في بيانات held-out، أي البيانات المحجوزة عن التدريب.
Everything on one card
كل شيء في بطاقة واحدة
Perplexity cheat card
بطاقة مراجعة الـ perplexity
- Definition: PP(W) = P(W)−1/N.
التعريف: PP(W) = P(W)−1/N.
- Recipe: flip (1/P), then Nth root. N = number of tokens.
الوصفة: اقلب (1/P)، ثم خُذ الجذر النوني. وN = عدد tokens.
- Chain-rule form: geometric mean of 1 / P(wi | w<i).
صيغة chain rule: المتوسط الهندسي (geometric mean) لـ 1 / P(wi | w<i).
- Log form: exp(−(1/N) Σ ln P(wi | wi−1)).
الصيغة اللوغاريتمية: exp(−(1/N) Σ ln P(wi | wi−1)).
- Sanity check: all probabilities 1/k means PP = k.
فحص التحقق: إذا كانت كل الاحتمالات 1/k، فإن PP = k.
- Direction: lower is better. Minimizing PP = maximizing test-set probability.
الاتجاه: الأقل هو الأفضل. وتصغير PP يكافئ تعظيم احتمال test set.
- Meaning: weighted (effective) branching factor.
المعنى: branching factor المُوزَّن، أي الفعّال.
- Range: 1 at best, V for uniform, infinite if any token gets probability 0.
المدى: 1 في أفضل حال، وV في حالة التوزيع المتساوي، ولا نهاية إذا حصل أي token على احتمال 0.
- Comparable: only across identical vocabulary, tokenization, boundary convention and test set.
قابلية المقارنة: فقط عند تطابق vocabulary وtokenization واصطلاح رموز الحدود وtest set.
- WSJ: unigram 962, bigram 170, trigram 109.
WSJ: unigram 962، وbigram 170، وtrigram 109.
Check yourself
اختبر نفسك
Unigram PP = 962, bigram PP = 170, trigram PP = 109 on WSJ. Which model is best?
على WSJ: unigram PP = 962، وbigram PP = 170، وtrigram PP = 109. أي نموذج هو الأفضل؟
Lower perplexity is better. Perplexity is confusion, or effective branching factor, so less is better. Because of the flip in the definition, minimizing perplexity is exactly equivalent to maximizing the probability the model assigns to the test data. Option A is the inverted-direction error, the single most common mistake on this topic.
perplexity الأقل هو الأفضل. فـ perplexity يعبّر عن الحيرة، أو عن branching factor الفعّال، ولذلك فالأقل أفضل. وبسبب القلب الموجود في التعريف، فإن تصغير perplexity مُكافئ تماماً لتعظيم الاحتمال الذي يُعطيه النموذج لبيانات الاختبار. والخيار A هو خطأ عكس الاتجاه، وهو أشهر خطأ منفرد في هذا الموضوع.
A bigram model assigns a 4-word test sentence the probability P(W) = 0.0016. What is the perplexity?
نموذج bigram أعطى جملة اختبار من 4 كلمات احتمالاً P(W) = 0.0016. فما قيمة perplexity؟
N is the number of tokens, which is 4, so you take the fourth root: 6251/4 = 5. Option A takes a square root and is off by 5x. Option D is the other version of the same error: the "n" in n-gram has nothing to do with the N in the exponent. Check it with the sanity rule: 0.0016 = (1/5)4, every word had probability 1/5, so PP must be 5.
إن N هو عدد tokens، وهو 4، فتأخذ الجذر من الدرجة الرابعة: 6251/4 = 5. والخيار A يأخذ الجذر التربيعي فيخطئ بمقدار 5x. والخيار D هو الصورة الأخرى لنفس الخطأ: فالـ n في n-gram لا علاقة له إطلاقاً بـ N الموجود في الأُس. وتحقق من الجواب بقاعدة التحقق: 0.0016 = (1/5)4، أي أن كل كلمة كان احتمالها 1/5، فلا بد أن يكون PP يساوي 5.
A model gives every word in a 12-word test sentence probability exactly 1/8. What is PP?
نموذج يُعطي كل كلمة في جملة اختبار من 12 كلمة احتمالاً يساوي 1/8 بالضبط. فما قيمة PP؟
The memorized sanity check: every probability 1/k gives PP = k, for any N. Algebraically P(W) = (1/8)12, and ((1/8)12)−1/12 = (1/8)−1 = 8. The 12 in the exponent and the 12 in the root cancel, which is the entire purpose of the Nth root. Options A and B confuse sentence length with branching factor.
هذا هو فحص التحقق المحفوظ: إذا كان كل احتمال يساوي 1/k، فإن PP = k، لأي قيمة N. وجبرياً: P(W) = (1/8)12، ومنها ((1/8)12)−1/12 = (1/8)−1 = 8. فالرقم 12 في الأُس والرقم 12 في الجذر يُلغيان أحدهما الآخر، وهذا هو الغرض الكامل من الجذر النوني. والخياران A وB يخلطان بين طول الجملة وbranching factor.
What does the minus sign in the exponent of P(W)−1/N accomplish?
ما الذي تؤديه الإشارة السالبة في أُس P(W)−1/N؟
x−a = 1/xa, so the minus sign is the flip. The 1/N is what normalizes for length (option C describes the root, not the minus). Perplexity is always at least 1 and never negative, so option B is impossible.
بما أن x−a = 1/xa، فالإشارة السالبة هي عملية القلب. أما 1/N فهو الذي يُسوّي أثر الطول، فالخيار C يصف الجذر لا الإشارة السالبة. وperplexity لا يقل أبداً عن 1 ولا يكون سالباً إطلاقاً، فالخيار B مستحيل.
Over the vocabulary {red, blue, green}, a model moves from uniform (1/3 each) to P(red) = 0.8, P(blue) = P(green) = 0.1. What happens to perplexity?
على vocabulary المكوّن من {red, blue, green}، انتقل نموذج من التوزيع المتساوي (1/3 لكل لون) إلى P(red) = 0.8 وP(blue) = P(green) = 0.1. فماذا يحدث لـ perplexity؟
Via entropy: H = 0.922 bits, so PP = 20.922 = 1.89. Via a representative 10-token sample of 8 reds, 1 blue, 1 green: P(W) = 0.88 × 0.01 = 0.00167772, flip to 596.05, tenth root gives 1.8946. Option A is the error of confusing perplexity with vocabulary size: V is only the upper bound, reached when the model is uniform. Option D is impossible because perplexity is never below 1.
عن طريق entropy: H = 0.922 بت، إذن PP = 20.922 = 1.89. وعن طريق عيّنة تمثيلية من 10 tokens فيها 8 red و1 blue و1 green: P(W) = 0.88 × 0.01 = 0.00167772، ثم القلب يعطي 596.05، والجذر من الدرجة العاشرة يعطي 1.8946. والخيار A هو خطأ الخلط بين perplexity وحجم vocabulary: فـ V هو الحد الأعلى فقط، ولا يُبلَغ إلا عندما يكون النموذج متساوي التوزيع. والخيار D مستحيل لأن perplexity لا ينزل أبداً عن 1.
Your bigram model assigns probability 0 to one bigram in a 50,000-token test set. You switch the computation to log space to be safe. What is the perplexity?
نموذج bigram لديك يُسند احتمالاً 0 إلى bigram واحد في مجموعة اختبار من 50,000 token. تنقل الحساب إلى فضاء اللوغاريتم احتياطاً. كم تكون الـ perplexity؟
P(W) = 0 gives PP = 0−1/N = 1/0, which is infinite. In log space, ln 0 = −∞, the average is −∞, and exp(+∞) is still infinite. Log space solves underflow only. Zeros are a modelling problem fixed by smoothing. Option C inverts the direction: P = 0 is the worst case, so PP is maximal, not minimal.
P(W) = 0 يعطي PP = 0−1/N = 1/0، وهي لا نهائية. وفي فضاء اللوغاريتم ln 0 = −∞، فيكون المتوسط −∞، وexp(+∞) يبقى لا نهائياً. فضاء اللوغاريتم يحل مشكلة underflow فقط. أما الأصفار فمشكلة في النمذجة يحلها smoothing. الخيار C يعكس الاتجاه: P = 0 هي أسوأ حالة، فتكون PP في أقصاها لا في أدناها.
Team A reports perplexity 42 with a 10k-word vocabulary and aggressive <UNK> mapping. Team B reports 170 with a 60k-word vocabulary on the same raw text. Which conclusion is sound?
الفريق A يُبلغ عن perplexity قدرها 42 بمفردات من 10k كلمة وتحويل مكثف إلى <UNK>. والفريق B يُبلغ عن 170 بمفردات من 60k كلمة على النص الخام نفسه. أي استنتاج سليم؟
Perplexity comparisons are only valid across identical vocabulary and tokenization (and the same boundary convention and test set). Team A's small vocabulary funnels rare words into <UNK>, which is easy to predict, so its perplexity is artificially deflated. The two numbers measure different quantities and no arithmetic on them is meaningful.
مقارنات الـ perplexity لا تصح إلا مع مفردات وتقطيع متطابقين (ومع اصطلاح الحدود نفسه ومجموعة الاختبار نفسها). مفردات الفريق A الصغيرة تحوّل الكلمات النادرة إلى <UNK>، وهو سهل التنبؤ، فتنخفض الـ perplexity لديه انخفاضاً مصطنعاً. الرقمان يقيسان كميتين مختلفتين، ولا معنى لأي عملية حسابية عليهما.