Lecture 03 · N-grams

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

Sampling from a Language Model

أخذ العينات من نموذج اللغة (Sampling)

Slides 41–52الشرائح 41–52 P3 SKIM

How to generate text from an n-gram model, and why you draw from the distribution instead of taking the most likely word.

كيف تولّد نصاً من نموذج n-gram، ولماذا تسحب من distribution بدلاً من أخذ الكلمة الأكثر احتمالاً.

Lower exam priorityأولوية أقل في الامتحان

This topic is conceptual, has no formulas to compute, and spans mostly picture slides. Skim it. Spend your time on perplexity and smoothing instead. If you only read one thing here, read the draw, not argmax section.

هذا الموضوع مفاهيمي، لا معادلات تُحسب فيه، ومعظم شرائحه صور. امسحه سريعاً واصرف وقتك على perplexity وsmoothing. وإذا لم تقرأ إلا شيئاً واحداً هنا، فاقرأ قسم draw, not argmax.

Why sample at all

لماذا نأخذ عينات أصلاً

Sampling is a diagnostic. It is how you see what the model has actually learned.

Sampling أداة تشخيص: بها ترى ما تعلّمه النموذج فعلياً.

Slide 41 gives three reasons:

تعطي الشريحة 41 ثلاثة أسباب:

You are not sampling to produce good text. You are sampling to inspect a distribution you cannot read directly. A bigram table has V² numbers in it. Nobody reads that. Generating ten sentences from it takes five seconds and tells you more.

أنت لا تستخدم sampling لإنتاج نص جيد، بل لتفحص distribution لا تستطيع قراءتها مباشرة. جدول bigram فيه V² من الأرقام، ولا أحد يقرأ ذلك. توليد عشر جمل منه يأخذ خمس ثوانٍ ويخبرك أكثر.

Sampling from a unigram model: the number line

أخذ العينات من نموذج unigram: خط الأعداد

Slide 42 gives the procedure. Slide 43 gives the picture.

الشريحة 42 تعطي الخطوات، والشريحة 43 تعطي الصورة.

  1. Lay every word of the vocabulary on the interval from 0 to 1. Each word gets a slice whose width equals its unigram probability.

    ضع كل كلمات vocabulary على الفترة من 0 إلى 1، وتأخذ كل كلمة شريحة عرضها يساوي احتمالها unigram.

  2. Draw a random number uniformly from 0 to 1.

    اسحب رقماً عشوائياً بتوزيع منتظم بين 0 و1.

  3. Print the word whose interval contains that number.

    اطبع الكلمة التي تقع الفترة الخاصة بها على ذلك الرقم.

  4. Repeat until you generate </s>.

    كرّر حتى تولّد </s>.

the of a to in 0.06 0.03 0.02 0.02 0.02 · · · however p=0.0003 polyphonic p=0.0000018 0 .06 .09 .11 .13 .15 .66 .99 1 random draw = 0.57 lands here
Slide 43. Each word owns a slice of [0, 1] proportional to its probability. The tick marks are cumulative probabilities. A uniform random draw lands in a frequent word's wide slice far more often than in a rare word's sliver.الشريحة 43. كل كلمة تملك شريحة من [0, 1] متناسبة مع احتمالها. العلامات على الخط هي الاحتمالات التراكمية. السحب العشوائي المنتظم يقع في الشريحة العريضة للكلمة الشائعة أكثر بكثير مما يقع في الشظية الرفيعة للكلمة النادرة.

يرسم الشكل خط الأعداد من 0 إلى 1. كل كلمة تملك شريحة عرضها يساوي احتمالها، فـ the تأخذ 0.06 وof تأخذ 0.03، أما polyphonic فشريحتها شُعيرة بالكاد تُرى. اقرأ الأرقام تحت الخط (.06, .09, .11, .13, .15) كاحتمالات تراكمية، أي حدود الشرائح لا عرضها. السحب العشوائي 0.57 يقع في مكانه على الخط فتُطبع الكلمة المالكة لذلك الموضع.

The key property: the width of the slice is the probability. You do not need any extra machinery. A uniform draw over [0, 1] automatically picks words in proportion to how likely the model thinks they are.

الخاصية الأساسية: عرض الشريحة هو الاحتمال نفسه، فلا تحتاج أي آلية إضافية. السحب المنتظم على [0, 1] يختار الكلمات تلقائياً بنسبة ما يرجّحه النموذج لها.

Memory hookخطّاف للذاكرة

Wide slice, more hits. the owns 6 percent of the line, polyphonic owns 0.00018 percent. The random number does not care about ranking, only about width.

