Lecture 03 · N-grams

المحاضرة 03 · نماذج N-grams

Perplexity

الحيرة (Perplexity)

Slides 33–40الشرائح 33–40 P1 MUST

Two 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)، أي الاحتمال الذي أعطاه النموذج لنص الاختبار. وما تريده هو رقم "كم خيار" لكل كلمة. خطوتان تُوصلانك إليه.

  1. 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). فكلما ارتفع الاحتمال، نزل هذا الرقم. وبهذا يحصل النموذج الأفضل على رقم أصغر، وهذا هو الاتجاه الذي نريده لمقياس يعبّر عن "الحيرة".

  2. 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، أي المتوسط الهندسي.

Workedمثال محلول
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.
Workedمثال محلول
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، أي الرموز في النص.

P(W) 0.001 FLIP 1 / P inverse probability 1000 Nth ROOT N = 3 PP(W) 10 choices per word THE RECIPE both steps as one symbol: PP(W) = P(W)^(-1/N) the minus sign is the flip, the 1/N is the root
Flip, then take the Nth root. The exponent −1/N is only shorthand for those two moves.اقلب، ثم خذ الجذر النوني. الأس −1/N ليس إلا اختصاراً لهاتين الحركتين.

Now the formula, and why it is the same thing

الآن المعادلة، ولماذا هي الشيء نفسه

PP(W) = P(W)−1/N

Read the exponent in two pieces.

اقرأ الأُس على جزئين منفصلين.

The minus sign is the flip

إشارة السالب هي القلب

A negative exponent means reciprocal. That is just the exponent rule:

الأُس السالب يعني المقلوب، وهذه ليست إلا قاعدة الأُسس المعروفة:

x−a = 1xa so P(W)−1/N = 1P(W)1/N

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 يعني الجذر النوني، وهذه أيضاً مجرد قاعدة أُسس:

x1/N = the N-th root of x x1/2 = √x   x1/3 = ∛x   x1/4 = ⁴√x

Put them back together

أعِد جمعهما معاً

Workedمثال محلول
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.
Memory hookخطّاف للذاكرة

"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

اختبار المعقولية الذي يجب حفظه

Memorize thisاحفظ هذا

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:

P(W) = (1k)N PP = ((1k)N)−1/N = (1k)N · (−1/N) = (1k)−1 = k

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منتظم على kNP(W)PP
coin, k = 2عملة، k = 25(1/2)⁵ = 0.031252
die, k = 6حجر نرد، k = 62(1/6)² = 0.02786
digits, k = 10أرقام، k = 107(1/10)⁷ = 1e-710
colors, k = 3ألوان، k = 3anyأي قيمة(1/3)ᴺ3
whole BERP vocab, k = 1446مفردات BERP كاملة، k = 1446anyأي قيمة(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 داخل المعادلة.

PP(W) = N-th root of  ∏i=1..N  1P(wi | w<i)

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

PP(W) = N-th root of ∏ 1P(wi)

No conditioning. Each factor depends only on the word itself.

لا يوجد شرط ولا سياق. كل عامل يعتمد على الكلمة نفسها فقط.

Bigram

Bigram

PP(W) = N-th root of ∏ 1P(wi | wi−1)

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 لكل كلمة، ثم نأخذ الأُس مرة واحدة في النهاية.

PP(W) = exp( −1N · Σ ln P(wi | wi−1) )
Workedمثال محلول
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.
Trapفخ

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:

نص الاختبار يتكوّن من جمل كثيرة، لذلك فإن حساب الاحتمال يعبر حدود الجمل. وهنا ثلاث قواعد:

Trapفخ

Perplexity is not a universal, model-free number. Two models are only comparable if they share:

قيمة perplexity ليست رقماً عالمياً مستقلاً عن النموذج. ولا يمكن مقارنة نموذجين إلا إذا اتفقا في:

"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النموذجPerplexityEffective choices per wordالخيارات الفعلية لكل كلمةReadingالقراءة
Unigram962≈ 962context ignored, nearly as lost as uniform guessingالسياق مُهمَل، وهو تائه تقريباً كالتخمين المنتظم
Bigram170≈ 170one word of context cuts confusion by about 5.7xكلمة واحدة من السياق تخفّض الحيرة بنحو 5.7x
Trigram109≈ 109two words of context, a further 1.6xكلمتان من السياق، تخفيض إضافي بمقدار 1.6x

يقارن الجدول ثلاثة نماذج على نفس البيانات: unigram بـ 962، وbigram بـ 170، وtrigram بـ 109. والذي يجب أن تلاحظه: الرقم ينزل كلما زاد السياق، والرقم نفسه يُقرأ مباشرة كعدد الخيارات الفعّالة لكل كلمة. وأهم ملاحظة أن النزول غير متساوٍ: القفزة الأولى ضخمة والثانية أصغر بكثير.

Two things to take from this table.

نقطتان يجب أخذهما من هذا الجدول.

Trapفخ

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.

UNIFORM SKEWED red blue green 0.3330.3330.333 three equally live options PP = 3.0 red blue green 0.80.10.1 still 3 options, but barely 2 live ones PP ≈ 1.9
Same vocabulary of size 3 in both cases. Confidence, not vocabulary size, sets the effective branching factor.المفردات نفسها بحجم 3 في الحالتين. الثقة، لا حجم المفردات، هي التي تحدد عامل التفرّع الفعلي.

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 لكل لون

Workedمثال محلول
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. احسبه بطريقتين وتأكد أنهما تتفقان.

Workedمثال محلول
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.3330.3330.3331.5853.00
Skewedمنحاز0.80.10.10.9221.89
Certainيقيني1.0000.0001.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

مشكلة الصفر، مرة أخرى

Trapفخ

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:

ولكي تكون المسألة واضحة تماماً، هاتان مشكلتان منفصلتان:

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)، وله حدود لا يتجاوزها.

Everything on one card

كل شيء في بطاقة واحدة

Perplexity cheat card

بطاقة مراجعة الـ perplexity

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 لديه انخفاضاً مصطنعاً. الرقمان يقيسان كميتين مختلفتين، ولا معنى لأي عملية حسابية عليهما.