Lecture 03 · N-grams
المحاضرة 03 · نماذج N-grams
Chain Rule and the Markov Assumption
قاعدة السلسلة (Chain Rule) وافتراض ماركوف (Markov Assumption)
P2 SHOULDHow an exact but uncomputable formula becomes a short, countable one, and the off-by-one that the examiner is hunting for.
كيف تتحول معادلة دقيقة لكن غير قابلة للحساب إلى معادلة قصيرة يمكن عدّها، وما هو خطأ off-by-one الذي يتربص به الممتحن.
The goal, and why it fails
الهدف، ولماذا يفشل
Slide 10 states the goal plainly: compute P(w | h), the probability of a word w given a history h. Concretely:
تضع الشريحة 10 الهدف بوضوح: احسب P(w | h)، أي احتمال كلمة w بشرط تاريخ سابق h. وبشكل ملموس:
P(blue | The water of Walden Pond is so beautifully)
The obvious plan is to count. Go find every occurrence of the history in a giant corpus, count how many times blue followed it, divide. Slide 10 says that is impossible, for two reasons it lists explicitly:
الخطة البديهية هي العدّ: ابحث عن كل ظهور لهذا التاريخ في corpus ضخم، واحسب كم مرة جاءت blue بعده، ثم اقسم. تقول الشريحة 10 إن هذا مستحيل، لسببين تذكرهما صراحة:
- Histories are too long. There is no bound on how much text precedes a word.
التواريخ طويلة جداً. لا يوجد أي حد أعلى لمقدار النص الذي يسبق الكلمة.
- Most exact histories never occur. Language is creative. "The water of Walden Pond is so beautifully" has almost certainly appeared zero times in whatever corpus you have. Zero occurrences means you cannot divide, and anything you do manage to count is based on one or two examples.
معظم التواريخ الدقيقة لا تظهر أبداً. اللغة إبداعية: عبارة "The water of Walden Pond is so beautifully" لم تظهر على الأغلب ولا مرة واحدة في أي corpus لديك. والظهور الصفري يعني أنك لا تستطيع القسمة من الأساس، وأي عدّ تنجح في الحصول عليه سيكون مبنياً على مثال أو مثالين فقط.
Note which problem is the real killer. It is not that long histories are slow to count. It is that the count is zero, so there is no estimate at all. This is sparsity, and it will come back on every page after this one.
لاحظ أي المشكلتين هي القاتلة فعلاً. المشكلة ليست أن التواريخ الطويلة بطيئة في العدّ، بل أن العدّ يساوي صفراً، فلا يوجد تقدير من الأساس. هذه هي مشكلة sparsity، وستعود إليك في كل صفحة بعد هذه الصفحة.
The chain rule: exact, and that is the problem
قاعدة السلسلة: دقيقة، وهذه هي المشكلة
Slide 11 gives the chain rule for sequences:
تعطي الشريحة 11 قاعدة chain rule للمتسلسلات:
P(w1:n) = ∏k=1..n P(wk | w1:k-1)
Read it as: the probability of a whole sentence equals the product of the probability of each word given everything before it. The slide's own expansion:
اقرأها كالتالي: احتمال الجملة كاملة يساوي حاصل ضرب احتمال كل كلمة بشرط كل ما جاء قبلها. وهذا هو التوسيع الذي تعرضه الشريحة نفسها:
P("its water is so transparent")
= P(its)
× P(water | its)
× P(is | its water)
× P(so | its water is)
× P(transparent| its water is so)
Two facts about this that must not get merged:
حقيقتان عن هذا الأمر يجب ألا تختلطا عندك أبداً:
The chain rule is exact
قاعدة السلسلة دقيقة
No approximation anywhere. It is a plain identity from the definition of conditional probability. If you knew every factor exactly, the product would be the exact sentence probability.
لا يوجد أي تقريب في أي مكان هنا. إنها مطابقة رياضية مباشرة مشتقة من تعريف conditional probability. ولو عرفت كل عامل بدقة تامة، فإن حاصل الضرب سيعطيك احتمال الجملة الدقيق تماماً.
Nothing is lost here. There is no "chain rule assumption".
لا شيء يُفقد هنا، ولا يوجد شيء اسمه "chain rule assumption".
The chain rule is useless alone
قاعدة السلسلة وحدها عديمة الفائدة
Look at the last factor: P(transparent | its water is so). Its history is three words long and the next sentence's will be longer. We have just renamed the problem from slide 10, not solved it.
انظر إلى العامل الأخير: P(transparent | its water is so). تاريخه بطول ثلاث كلمات، وفي الجملة التالية سيكون أطول. نحن أعدنا تسمية مشكلة الشريحة 10 فقط، ولم نحلها.
Slide 11's second bullet says it: "Still not computable for long contexts without approximation."
والنقطة الثانية في الشريحة 11 تقولها بصراحة: "Still not computable for long contexts without approximation."
If a question asks "which step introduces the approximation", the answer is the Markov assumption, never the chain rule. The chain rule is an exact decomposition. Students who learned the two slides as one blur say "the chain rule approximates the sentence probability". That is wrong and it is a free mark for the grader.
إذا سألك السؤال "أي خطوة هي التي تُدخل التقريب؟"، فالجواب هو Markov assumption، وليس chain rule أبداً. فـchain rule هو تفكيك دقيق تماماً. الطلاب الذين حفظوا الشريحتين ككتلة واحدة ضبابية يقولون إن chain rule يقرّب احتمال الجملة، وهذا خطأ، وهو درجة مجانية تمنحها للمصحح.
The Markov assumption: just truncate the history
افتراض ماركوف: اقطع التاريخ فحسب
Slide 12 is the pivot of the entire lecture. The fix is blunt: throw away all but the last few words of the history.
الشريحة 12 هي محور المحاضرة كلها. والحل فيها صريح وبسيط: تخلّص من التاريخ كله إلا آخر كلمات قليلة منه.
P(wn | w1:n-1) ≈ P(wn | wn-N+1:n-1)
The left side conditions on the entire history from word 1. The right side conditions only on the window starting at position n - N + 1. That window has length (n - 1) - (n - N + 1) + 1 = N - 1 words.
الطرف الأيسر من المعادلة يشترط على التاريخ كامله بدءاً من الكلمة الأولى، أما الطرف الأيمن فيشترط فقط على النافذة التي تبدأ عند الموضع n - N + 1. وطول هذه النافذة هو (n - 1) - (n - N + 1) + 1 = N - 1 كلمة.
Slide 12's two instances:
الحالتان المذكورتان في الشريحة 12:
- Bigram (N = 2): P(wn | wn-1). One word of context.
Bigram (N = 2): P(wn | wn-1)، أي كلمة واحدة من context.
- Trigram (N = 3): P(wn | wn-2, wn-1). Two words of context.
Trigram (N = 3): P(wn | wn-2, wn-1)، أي كلمتان من context.
The Key-idea box on slide 12 says the history is approximated by "the last n - 1 words", with a lowercase n, but the formula directly underneath it uses uppercase N for the model order: P(wn | wn-N+1:n-1).
صندوق Key-idea في الشريحة 12 يقول إن التاريخ يُقرَّب بـ"the last n - 1 words" بحرف n صغير، لكن المعادلة الموجودة تحته مباشرة تستخدم N الكبير لرتبة النموذج: P(wn | wn-N+1:n-1).
The formula is the correct one. The window is the last N - 1 words, where N is the n-gram order. Lowercase n in this notation is the position of the word being predicted, not the order of the model. So "the last n - 1 words" in the prose literally reads as "all the words before position n", which is the un-truncated history the slide is trying to get rid of.
المعادلة هي الصحيحة. النافذة هي آخر N - 1 كلمة، حيث N هي رتبة n-gram. أما n الصغير في هذا الترميز فهو موضع الكلمة التي نتوقعها، وليس رتبة النموذج. ولذلك فإن عبارة "the last n - 1 words" تُقرأ حرفياً على أنها "كل الكلمات التي تسبق الموضع n"، وهي بالضبط التاريخ غير المقتطع الذي تحاول الشريحة التخلص منه.
Keep the two symbols apart for the whole exam: N = order of the model (2 for bigram), n = index of the current word in the sentence.
افصل بين الرمزين طوال الاختبار: N هي رتبة النموذج (2 في حالة bigram)، وn هي ترتيب الكلمة الحالية داخل الجملة.
The off-by-one, stated once and properly
خطأ الفرق بواحد (off-by-one)، مشروحاً مرة واحدة وبشكل صحيح
N-gram order = context length + 1. Equivalently, context length = N - 1.
رتبة n-gram = طول context + 1. أو بالصيغة المقابلة: طول context = N - 1.
Why: the window of N tokens includes the word you are predicting. Only N - 1 of the N tokens are history.
والسبب: نافذة الـN tokens تشمل الكلمة التي تتوقعها أنت، فلا يبقى منها كتاريخ إلا N - 1 فقط.
Say it as: "bigram, one back. Trigram, two back." The name is always one bigger than the number of words you look at.
رددها على نفسك هكذا: "bigram, one back. Trigram, two back." الاسم دائماً أكبر بواحد من عدد الكلمات التي تنظر إليها فعلاً.
| Modelالنموذج | N | Context wordsكلمات السياق | Conditional formالصيغة الشرطية | Window on "... is so transparent"النافذة على "... is so transparent" |
|---|---|---|---|---|
| unigram | 1 | 0 | P(wn) | transparent |
| bigram | 2 | 1 | P(wn | wn-1) | so transparent |
| trigram | 3 | 2 | P(wn | wn-2, wn-1) | is so transparent |
| 4-gram | 4 | 3 | P(wn | wn-3, wn-2, wn-1) | water is so transparent |
يقارن الجدول أربعة نماذج: unigram وbigram وtrigram و4-gram، ويعطي لكل منها قيمة N وعدد كلمات context والصيغة الشرطية والنافذة الفعلية على الجملة. الذي يجب أن تلاحظه: عمود N دائماً أكبر بواحد من عمود كلمات context، وunigram سياقه صفر تماماً، وعمود النافذة يضم الكلمة المتوقعة نفسها فيكون طوله N لا N - 1.
Slide 13's example, read correctly
مثال الشريحة 13، مقروءاً بشكل صحيح
Slide 13 shows the bigram approximation on the Walden sentence:
تعرض الشريحة 13 تقريب bigram مطبقاً على جملة Walden:
P(blue | The water of Walden Pond is so beautifully) ≈ P(blue | beautifully)
Eight words of history go in, one word survives. That is the trade slide 13's bullets name: we trade long-range context for learnability from finite data. Larger N keeps more context but makes counts sparser, so you have less data per n-gram.
ثماني كلمات من التاريخ تدخل، وكلمة واحدة فقط تبقى. وهذه هي المقايضة التي تسميها نقاط الشريحة 13: نحن نقايض long-range context مقابل القدرة على التعلم من بيانات محدودة. فكلما كبرت N حافظت على سياق أطول، لكن صارت الأعداد أكثر تشتتاً، فتقل البيانات المتوفرة لكل n-gram.
Sentence probability under a bigram model
احتمال الجملة في نموذج bigram
Slide 14 gives the bigram sentence formula and, critically, the boundary tokens:
تعطي الشريحة 14 معادلة احتمال الجملة في نموذج bigram، والأهم من ذلك: boundary tokens.
P(w1:n) ≈ ∏k=1..n P(wk | wk-1)
The slide's two bullets are both exam-worthy:
نقطتا الشريحة كلتاهما تستحقان الحفظ للاختبار:
- You need boundary tokens <s> and </s>. Without <s> the first word has no predecessor, so P(w1 | w0) is undefined. <s> supplies w0.
تحتاج إلى boundary tokens وهما <s> و</s>. بدون <s> لا تجد الكلمة الأولى أي كلمة تسبقها، فيصبح P(w1 | w0) غير معرّف. و<s> هو الذي يوفر w0.
- </s> makes it a proper distribution over all sentence lengths. Without an end token, the probabilities of all sentences of every length would not sum to 1, because nothing ever charges you for stopping. </s> is the "I am finished" event and it has to carry probability mass.
</s> هو ما يجعلها proper distribution على كل أطوال الجمل. بدون end token لن يكون مجموع احتمالات كل الجمل بكل أطوالها مساوياً للواحد، لأن لا شيء يحاسبك على التوقف. فـ</s> هو حدث "انتهيت"، ولا بد أن يحمل نصيباً من probability mass.
Factor <s> its water is so transparent </s> under a bigram model. Pad first, then slide a width-2 window across.
فكّك <s> its water is so transparent </s> باستخدام نموذج bigram. أضف padding أولاً، ثم مرّر نافذة بعرض 2 على الجملة.
padded: <s> its water is so transparent </s> index: 0 1 2 3 4 5 6 bigram factors, one per real token plus the end token: P(<s> its water is so transparent </s>) = P(its | <s>) x P(water | its) x P(is | water) x P(so | is) x P(transparent | so) x P(</s> | transparent) factors = 6
Count check. The padded string has 7 tokens. A bigram model produces one factor per bigram, and the number of bigrams in a 7-token string is 7 - 2 + 1 = 6. The factors match. Notice <s> never appears to the left of the bar as a predicted word, and </s> never appears as context. They are an opening bracket and a closing bracket, not words.
تحقق من العدد: السلسلة بعد padding فيها 7 tokens، ونموذج bigram ينتج عاملاً واحداً لكل bigram، وعدد bigrams في سلسلة من 7 tokens هو 7 - 2 + 1 = 6، فالعدد مطابق. ولاحظ أن <s> لا يظهر أبداً على يسار الشرطة العمودية ككلمة متوقعة، وأن </s> لا يظهر أبداً كـcontext. فهما قوس افتتاح وقوس إغلاق، وليسا كلمتين.
A trigram model needs TWO <s> tokens at the start.
نموذج trigram يحتاج إلى <s> مرتين في البداية.
bigram : <s> I want to eat </s> trigram : <s> <s> I want to eat </s>
Reason: the first factor of a trigram model is P(w1 | w-1, w0). It needs two preceding slots, so you must pad with N - 1 = 2 start symbols. A 4-gram needs three. The general rule is N - 1 copies of <s>, always exactly one </s>, because the end token is a single stopping event regardless of order.
السبب: أول عامل في نموذج trigram هو P(w1 | w-1, w0)، وهو يحتاج إلى خانتين سابقتين، فيجب أن تضيف N - 1 = 2 من رموز البداية، و4-gram يحتاج ثلاثة. والقاعدة العامة هي: N - 1 نسخة من <s>، ونسخة واحدة فقط دائماً من </s>، لأن end token هو حدث توقف واحد مهما كانت رتبة النموذج.
Students who pad with one <s> for a trigram either drop the first factor or silently compute a bigram probability for it. Both lose marks.
الطالب الذي يضيف <s> واحداً فقط لنموذج trigram إما يحذف العامل الأول أو يحسب له احتمال bigram دون أن ينتبه، وكلتا الحالتين تخسر درجات.
Unigram vs bigram vs trigram: what actually changes
Unigram مقابل bigram مقابل trigram: ما الذي يتغير فعلاً
Slide 15 is a three-column comparison. Here it is with the consequences filled in.
الشريحة 15 عبارة عن مقارنة من ثلاثة أعمدة، وهذه هي المقارنة نفسها مع إضافة النتائج المترتبة على كل حالة.
Unigram (N = 1)
Unigram (N = 1)
No context.
لا يوجد context إطلاقاً.
P(w)
Slide 15's phrase: "bag of words". Word order is invisible. Counts are dense (every word you have ever seen has a count), so estimation is easy and the model is bad.
التسمية في الشريحة 15: "bag of words". ترتيب الكلمات غير مرئي تماماً. الأعداد كثيفة (كل كلمة رأيتها لها عدّ)، فالتقدير سهل والنموذج سيئ.
Bigram (N = 2)
Bigram (N = 2)
1-word context.
context بطول كلمة واحدة.
P(wi | wi-1)
Slide 15's phrase: "local syntax". Catches things like to being followed by a verb. V2 possible bigrams, so counts already get sparse.
التسمية في الشريحة 15: "local syntax". يلتقط أموراً مثل أن to يتبعها فعل. عدد bigrams الممكنة هو V2، فالأعداد تبدأ بالتشتت من هنا.
Trigram (N = 3)
Trigram (N = 3)
2-word context.
context بطول كلمتين.
P(wi | wi-2, wi-1)
Slide 15's phrase: "more fluency". V3 possible trigrams. Generated text reads better, but most trigrams have count zero and you lean hard on smoothing.
التسمية في الشريحة 15: "more fluency". عدد trigrams الممكنة هو V3. النص المُولَّد يُقرأ بصورة أفضل، لكن معظم trigrams عدّها صفر، فتعتمد بشكل كبير على smoothing.
| Going from N to N + 1عند الانتقال من N إلى N + 1 | Effectالأثر |
|---|---|
| Context lengthطول السياق | Increases by 1 word, so more of the real history is usedيزيد كلمة واحدة، فيُستخدم جزء أكبر من التاريخ الحقيقي |
| Parameters to estimateالمعاملات المطلوب تقديرها | Multiplies by V, the vocabulary sizeتُضرب في V، أي حجم المفردات |
| Count per n-gramالعدّ لكل n-gram | Drops sharply, so estimates get noisierينخفض بحدة، فتصبح التقديرات أكثر ضوضاء |
| Zero countsالعدّات الصفرية | Explode, which is exactly why smoothing existsتنفجر، وهذا بالضبط سبب وجود smoothing |
| Fluency of generated textسلاسة النص المولَّد | Improves, up to the point where the model is just memorising training sentencesتتحسن، حتى النقطة التي يصبح فيها النموذج مجرد حافظ لجمل التدريب |
يلخص الجدول ما يحدث لكل شيء عند الانتقال من N إلى N + 1. المطلوب أن تلاحظ أن الصفوف تمشي في اتجاهين متعاكسين: السياق وطلاقة النص المُولَّد يتحسنان، لكن عدد parameters يُضرب في حجم vocabulary، ويهبط عدّ كل n-gram بشدة، وتنفجر الأصفار. هذه هي المقايضة بعينها، وهي سبب وجود smoothing.
Slide 13 compresses all of this into one line: "Larger n captures more context but increases sparsity." That sentence is the answer to any "what is the trade-off" question on this lecture.
تضغط الشريحة 13 كل هذا في سطر واحد: "Larger n captures more context but increases sparsity." هذه الجملة وحدها هي الجواب على أي سؤال عن trade-off في هذه المحاضرة.
"More context, less data." Every word you add to the window divides your counts by roughly the vocabulary size. Context and countability pull in opposite directions, and choosing N is choosing where to stand between them.
"More context, less data." كل كلمة تضيفها إلى النافذة تقسم أعدادك على ما يقارب حجم vocabulary. السياق والقابلية للعدّ يشدّان في اتجاهين متعاكسين، واختيار N هو في الحقيقة اختيار موقعك بينهما.
Check yourself
اختبر نفسك
Which of these is an exact identity rather than an approximation?
أي من هذه العبارات هي مطابقة دقيقة وليست تقريباً؟
B. That is the chain rule from slide 11, a consequence of the definition of conditional probability with nothing thrown away. A, C, and D all truncate the history and so all carry the Markov assumption (bigram, bigram sentence model, and unigram respectively). The giveaway is the approximation sign, but you should also be able to say why: only B conditions on the full history w1:k-1.
B. هذه هي chain rule من الشريحة 11، وهي نتيجة مباشرة لتعريف conditional probability ولا يُحذف فيها شيء. أما A وC وD فكلها تقتطع التاريخ، وبالتالي كلها تحمل Markov assumption (bigram، ثم نموذج الجملة بـbigram، ثم unigram، على التوالي). العلامة الفارقة هي إشارة التقريب، لكن يجب أن تكون قادراً على تفسير السبب أيضاً: B وحدها هي التي تشترط على التاريخ الكامل w1:k-1.
In the Markov approximation P(wn | wn-N+1:n-1), how many words of context does the model actually use?
في تقريب Markov المكتوب P(wn | wn-N+1:n-1)، كم كلمة من context يستخدم النموذج فعلياً؟
C. The window runs from index n - N + 1 to n - 1, which is (n-1) - (n-N+1) + 1 = N - 1 words. Answer A is the error printed in slide 12's own Key-idea prose: lowercase n is the position of the current word, so "n - 1 words" would mean the entire preceding history, which is the thing the assumption removes. Answer B counts the predicted word as context.
C. النافذة تمتد من الموضع n - N + 1 إلى n - 1، أي (n-1) - (n-N+1) + 1 = N - 1 كلمة. والخيار A هو نفس الخطأ المطبوع في نص Key-idea في الشريحة 12: فـn الصغير هو موضع الكلمة الحالية، فعبارة "n - 1 words" تعني التاريخ السابق بالكامل، وهو تحديداً الشيء الذي جاء هذا التقريب ليحذفه. أما الخيار B فيحسب الكلمة المتوقعة نفسها ضمن context.
How many <s> tokens should you prepend when scoring a sentence with a trigram model?
كم <s> يجب أن تضيف في بداية الجملة عند حساب احتمالها بنموذج trigram؟
D. A trigram factor is P(w | wi-2, wi-1), so the first real word needs two preceding slots. The rule is N - 1 start tokens, so a trigram gets two and a 4-gram gets three. Answer C is the off-by-one that treats N as the context length. You still use exactly one </s> no matter the order.
D. عامل trigram هو P(w | wi-2, wi-1)، فالكلمة الحقيقية الأولى تحتاج إلى خانتين سابقتين. والقاعدة هي N - 1 من رموز البداية، فـtrigram يأخذ اثنين و4-gram يأخذ ثلاثة. والخيار C هو خطأ off-by-one الذي يتعامل مع N كأنها طول context. وفي كل الأحوال تستخدم </s> واحداً فقط مهما كانت رتبة النموذج.
Slide 10 says direct counting of full histories fails. What is the more damaging of the two reasons it lists?
تقول الشريحة 10 إن العدّ المباشر للتواريخ الكاملة يفشل. فأي السببين المذكورين فيها هو الأشد ضرراً؟
A. Slide 10's bullets are "histories are too long" and "most exact histories never occur (language is creative)". The second is fatal rather than merely inconvenient: a zero denominator means no estimate exists. B describes an engineering cost you could in principle pay. This sparsity point is the thread running through the rest of the lecture into smoothing.
A. نقطتا الشريحة 10 هما "histories are too long" و"most exact histories never occur (language is creative)". والثانية قاتلة وليست مجرد إزعاج: المقام الصفري يعني أن التقدير غير موجود من الأساس. أما B فهو تكلفة هندسية يمكنك من حيث المبدأ أن تدفعها. ونقطة sparsity هذه هي الخيط الذي يمتد في بقية المحاضرة حتى يصل بك إلى smoothing.
How many bigram factors does <s> I want to eat </s> produce?
كم عاملاً من عوامل bigram تنتج الجملة <s> I want to eat </s>؟
B. The padded string has 6 tokens, so it has 6 - 2 + 1 = 5 bigrams: (<s>, I), (I, want), (want, to), (to, eat), (eat, </s>). Answer A forgets the P(</s> | eat) factor, which is the most common slip and the one that breaks the "proper distribution over sentence lengths" property from slide 14. Answer C counts tokens instead of bigrams.
B. السلسلة بعد الحشو فيها 6 tokens، فعدد الـ bigrams هو 6 - 2 + 1 = 5: (<s>, I) و(I, want) و(want, to) و(to, eat) و(eat, </s>). الخيار A ينسى العامل P(</s> | eat)، وهو الزلة الأكثر شيوعاً، وهي التي تكسر خاصية "التوزيع السليم على أطوال الجمل" (proper distribution over sentence lengths) من الشريحة 14. والخيار C يعدّ الـ tokens بدل الـ bigrams.