شريحة أوسع، إصابات أكثر. كلمة the تملك ستة بالمئة من الخط، وpolyphonic تملك 0.00018 بالمئة. الرقم العشوائي لا يهمه الترتيب، بل العرض فقط.

Extending to bigrams and higher

التوسع إلى الـ bigrams وما فوقها

Slide 44. Same number line, different distribution at each step.

الشريحة 44: خط الأعداد نفسه، لكن distribution مختلفة في كل خطوة.

  1. Start with <s>. Sample the first word from P(w | <s>).

    ابدأ بـ <s>، واسحب الكلمة الأولى من P(w | <s>).

  2. Say you got w1. Now sample w2 from P(w2 | w1).

    لنفترض أنك حصلت على w1، فاسحب الآن w2 من P(w2 | w1).

  3. Keep going: each new word is sampled from the distribution conditioned on the previous n−1 words.

    واصل هكذا: كل كلمة جديدة تُسحب من distribution مشروطة بالكلمات n−1 السابقة.

  4. Stop when you sample </s>.

    توقّف عندما تسحب </s>.

For a bigram you condition on one previous word. For a trigram you condition on two. The procedure is identical, only the size of the context window changes.

في bigram تشترط على كلمة سابقة واحدة، وفي trigram على كلمتين. الإجراء نفسه تماماً، والذي يتغير هو حجم نافذة السياق فقط.

The walkthrough on slides 45–52

الشرح التفصيلي في الشرائح 45–52

The deck labels these "Bigram example", but the figure actually slides a two-word context window, which is a trigram-sized context. Read it as the general n-gram sampling loop. The important part is the loop, not the value of n.

تسمّي الشرائح هذا "Bigram example"، لكن الشكل في الحقيقة يحرّك نافذة سياق من كلمتين، وهذا حجم سياق trigram. اقرأها كحلقة sampling عامة لأي n-gram. المهم هو الحلقة نفسها، لا قيمة n.

SlideالشريحةText so farالنص حتى الآنCondition onالشرط علىDistribution (top of list)التوزيع (أعلى القائمة)Sampledالعينة المسحوبة
45deep ___deepn/an/a
46deep ___deeplearning 0.23, reinforcement 0.05, convolutional 0.04, neural 0.04learning
47deep learning ___deep learningn/an/a
48deep learning ___deep learningmodels 0.06, . 0.05, ; 0.05, based 0.05, , 0.05, for 0.04,
49deep learning , ___n/an/an/a
50deep learning , ___learning , (deep discarded)learning , (حُذفت deep)n/an/a
51deep learning , ___learning ,and 0.14, we 0.07, which 0.05, the 0.05, where 0.03, a 0.02, but 0.02but
52deep learning , but ___n/an/an/a

يتتبع الجدول الشرائح 45 إلى 52 خطوة بخطوة. اقرأ كل صف كلقطة واحدة: النص المتولَّد حتى الآن، ثم السياق المشروط عليه، ثم أعلى الاحتمالات، ثم الكلمة التي سُحبت فعلاً. المسار هو deep ثم learning ثم , ثم but. لاحظ صفين: الشريحة 50 حيث خرجت deep من النافذة، والشريحتان 48 و51 حيث الكلمة المسحوبة ليست الأعلى احتمالاً.

Three things to notice in that table:

ثلاثة أمور تلاحظها في هذا الجدول:

Draw, do not argmax

اسحب عينة، ولا تستخدم argmax

This is the one thing on this page that can appear as an exam question.

هذا هو الشيء الوحيد في هذه الصفحة الذي قد يأتي كسؤال امتحان.

sample: w ~ P(w | context) draw proportional to probability argmax: w = arg max P(w | context) always the same word

يقابل الصندوق العمليتين. السطر الأول sample: الرمز ~ يعني "مسحوبة من"، فالكلمة تُختار باحتمال يتناسب مع P(w | context). السطر الثاني argmax: الكلمة هي صاحبة أعلى احتمال، فالناتج واحد لا يتغير مع السياق نفسه.

Sampling means drawing a random point according to likelihood. Argmax means always taking the top of the list. They are different operations with very different outputs.

Sampling يعني سحب نقطة عشوائية بحسب الترجيح، وargmax يعني أخذ أعلى القائمة دائماً. عمليتان مختلفتان ومخرجاتهما مختلفة جداً.

Why argmax breaks

لماذا ينهار argmax

Argmax is deterministic. Given the same context, it always returns the same word. In a bigram model, that means once you revisit a context you have already seen, you are locked into a cycle forever.

Argmax حتمي (deterministic): مع السياق نفسه يُعيد الكلمة نفسها دائماً. في نموذج bigram هذا يعني أنك بمجرد أن تعود إلى سياق رأيته سابقاً تُحبس في حلقة لا تنتهي.

Worked: argmax collapses into a loopمثال محلول: argmax ينهار إلى حلقة مكررة

Suppose a bigram model where arg max P(w | the) = man and arg max P(w | man) = the.

افترض نموذج bigram فيه arg max P(w | the) = man وarg max P(w | man) = the.

context = the   -> argmax -> man
context = man   -> argmax -> the
context = the   -> argmax -> man      same context as step 1
context = man   -> argmax -> the
...
output: the man the man the man the man ...

يُظهر التتبع الحلقة: السياق the يعطي man، والسياق man يعطي the، ثم يعود السياق إلى the نفسه كما في الخطوة الأولى. لأن argmax حتمي، فتكرار السياق يعني تكرار كل ما بعده، والناتج النهائي the man the man ... إلى ما لا نهاية.

The generator never reaches </s>, so it never terminates on its own. Now do the same thing with sampling: at the you draw, so sometimes you get man, sometimes other, sometimes </s>. The cycle is broken by the randomness, and the output stays varied.

المولّد لا يصل إلى </s> أبداً، فلا يتوقف من نفسه. الآن أعد الشيء نفسه مع sampling: عند the تسحب، فتحصل مرة على man ومرة على other ومرة على </s>. العشوائية تكسر الحلقة، ويبقى الناتج متنوعاً.

Explicit comparison

مقارنة صريحة

PropertyالخاصيةSampling (draw)أخذ العينات (سحب)Argmax (greedy)Argmax (جشع)
RuleالقاعدةPick w with probability P(w | context)اختر w باحتمال P(w | context)Pick the single most likely wاختر w الأكثر احتمالاً وحدها
Run it twiceشغّله مرتينDifferent output each timeمخرجات مختلفة في كل مرةIdentical output each timeمخرجات متطابقة في كل مرة
Rare wordsالكلمات النادرةAppear occasionally, at the right rateتظهر أحياناً، بالمعدل الصحيحNever appear at allلا تظهر أبداً
Typical failureالإخفاق المعتادOccasional incoherent wordكلمة غير متسقة أحياناًDegenerate repetitive loopحلقة تكرارية متدهورة
Reflects the model's distribution?هل يعكس توزيع النموذج؟Yes, that is the pointنعم، وهذا هو المقصودNo, only its modeلا، يعكس منواله (mode) فقط
Useful as a diagnostic?هل هو مفيد كأداة تشخيص؟YesنعمNoلا

يقارن الجدول sampling بـ argmax على ستة محاور. اقرأ العمود الأوسط كسلوك السحب والأيمن كسلوك الجشع (greedy). الخلاصة: السحب يتغير ناتجه في كل تشغيل ويُظهر الكلمات النادرة بمعدلها الصحيح ويعكس distribution كلها، أما argmax فناتجه ثابت ولا يُظهر النادر أبداً ولا يعكس إلا قمة التوزيع (mode)، ولذلك لا يصلح أداة تشخيص.

Trapفخ

"Sampling generates the most likely sentence" is false. Sampling generates sentences in proportion to their probability. The single most likely sentence under almost any LM is something short and dull, and sampling will rarely produce it, because all the other sentences together hold most of the mass. Also false: "sampling needs a separate rule to stop." It does not. </s> is just another word on the number line, and you stop when you draw it.

عبارة "Sampling generates the most likely sentence" خاطئة. Sampling يولّد الجمل بنسبة احتمالاتها. والجملة الأعلى احتمالاً في أي LM تقريباً هي شيء قصير وممل، ونادراً ما يولّدها السحب لأن بقية الجمل مجتمعة تحمل معظم الكتلة الاحتمالية. وخاطئة أيضاً: "sampling needs a separate rule to stop". لا يحتاج، فـ </s> كلمة كأي كلمة على خط الأعداد، وتتوقف عندما تسحبها.

Memory hookخطّاف للذاكرة

Roll the die, do not read the leaderboard. Slides 48 and 51 both sampled a word that was not at the top of the list. If the slides had used argmax, they would have printed models and and every single time.

ارمِ حجر النرد، لا تقرأ لوحة الصدارة. الشريحتان 48 و51 سحبتا كلمة ليست في أعلى القائمة. ولو استخدمتا argmax لطبعتا models وand في كل مرة بلا استثناء.

What sampling reveals

ما الذي يكشفه أخذ العينات

Because the output mirrors the training distribution, the generated text is a readout of what the model memorized. That is exactly the setup for the next page: sample from a unigram, a bigram, a trigram and a 4-gram on the same corpus, and the quality shift tells you how the model is trading coherence against memorization.

لأن الناتج يعكس توزيع بيانات التدريب، فالنص المتولَّد قراءة مباشرة لما حفظه النموذج. وهذا تماماً مدخل الصفحة التالية: اسحب من unigram وbigram وtrigram و4-gram على corpus واحد، وفرق الجودة بينها يخبرك كيف يوازن النموذج بين التماسك والحفظ.

Check yourself

اختبر نفسك

In the unigram number-line picture, what does the width of a word's interval represent?

في صورة خط الأعداد لنموذج unigram، ماذا يمثّل عرض الفترة الخاصة بالكلمة؟

C. Each word covers an interval proportional to its probability, so a uniform draw selects words at exactly the right rate. D is the tempting wrong one: the cumulative probability is what the tick marks on the line show (.06, .09, .11, ...), not the width of a slice. The width is the individual probability; the tick is the running total.

الجواب C. كل كلمة تغطي فترة تتناسب مع احتمالها، فالسحب المنتظم يختارها بالمعدل الصحيح تماماً. والجواب D هو المُغري الخاطئ: الاحتمال التراكمي هو ما تُظهره علامات التأشير على الخط (.06, .09, .11, ...)، لا عرض الشريحة. العرض هو الاحتمال المفرد، والعلامة هي المجموع الجاري.

On slide 48 the distribution after "deep learning" was models 0.06, . 0.05, ; 0.05, based 0.05, "," 0.05, for 0.04. The slide samples ",". What does that demonstrate?

في الشريحة 48 كان التوزيع بعد "deep learning" هو models 0.06، . 0.05، ; 0.05، based 0.05، "," 0.05، for 0.04، وسحبت الشريحة ",". على ماذا يدل هذا؟

B. "," had probability 0.05 against the top word's 0.06, so it was very nearly as likely to be drawn. A is the trap: nothing is broken. If the slide were taking argmax it would print models every time, which is precisely the behavior sampling is designed to avoid.

الجواب B. احتمال "," كان 0.05 مقابل 0.06 للكلمة الأعلى، أي قريب منها جداً في فرصة السحب. والجواب A هو الفخ: لا شيء معطوب. لو كانت الشريحة تأخذ argmax لطبعت models في كل مرة، وهذا بالضبط السلوك الذي صُمم sampling لتجنبه.

What happens if you generate from a bigram model by always picking the most probable next word?

ماذا يحدث إذا ولّدت من نموذج bigram بأخذ الكلمة التالية الأعلى احتمالاً دائماً؟

D. Argmax is deterministic, so the moment a context repeats, the whole continuation repeats, and you are stuck in a cycle that may never reach </s>. A is wrong because greedy word-by-word choice does not maximize the product over the whole sentence, it only maximizes each local step.

الجواب D. Argmax حتمي، فبمجرد أن يتكرر السياق تتكرر كل التكملة، وتنحبس في حلقة قد لا تصل إلى </s> أبداً. والجواب A خاطئ لأن الاختيار الجشع كلمة بكلمة لا يعظّم حاصل الضرب على الجملة كاملة، بل يعظّم كل خطوة محلية فقط.

How does the sampling loop know when to stop generating?

كيف تعرف حلقة sampling متى تتوقف عن التوليد؟

A. </s> sits on the number line like any other token and owns a slice equal to its probability. You stop when the draw lands in that slice. No external length limit and no probability threshold is part of the algorithm.

الجواب A. الرمز </s> يجلس على خط الأعداد كأي token آخر ويملك شريحة بحجم احتماله، وتتوقف عندما يقع السحب في تلك الشريحة. لا حدّ طول خارجي ولا عتبة احتمال جزء من الخوارزمية.

Slide 50 crosses out "deep" and conditions only on "learning ,". Why?

تشطب الشريحة 50 كلمة "deep" وتشترط على "learning ," فقط. لماذا؟

C. An n-gram model conditions on a fixed window of the previous n−1 words. Once the window has filled, each new word pushes the oldest one out. B is the tempting wrong one: nothing is discarded because of its probability, the window just slid. D is also wrong, the comma is an ordinary token in the context.

الجواب C. نموذج n-gram يشترط على نافذة ثابتة من الكلمات n−1 السابقة. وبعد أن تمتلئ النافذة، كل كلمة جديدة تدفع الأقدم خارجها. والجواب B هو المُغري الخاطئ: لا شيء يُحذف بسبب احتماله، النافذة انزلقت فقط. والجواب D خاطئ أيضاً، فالفاصلة token عادي داخل السياق.