ICS 582Lecture 06المحاضرة 06Part 03الجزء 03
RNN language models and autoregressive generationنماذج اللغة RNN والتوليد الانحداري الذاتي
Why neural LMs beat counted n-grams on sparsity, how an RNN LM computes a next-word distribution at every step, how it generates text by sampling, and what multi-layer RNN LMs look like.لماذا تتفوق نماذج اللغة العصبية على الـ n-grams المعدودة في مشكلة التفرق، وكيف يحسب نموذج RNN توزيع الكلمة التالية في كل خطوة، وكيف يولد النص بالعينات، وكيف تبدو نماذج RNN متعددة الطبقات.
- Conceptsالمفاهيم
- 6
- Slidesالشرائح
- 12-18
- Readingمدة القراءة
- 36 min36 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
In lecture 03 you built language models by counting. A bigram model looked at one previous word, a trigram model at two, and every word was an index into a table. This part replaces the table with a network. An RNN language model reads a sentence one word at a time, keeps a running summary of everything it has read, and turns that summary into a probability for every word in the vocabulary.
The concepts build on each other. First, why moving from counts to vectors fixes sparsity. Second, the four equations of the RNN LM with every matrix shape. Third, how the same model writes text by feeding its own words back in. Fourth, the second notation on slide 16, which reuses the letter U for a different matrix. Fifth, what recurrence buys and what it costs, and last, how layers stack. For the exam, expect to write the forward equations with shapes, list advantages and problems, and explain why the model size does not grow with input length.
في المحاضرة 03 بنيت نماذج لغة بالعدّ. نموذج bigram كان ينظر إلى كلمة سابقة واحدة، ونموذج trigram إلى كلمتين، وكانت كل كلمة مجرد رقم فهرس في جدول. هذا الجزء يستبدل الجدول بشبكة عصبية. نموذج اللغة RNN يقرأ الجملة كلمة كلمة، ويحتفظ بملخص متجدد لكل ما قرأه حتى الآن، ثم يحوّل هذا الملخص إلى احتمال لكل كلمة في المفردات (vocabulary).
المفاهيم مبنية بعضها على بعض. أولًا، لماذا ينحل مشكل التناثر (sparsity) عند الانتقال من العدّ إلى المتجهات. ثانيًا، المعادلات الأربع لنموذج RNN LM مع أبعاد كل مصفوفة. ثالثًا، كيف يكتب النموذج نفسه نصًا بأن يعيد إدخال كلماته هو. رابعًا، الترميز الثاني في الشريحة 16، الذي يستخدم الحرف U لمصفوفة مختلفة. خامسًا، ماذا يكسب التكرار (recurrence) وماذا يكلّف، وأخيرًا كيف تُكدَّس الطبقات. في الامتحان توقّع أن يُطلب منك كتابة معادلات التمرير الأمامي مع الأبعاد، وذكر المزايا والمشكلات، وشرح لماذا لا يزداد حجم النموذج بزيادة طول المدخل.
By the end you canفي نهاية هذا الجزء ستستطيع
- Contrast a count-based n-gram LM with a neural LM: the Markov assumption and counting versus learned word vectors that share statistical strength.تقارن بين نموذج لغة n-gram قائم على العدّ ونموذج لغة عصبي: افتراض ماركوف والعدّ في مقابل متجهات كلمات متعلَّمة تتشارك القوة الإحصائية بين الكلمات المتشابهة.
- Write the four RNN LM equations (embedding, recurrence, softmax output, cross-entropy loss) and give the shape of every vector and matrix.تكتب معادلات RNN LM الأربع (التضمين embedding، والتكرار recurrence، ومخرج softmax، وخسارة cross-entropy) وتعطي أبعاد كل متجه وكل مصفوفة.
- Count the parameters of an RNN LM and explain why the count does not grow with input length.تحسب عدد معاملات RNN LM وتشرح لماذا لا يزداد هذا العدد بزيادة طول المدخل.
- Describe autoregressive generation from <s> to </s>, and contrast greedy decoding, sampling and temperature.تصف التوليد الانحداري الذاتي (autoregressive generation) من <s> إلى </s>، وتقارن بين فك الترميز الجشع (greedy decoding) والاختيار العشوائي (sampling) ودرجة الحرارة (temperature).
- Translate between the SLP3 notation (E, W, U, V) and the CS224n notation (E, W_e, W_h, U) used on the slides.تترجم بين ترميز SLP3 (أي E وW وU وV) وترميز CS224n (أي E وW_e وW_h وU) المستخدمَين في الشرائح.
- List two advantages and two remaining problems of RNN LMs, and sketch a two-layer RNN LM.تذكر ميزتين ومشكلتين باقيتين في نماذج RNN LM، وترسم مخططًا لنموذج RNN LM من طبقتين.
Suppose your training corpus contains "the cat sat on the mat" many times but never "the dog sat on the rug". A trigram model asked for after a dog sentence has nothing to say about dogs or rugs specifically. To the count table, cat and dog are just two unrelated column numbers. Every fact it knows about cats is useless for dogs.
افترض أن مدوّنة التدريب (corpus) تحتوي الجملة "the cat sat on the mat" مرات كثيرة، لكنها لا تحتوي أبدًا "the dog sat on the rug". نموذج trigram يُسأل عن بعد جملة عن الكلب لا يملك ما يقوله عن الكلاب أو السجاد تحديدًا. بالنسبة لجدول العدّ، cat وdog مجرد رقمي عمودين لا علاقة بينهما. وكل معلومة يعرفها عن القطط عديمة الفائدة للكلاب.
Count-based models: Markov assumption plus countingالنماذج القائمة على العدّ: افتراض ماركوف مع العدّ
A Count-based (n-gram) language model makes two moves. It assumes the next word depends only on the previous n-1 words (an n-th order Markov assumption in the slide's phrasing), and it estimates each conditional probability as a ratio of counts, then smooths so that unseen events do not get probability zero. Both moves are where the trouble lives. The window throws away anything older than n-1 words, and the counts treat each word as an atomic symbol, so the model cannot generalize from one word to a similar one. Making n larger to see more context makes the problem worse, because the number of possible n-grams grows as |V|^n: with |V| = 10,000, there are already 10^12 possible trigrams, almost all unseen.
يقوم count-based LM بخطوتين. يفترض أن الكلمة التالية تعتمد فقط على آخر n-1 كلمة (افتراض ماركوف من الرتبة n بحسب صياغة الشريحة)، ويقدّر كل احتمال شرطي بوصفه نسبة بين عددين، ثم يطبّق التنعيم (smoothing) كي لا تحصل الأحداث غير المرئية على احتمال صفر. والمشكلة تكمن في هاتين الخطوتين بالذات. النافذة تتخلص من كل ما هو أقدم من n-1 كلمة، والعدّ يعامل كل كلمة رمزًا ذريًا مستقلًا، فلا يستطيع النموذج التعميم من كلمة إلى كلمة مشابهة لها. وتكبير n لرؤية سياق أوسع يزيد المشكلة سوءًا، لأن عدد الـ n-grams الممكنة ينمو بمقدار |V|^n: فمع |V| = 10,000 يوجد بالفعل 10^12 trigram ممكن، وكلها تقريبًا لم تُرَ قط.
Neural models: words as vectors, parameters learnedالنماذج العصبية: الكلمات متجهات، والمعاملات متعلَّمة
A Neural language model attacks sparsity by representing each word as a dense vector. Training pushes words that appear in similar contexts toward similar vectors, so cat and dog end up close, and mat and rug end up close. Now the evidence from "the cat sat on the mat" also raises the probability of "the dog sat on the rug", because the network's computation is smooth in its inputs: nearby input vectors give nearby outputs. Bengio and colleagues (2003) put it this way: a new sentence can receive high probability if its words are close in representation space to words in sentences already seen. The second bullet of slide 13 matters too: nothing is counted. The vectors and all other weights are parameters learned by gradient descent as part of training.
يعالج neural language model مشكلة التناثر بتمثيل كل كلمة متجهًا كثيفًا (dense vector). التدريب يدفع الكلمات التي تظهر في سياقات متشابهة نحو متجهات متشابهة، فينتهي الأمر بـ cat وdog متقاربتين، وبـ mat وrug متقاربتين. والآن فإن الدليل الآتي من "the cat sat on the mat" يرفع أيضًا احتمال "the dog sat on the rug"، لأن حسابات الشبكة سلسة بالنسبة لمدخلاتها: المتجهات المتقاربة في المدخل تعطي مخرجات متقاربة. وقد عبّر عن ذلك Bengio وزملاؤه (2003) هكذا: الجملة الجديدة يمكن أن تنال احتمالًا عاليًا إذا كانت كلماتها قريبة في فضاء التمثيل من كلمات في جمل سبق رؤيتها. والنقطة الثانية في الشريحة 13 مهمة كذلك: لا شيء يُعدّ هنا. المتجهات وكل الأوزان الأخرى معاملات تُتعلَّم بالانحدار التدرجي (gradient descent) جزءًا من التدريب.
| Count-based (statistical)القائمة على العدّ (الإحصائية) | Neuralالعصبية | |
|---|---|---|
| What is assumedما الذي يُفترض | An (n-1)-th order Markov assumption: only the last n-1 words matterافتراض ماركوف من الرتبة (n-1): آخر n-1 كلمة فقط هي المهمة | No fixed window; an RNN summarizes the whole prefix in a hidden stateلا نافذة ثابتة؛ تلخّص RNN البادئة كاملة في حالة مخفية |
| How probabilities are obtainedكيف نحصل على الاحتمالات | Counting n-grams in a corpus, then smoothingبعدّ الـ n-grams في مدوّنة ثم التنعيم (smoothing) | Computed by a network whose parameters are learned by trainingتحسبها شبكة تُتعلَّم معاملاتها بالتدريب |
| How a word is representedكيف تُمثَّل الكلمة | An atomic symbol, a row or column index in a count tableرمز ذري، أي رقم صف أو عمود في جدول العدّ | A dense learned vector, so similar words sit close togetherمتجه كثيف متعلَّم، فتقع الكلمات المتشابهة قريبة بعضها من بعض |
| What happens to an unseen n-gramماذا يحدث لـ n-gram لم يُرَ | Zero count; it only gets mass that smoothing steals from seen eventsعدده صفر؛ لا ينال إلا الكتلة الاحتمالية التي يقتطعها التنعيم من الأحداث المرئية | Gets a sensible probability if its words resemble words seen in trainingينال احتمالًا معقولًا إذا كانت كلماته تشبه كلمات رُئيت في التدريب |
| Size as context growsالحجم مع اتساع السياق | Possible n-grams grow as |V|^nعدد الـ n-grams الممكنة ينمو بمقدار |V|^n | Fixed: the same matrices are reused at every stepثابت: المصفوفات نفسها يُعاد استخدامها في كل خطوة |
Lecture 04 learned embeddings with a separate method such as word2vec. Here the embedding matrix is part of the language model itself and is learned together with everything else. And because an RNN carries a hidden state forward, it also drops the fixed window: it conditions on the whole prefix, not just the last n-1 words.
في المحاضرة 04 تعلّمنا التضمينات (embeddings) بطريقة منفصلة مثل word2vec. أما هنا فمصفوفة التضمين جزء من نموذج اللغة نفسه وتُتعلَّم مع كل شيء آخر. ولأن RNN تحمل حالة مخفية إلى الأمام، فإنها تتخلص أيضًا من النافذة الثابتة: تشترط على البادئة كاملة (prefix)، لا على آخر n-1 كلمة فقط.
Recall
Name the two things a count-based LM does, and the two things slide 13 says a neural LM does instead.اذكر الأمرين اللذين يقوم بهما نموذج اللغة القائم على العدّ، والأمرين اللذين تقول الشريحة 13 إن النموذج العصبي يقوم بهما بدلًا منهما.
Count-based: it makes an n-th order Markov assumption and estimates n-gram probabilities by counting and smoothing. Neural: it solves data sparsity by representing words as vectors, and its parameters are learned as part of training.
القائم على العدّ: يفترض افتراض ماركوف من الرتبة n، ويقدّر احتمالات n-gram بالعدّ والتنعيم. العصبي: يحل مشكلة تناثر البيانات بتمثيل الكلمات متجهات، ومعاملاته تُتعلَّم جزءًا من التدريب.
Take the prefix "So long and" from the slide's figure. The model must output a probability for every vocabulary word as the next word, and it should put a lot of mass on "thanks". Here is the exact path a word takes from input to that distribution, in the notation of Jurafsky and Martin (SLP3), which slide 14 uses.
خذ البادئة "So long and" من رسم الشريحة. يجب أن يُخرج النموذج احتمالًا لكل كلمة في المفردات بوصفها الكلمة التالية، وينبغي أن يضع كتلة احتمالية كبيرة على "thanks". وهذا هو المسار الدقيق الذي تسلكه الكلمة من المدخل إلى ذلك التوزيع، بترميز Jurafsky وMartin (SLP3) الذي تستخدمه الشريحة 14.
The four equationsالمعادلات الأربع
- Embed. The current word arrives as a one-hot vector . Multiplying by the Embedding matrix E just selects one column of , the word's vector .التضمين. تصل الكلمة الحالية متجهًا أحادي الساخن (one-hot) . والضرب في embedding matrix يختار ببساطة عمودًا واحدًا من ، وهو متجه الكلمة .
- Recur. The new Hidden state mixes the old state, weighted by , with the new word, weighted by , and passes the sum through a nonlinearity such as tanh. This is the Recurrent neural network (RNN) cell of part 1; nothing new except that the input is a word embedding.التكرار. الـ hidden state الجديدة تمزج الحالة القديمة، موزونة بـ ، مع الكلمة الجديدة، موزونة بـ ، ثم تمرّر المجموع عبر دالة غير خطية مثل tanh. هذه هي خلية RNN من الجزء 1؛ لا جديد فيها سوى أن المدخل تضمين كلمة.
- Score and normalize. gives one score per vocabulary word, and Softmax turns the scores into a probability distribution . Its entry is .حساب الدرجات والتطبيع. يعطي درجة (score) واحدة لكل كلمة في المفردات، وتحوّل softmax هذه الدرجات إلى توزيع احتمالي . والمدخل منه يساوي .
- Pay for the mistake. In training, the Cross-entropy loss at step t looks up the probability the model gave to the true next word and takes minus its log. Part 4 derives why the full cross-entropy collapses to this one term.دفع ثمن الخطأ. أثناء التدريب، تبحث cross-entropy loss عند الخطوة t عن الاحتمال الذي أعطاه النموذج للكلمة التالية الصحيحة وتأخذ سالب لوغاريتمه. الجزء 4 يشتق لماذا تنهار الـ cross-entropy الكاملة إلى هذا الحد الوحيد.
Every shape, and where the parameters liveكل الأبعاد، وأين تقع المعاملات
Shapes are the fastest way to check you remember the equations correctly, because a wrong matrix cannot be multiplied. Write |V| for the vocabulary size, d_e for the embedding size and d_h for the hidden size. SLP3 sets d_e = d_h = d for simplicity, which makes and both d × d.
الأبعاد أسرع طريقة للتأكد من أنك تتذكر المعادلات بشكل صحيح، لأن المصفوفة الخاطئة لا يمكن ضربها. اكتب |V| لحجم المفردات، وd_e لحجم التضمين، وd_h لحجم الحالة المخفية. يضع SLP3 القيمة d_e = d_h = d للتبسيط، مما يجعل و كلتيهما بحجم d × d.
Shapes in the RNN LM (column vectors)الأبعاد في RNN LM (متجهات عمودية)
- x_t (one-hot input word)x_t (كلمة الإدخال بصيغة one-hot)
- |V| × 1|V| × 1
- E (embedding matrix)E (مصفوفة التضمين)
- d_e × |V|d_e × |V|
- e_t = E x_t
- d_e × 1d_e × 1
- W (input to hidden)W (من الإدخال إلى المخفية)
- d_h × d_ed_h × d_e
- U (hidden to hidden)U (من المخفية إلى المخفية)
- d_h × d_hd_h × d_h
- h_t (hidden state)h_t (الحالة المخفية)
- d_h × 1d_h × 1
- V (hidden to output)V (من المخفية إلى الإخراج)
- |V| × d_h|V| × d_h
- V h_t and ŷ_tV h_t وŷ_t
- |V| × 1|V| × 1
Worked exampleمثال محلول
Parameter count for |V| = 10,000 and d_e = d_h = 512عدد المعاملات عندما |V| = 10,000 وd_e = d_h = 512
Embedding matrix Eمصفوفة التضمين E
512 × 10,000 = 5,120,000 weights.512 × 10,000 = 5,120,000 وزن.Input matrix W and recurrent matrix Uمصفوفة الإدخال W والمصفوفة التكرارية U
Each is 512 × 512 = 262,144, so 524,288 together.كل منهما بحجم 512 × 512 = 262,144، فمجموعهما 524,288.Output matrix Vمصفوفة الإخراج V
10,000 × 512 = 5,120,000 weights. This one matrix is as large as the whole embedding table.10,000 × 512 = 5,120,000 وزن. هذه المصفوفة وحدها بحجم جدول التضمين كله.Resultالنتيجة
About 10.76 M weights in total (10,764,288, ignoring biases), and over 95% of them sit in E and V, the two matrices that touch the vocabulary. With Weight tying, SLP3 reuses in place of , which removes 5.12 M weights.نحو 10.76 M وزن إجمالًا (10,764,288، مع إهمال الانحيازات biases)، وأكثر من 95% منها يقع في E وV، المصفوفتين اللتين تتعاملان مع المفردات. ومع weight tying يعيد SLP3 استخدام مكان ، مما يحذف 5.12 M وزن.
Worked exampleمثال محلول
One softmax step and its lossخطوة softmax واحدة وخسارتها
Scoresالدرجات
Shrink the vocabulary to three words, thanks, for and all, and suppose V h_t = [2.0, 1.0, 0.1] gives them scores 2.0, 1.0 and 0.1.صغّر المفردات إلى ثلاث كلمات: thanks وfor وall، وافترض أن V h_t = [2.0, 1.0, 0.1] يعطيها الدرجات 2.0 و1.0 و0.1.Exponentiate and normalizeالرفع الأسي ثم التطبيع
e^2.0 = 7.389, e^1.0 = 2.718, e^0.1 = 1.105, sum 11.212. So ŷ_t = [0.659, 0.242, 0.099].e^2.0 = 7.389 وe^1.0 = 2.718 وe^0.1 = 1.105، والمجموع 11.212. إذن ŷ_t = [0.659, 0.242, 0.099].Lossالخسارة
The true next word is thanks, so L_CE = -ln 0.659 = 0.417. Had the true word been all, the loss would be -ln 0.099 = 2.31: confident mistakes cost more.الكلمة التالية الصحيحة هي thanks، إذن L_CE = -ln 0.659 = 0.417. ولو كانت الكلمة الصحيحة all لكانت الخسارة -ln 0.099 = 2.31: الأخطاء الواثقة تكلّف أكثر.Resultالنتيجة
Training lowers the loss by raising the true word's probability at every position.التدريب يخفّض الخسارة برفع احتمال الكلمة الصحيحة عند كل موضع.
Quick checkاختبار سريع
With |V| = 10,000 and d_h = 512, what is the shape of the output matrix V in SLP3 notation?عندما |V| = 10,000 وd_h = 512، ما أبعاد مصفوفة الإخراج V بترميز SLP3؟
Recall
Write the four RNN LM equations from memory and say what ŷ_t[k] means.اكتب معادلات RNN LM الأربع من الذاكرة، ووضّح ماذا يعني ŷ_t[k].
e_t = E x_t; h_t = g(U h_{t-1} + W e_t); ŷ_t = softmax(V h_t); L_CE = -log ŷ_t[w_{t+1}]. The entry ŷ_t[k] is the probability that the next word is vocabulary item k given all words up to t.
e_t = E x_t؛ h_t = g(U h_{t-1} + W e_t)؛ ŷ_t = softmax(V h_t)؛ L_CE = -log ŷ_t[w_{t+1}]. والمدخل ŷ_t[k] هو احتمال أن تكون الكلمة التالية هي عنصر المفردات k بمعلومية كل الكلمات حتى t.
A trained RNN LM can write. Give it only the start marker <s>. It outputs a distribution over the first word; you pick "So". Now "So" becomes the next input, the model updates its hidden state and outputs a distribution for the second word; you pick "long", and so on. The model is reading its own writing.
نموذج RNN LM المدرَّب يستطيع الكتابة. أعطه علامة البداية <s> فقط. سيُخرج توزيعًا على الكلمة الأولى، فتختار "So". الآن تصبح "So" المدخل التالي، فيحدّث النموذج حالته المخفية ويُخرج توزيعًا للكلمة الثانية، فتختار "long"، وهكذا. النموذج يقرأ ما يكتبه بنفسه.
The procedureالإجراء
- Feed <s> as the first input and compute .أدخل <s> مدخلًا أول واحسب .
- Choose a word from that softmax distribution.اختر كلمة من توزيع softmax هذا.
- Look up the chosen word's embedding and use it as the input at the next step, carrying the hidden state forward.ابحث عن تضمين الكلمة المختارة واستخدمه مدخلًا في الخطوة التالية، مع حمل الحالة المخفية إلى الأمام.
- Repeat until the model produces </s> or a maximum length is reached.كرّر حتى يُنتج النموذج </s> أو يُبلَغ الحد الأقصى للطول.
This is Autoregressive generation: each output is conditioned on outputs the model itself chose earlier. SLP3 notes the name is used loosely, since a true autoregressive model in statistics is linear, while an LM is not. The procedure is exactly the Shannon-style sampling of lecture 03, with one change that matters: an n-gram sampler forgets everything older than n-1 words, while the RNN carries the whole prefix in .
هذا هو autoregressive generation: كل مخرج مشروط بمخرجات اختارها النموذج نفسه سابقًا. يلاحظ SLP3 أن الاسم مستخدم بتساهل، لأن النموذج الانحداري الذاتي الحقيقي في الإحصاء خطي، بينما نموذج اللغة ليس كذلك. والإجراء هو بالضبط الاختيار العشوائي على طريقة Shannon من المحاضرة 03، مع تغيير واحد مهم: مولّد n-gram ينسى كل ما هو أقدم من n-1 كلمة، بينما تحمل RNN البادئة كاملة في .
How to choose the word: greedy, sampling and temperatureكيف نختار الكلمة: الجشع، والاختيار العشوائي، ودرجة الحرارة
Step 2 hides a design choice. Greedy decoding always takes the most probable word. It is deterministic, so the same prefix always produces the same text, and SLP3 notes it tends to be generic and repetitive. Random sampling draws a word in proportion to its probability, which gives variety but sometimes picks a strange low-probability word. Temperature sampling sits between the two: divide the scores by before the softmax.
الخطوة 2 تخفي قرار تصميم. فك الترميز الجشع (greedy decoding) يأخذ دائمًا الكلمة الأعلى احتمالًا. وهو حتمي، فالبادئة نفسها تنتج دائمًا النص نفسه، ويلاحظ SLP3 أنه يميل إلى نص عام ومكرر. أما الاختيار العشوائي (random sampling) فيسحب كلمة بنسبة احتمالها، وهذا يعطي تنوعًا لكنه يختار أحيانًا كلمة غريبة منخفضة الاحتمال. والاختيار بدرجة الحرارة (temperature sampling) يقع بين الاثنين: اقسم الدرجات على قبل softmax.
Worked exampleمثال محلول
The same scores at two temperaturesالدرجات نفسها عند درجتي حرارة
At τ = 1عند τ = 1
Scores 2.0, 1.0, 0.1 give 0.659, 0.242, 0.099, as in the previous concept.الدرجات 2.0, 1.0, 0.1 تعطي 0.659, 0.242, 0.099، كما في المفهوم السابق.At τ = 0.5عند τ = 0.5
Scores become 4.0, 2.0, 0.2. Exponentials 54.60, 7.39, 1.22, sum 63.21, so the probabilities are 0.864, 0.117, 0.019.تصبح الدرجات 4.0, 2.0, 0.2. الأسس 54.60, 7.39, 1.22، والمجموع 63.21، إذن الاحتمالات 0.864, 0.117, 0.019.Resultالنتيجة
Halving the temperature moved the top word from 66% to 86%. As , sampling becomes greedy decoding.تنصيف درجة الحرارة نقل الكلمة الأولى من 66% إلى 86%. وعندما يصبح الاختيار العشوائي فك ترميز جشعًا.
Try it below. The toy model starts at <s> with about 0.62 on "the". Run greedy twice and you get the same sentence. Switch to sampling and pick "dog" when it comes up: after "on the", rug becomes likely instead of mat, because the hidden state still remembers the subject from four steps back, something a trigram model could not do.
جرّبه بالأسفل. النموذج المصغّر يبدأ من <s> باحتمال نحو 0.62 على "the". شغّل الوضع الجشع مرتين فتحصل على الجملة نفسها. انتقل إلى الاختيار العشوائي واختر "dog" عندما تظهر: بعد "on the" تصبح rug مرجّحة بدلًا من mat، لأن الحالة المخفية لا تزال تتذكر الفاعل من أربع خطوات سابقة، وهذا ما لا يستطيعه نموذج trigram.
- the0.62
- a0.28
- cat0.04
- dog0.04
- sat0.00
- slept0.00
- on0.00
- mat0.00
- rug0.00
- </s>0.00
The scores behind ŷ are a fixed toy table standing in for V h_t. They depend on the whole prefix, not only the last word: after "on the", the subject chosen several steps earlier decides whether mat or rug is likely. Greedy always takes the tallest bar, so it produces the same sentence every time.الدرجات التي يقوم عليها ŷ جدول تجريبي ثابت يحل محل V h_t. وهي تعتمد على البادئة كلها، لا على الكلمة الأخيرة فقط: بعد «on the» يحدد الفاعل الذي اختير قبل عدة خطوات أيّ الكلمتين أرجح، mat أم rug. الفك الجشع يأخذ دائماً أطول عمود، لذا ينتج الجملة نفسها في كل مرة.
Quick checkاختبار سريع
With greedy decoding, you generate twice from <s> using the same trained RNN LM. What do you get?باستخدام فك الترميز الجشع (greedy decoding)، تولّد مرتين بدءًا من <s> بنموذج RNN LM المدرَّب نفسه. ماذا تحصل؟
Recall
List the generation loop in four steps, including both stopping conditions.اذكر حلقة التوليد في أربع خطوات، مع شرطَي التوقف كليهما.
Feed <s>; choose a word from the softmax; feed that word's embedding as the next input; repeat until </s> is produced or the maximum length is reached.
أدخل <s>؛ اختر كلمة من softmax؛ أدخل تضمين تلك الكلمة مدخلًا تاليًا؛ كرّر حتى يُنتَج </s> أو يُبلَغ الحد الأقصى للطول.
Slide 16 shows the same model again, but its equations come from Stanford CS224n, not SLP3. Read it with slide 14 in mind and you hit a trap at once: the output distribution is , yet on slide 14 the matrix was the recurrent one. Same letter, different matrix.
الشريحة 16 تعرض النموذج نفسه مرة أخرى، لكن معادلاتها مأخوذة من Stanford CS224n لا من SLP3. اقرأها وفي ذهنك الشريحة 14 وستقع في فخ فورًا: توزيع الإخراج هو ، مع أن المصفوفة في الشريحة 14 كانت المصفوفة التكرارية. الحرف نفسه، والمصفوفة مختلفة.
| Roleالدور | Slide 14 (SLP3)الشريحة 14 (SLP3) | Slide 16 (CS224n)الشريحة 16 (CS224n) |
|---|---|---|
| Embedding lookupالبحث عن التضمين | EE | EE |
| Input embedding to hiddenمن تضمين الإدخال إلى المخفية | WW | W_eW_e |
| Hidden to hidden (recurrent)من المخفية إلى المخفية (التكرارية) | UU | W_hh (or W_h)W_hh (or W_h) |
| Hidden to output scoresمن المخفية إلى درجات الإخراج | VV | UU |
| Biasesالانحيازات (biases) | Omittedمحذوفة | b_1, b_2b_1, b_2 |
| Nonlinearityالدالة غير الخطية | g (often tanh)g (غالبًا tanh) | σ (any nonlinearity)σ (أي دالة غير خطية) |
| Time indexفهرس الزمن | subscript: h_tرمز سفلي: h_t | superscript: h^(t)رمز علوي: h^(t) |
Beyond the letters, slide 16 adds two real details. It includes bias vectors b_1 and b_2, which SLP3 drops to keep the equations short. And it states the output space explicitly, , which is the shape check from the previous concept. Its figure also shows an initial hidden state h^(0), usually a vector of zeros, and the softmax histogram over the vocabulary at the last step, with mat and floor as likely continuations of "The cat slept quietly on the".
بعيدًا عن الحروف، تضيف الشريحة 16 تفصيلين حقيقيين. فهي تتضمن متجهي الانحياز b_1 وb_2، اللذين يحذفهما SLP3 لإبقاء المعادلات قصيرة. وتنص صراحة على فضاء الإخراج ، وهو فحص الأبعاد نفسه من المفهوم السابق. ويُظهر رسمها أيضًا حالة مخفية ابتدائية h^(0)، وهي عادة متجه من الأصفار، ومدرّج softmax على المفردات في الخطوة الأخيرة، حيث mat وfloor تكملتان مرجّحتان لـ "The cat slept quietly on the".
Quick checkاختبار سريع
On slide 16 (CS224n notation), which matrix plays the role that V plays on slide 14?في الشريحة 16 (ترميز CS224n)، أي مصفوفة تؤدي الدور الذي تؤديه V في الشريحة 14؟
Recall
Translate h^(t) = σ(W_hh h^(t-1) + W_e e^(t) + b_1) into SLP3 letters.ترجم h^(t) = σ(W_hh h^(t-1) + W_e e^(t) + b_1) إلى حروف SLP3.
h_t = g(U h_{t-1} + W e_t), dropping the bias. W_hh becomes U, W_e becomes W, and the output matrix CS224n calls U becomes V.
h_t = g(U h_{t-1} + W e_t)، مع حذف الانحياز. W_hh تصبح U، وW_e تصبح W، ومصفوفة الإخراج التي يسميها CS224n باسم U تصبح V.
Feed the RNN LM a five-word sentence and then a five-hundred-word article. The second run takes a hundred times as many steps, but it uses exactly the same E, W, U and V. Nothing in the parameter count mentions the sentence length. That one fact explains most of slide 17.
أدخل إلى RNN LM جملة من خمس كلمات ثم مقالًا من خمسمئة كلمة. التشغيل الثاني يستغرق خطوات أكثر بمئة مرة، لكنه يستخدم بالضبط E وW وU وV نفسها. لا شيء في عدد المعاملات يتعلق بطول الجملة. وهذه الحقيقة الواحدة تفسّر معظم الشريحة 17.
Why the model size does not grow with input lengthلماذا لا يزداد حجم النموذج بزيادة طول المدخل
The parameter count from the forward-pass worked example, d_e|V| + d_h d_e + d_h^2 + |V| d_h, contains no term for the sequence length T. Length only changes how many times the cell is applied. Compare the two alternatives. A count-based model that wants more context needs a larger n, and its table can grow as |V|^n. A fixed-window neural LM, as in Bengio and colleagues, concatenates the window's embeddings, so its first weight matrix grows with the window size, and CS224n notes that each window position gets its own separate weights. The RNN applies one rule at every step instead, which is weight sharing across time.
عدد المعاملات من المثال المحلول في التمرير الأمامي، d_e|V| + d_h d_e + d_h^2 + |V| d_h، لا يحتوي أي حد لطول التسلسل T. الطول يغيّر فقط عدد مرات تطبيق الخلية. قارن بالبديلين. النموذج القائم على العدّ الذي يريد سياقًا أوسع يحتاج n أكبر، وجدوله قد ينمو بمقدار |V|^n. ونموذج اللغة العصبي ذو النافذة الثابتة، كما عند Bengio وزملائه، يضم تضمينات النافذة متتالية، فتكبر مصفوفة أوزانه الأولى مع حجم النافذة، ويلاحظ CS224n أن لكل موضع في النافذة أوزانه المنفصلة. أما RNN فتطبّق قاعدة واحدة في كل خطوة، وهذا هو تشارك الأوزان عبر الزمن (weight sharing across time).
| Advantageالميزة | Whyالسبب | Remaining problemالمشكلة الباقية | Whyالسبب |
|---|---|---|---|
| Any length inputمدخل بأي طول | The same cell is applied once per word, so 5 or 500 words both workالخلية نفسها تُطبَّق مرة لكل كلمة، فتصلح 5 كلمات أو 500 كلمة | Recurrent computation is slowالحساب التكراري بطيء | h_t needs h_{t-1}, so the steps of one sequence cannot run in parallelh_t تحتاج h_{t-1}، فلا يمكن تشغيل خطوات التسلسل الواحد على التوازي |
| Model size does not growحجم النموذج لا يزداد | E, W, U, V are the same matrices at every stepE وW وU وV هي المصفوفات نفسها في كل خطوة | Long-range information is hard in practiceالمعلومات البعيدة صعبة عمليًا | Gradients shrink through many steps, so distant words are poorly usedالتدرّجات تتقلص عبر خطوات كثيرة، فلا يُستفاد جيدًا من الكلمات البعيدة |
| Long history in theoryتاريخ طويل نظريًا | h_t can carry information from the very first wordيمكن لـ h_t أن تحمل معلومات من الكلمة الأولى نفسها | ||
| Shared weights across timeأوزان مشتركة عبر الزمن | Every position is processed by the same rule, so what is learned at one position transfersكل موضع يُعالَج بالقاعدة نفسها، فما يُتعلَّم في موضع ينتقل إلى غيره |
The two remaining problemsالمشكلتان الباقيتان
Recurrent computation is slow. To compute you need , which needs , and so on. A sequence of T words therefore takes T steps one after another, even during training when every word is already known. A GPU cannot spread one sequence's time steps across its cores. Transformers remove this dependency and train all positions at once; that is the Parallel training of the transformer parts later in this lecture.
الحساب التكراري بطيء. لحساب تحتاج ، التي تحتاج ، وهكذا. لذلك يستغرق تسلسل من T كلمة عدد T من الخطوات واحدة بعد الأخرى، حتى أثناء التدريب حين تكون كل الكلمات معروفة مسبقًا. لا تستطيع GPU توزيع الخطوات الزمنية لتسلسل واحد على أنويتها. المحوّلات (Transformers) تزيل هذا الاعتماد وتدرّب كل المواضع دفعة واحدة؛ وهذا هو parallel training في أجزاء المحوّلات لاحقًا في هذه المحاضرة.
Long-range information is hard in practice. In theory, can carry the first word of the document. In practice, the gradient that tells early steps how to help a late prediction is multiplied by one factor per step on the way back, and over many steps it shrinks toward zero. That is the Vanishing gradient problem, and it is why gated cells such as the LSTM of part 2 exist. Even an LSTM squeezes the whole past into a fixed-size vector, the bottleneck that attention removes in part 5.
المعلومات البعيدة صعبة الاستخدام عمليًا. نظريًا، يمكن لـ أن تحمل الكلمة الأولى من المستند. عمليًا، التدرّج (gradient) الذي يخبر الخطوات المبكرة كيف تساعد تنبؤًا متأخرًا يُضرب في معامل واحد لكل خطوة في طريق العودة، وعبر خطوات كثيرة يتقلص نحو الصفر. هذه هي مشكلة vanishing gradient، ولهذا وُجدت الخلايا ذات البوابات مثل LSTM في الجزء 2. وحتى LSTM تضغط الماضي كله في متجه ثابت الحجم، وهو عنق الزجاجة (bottleneck) الذي يزيله الانتباه (attention) في الجزء 5.
Quick checkاختبار سريع
Why does an RNN LM's parameter count stay fixed as the input sentence grows longer?لماذا يبقى عدد معاملات RNN LM ثابتًا كلما طالت جملة الإدخال؟
Recall
Why can an RNN LM not be parallelized across time within one sequence, even in training?لماذا لا يمكن توزيع حساب RNN LM على التوازي عبر الزمن داخل تسلسل واحد، حتى أثناء التدريب؟
Each hidden state depends on the previous one through h_t = g(U h_{t-1} + W e_t), so step t cannot start until step t-1 is finished. The T steps form a chain.
كل حالة مخفية تعتمد على سابقتها عبر h_t = g(U h_{t-1} + W e_t)، فالخطوة t لا يمكن أن تبدأ قبل انتهاء الخطوة t-1. الخطوات الـ T تشكّل سلسلة.
In the slide's example, the input is "I saw a cat". A first RNN reads the words and produces hidden states h_1^(1) to h_4^(1). A second RNN treats that sequence of states as its input sequence and produces h_1^(2) to h_4^(2). Only the second layer's states feed the softmax that predicts "saw a cat on".
في مثال الشريحة، المدخل هو "I saw a cat". تقرأ RNN أولى الكلمات وتنتج الحالات المخفية من h_1^(1) إلى h_4^(1). ثم تعامل RNN ثانية هذا التسلسل من الحالات بوصفه تسلسل مدخلاتها وتنتج من h_1^(2) إلى h_4^(2). وحالات الطبقة الثانية وحدها تغذّي softmax التي تتنبأ بـ "saw a cat on".
This is a Stacked (multi-layer) RNN. Each layer has its own recurrent matrix and its own input matrix, and each carries its own hidden state through time, starting from its own h_0. The second layer's input matrix is d_h × d_h, because its input is a hidden state rather than an embedding. SLP3 reports that stacked RNNs generally outperform single-layer ones, likely because layers learn representations at different levels of abstraction, much as early visual areas detect edges that later areas assemble into shapes. The cost is more training time, and the best number of layers depends on the task and data.
هذه stacked RNN. لكل طبقة مصفوفتها التكرارية الخاصة ومصفوفة إدخالها الخاصة، وكل منها تحمل حالتها المخفية الخاصة عبر الزمن، بدءًا من h_0 الخاصة بها. مصفوفة إدخال الطبقة الثانية بحجم d_h × d_h، لأن مدخلها حالة مخفية لا تضمين. ويذكر SLP3 أن شبكات RNN المكدّسة تتفوق عمومًا على ذات الطبقة الواحدة، على الأرجح لأن الطبقات تتعلم تمثيلات على مستويات مختلفة من التجريد، كما تكتشف المناطق البصرية المبكرة الحواف التي تجمّعها المناطق اللاحقة في أشكال. والثمن هو وقت تدريب أطول، والعدد الأمثل للطبقات يعتمد على المهمة والبيانات.
Recall
In a two-layer RNN LM, what is the input of the second layer at step t, and which layer feeds the softmax?في نموذج RNN LM من طبقتين، ما مدخل الطبقة الثانية عند الخطوة t، وأي طبقة تغذّي softmax؟
The second layer's input at step t is the first layer's hidden state h_t^(1), in place of a word embedding. Only the top layer's state h_t^(2) feeds the output matrix and softmax.
مدخل الطبقة الثانية عند الخطوة t هو الحالة المخفية للطبقة الأولى h_t^(1)، بدلًا من تضمين كلمة. وحالة الطبقة العليا وحدها h_t^(2) تغذّي مصفوفة الإخراج وsoftmax.
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- n-gram LMs make an (n-1)-word Markov assumption and estimate probabilities by counting and smoothing; unseen n-grams get no evidence of their own.نماذج n-gram تفترض افتراض ماركوف بطول (n-1) كلمة، وتقدّر الاحتمالات بالعدّ ثم التنعيم (smoothing)؛ والـ n-gram التي لم تُرَ لا تملك أي دليل خاص بها.
- Neural LMs map words to learned vectors, so evidence about one word also helps similar words, and all parameters are learned by training.النماذج العصبية تحوّل الكلمات إلى متجهات متعلَّمة، فالدليل عن كلمة ما يفيد الكلمات المشابهة لها أيضًا، وكل المعاملات تُتعلَّم أثناء التدريب.
- RNN LM forward pass (SLP3): e_t = E x_t, h_t = g(U h_{t-1} + W e_t), ŷ_t = softmax(V h_t). The prediction is ŷ_t, not y_t.التمرير الأمامي في RNN LM (ترميز SLP3): e_t = E x_t ثم h_t = g(U h_{t-1} + W e_t) ثم ŷ_t = softmax(V h_t). التنبؤ هو ŷ_t وليس y_t.
- Shapes: x_t is |V|×1, E is d_e×|V|, W is d_h×d_e, U is d_h×d_h, V is |V|×d_h. With |V| = 10,000 and d_h = 512, V alone holds 5.12 M weights.الأبعاد: x_t بحجم |V|×1، وE بحجم d_e×|V|، وW بحجم d_h×d_e، وU بحجم d_h×d_h، وV بحجم |V|×d_h. عندما |V| = 10,000 وd_h = 512 تحمل V وحدها 5.12 M وزن.
- Loss at step t: L_CE = -log ŷ_t[w_{t+1}], the negative log probability of the true next word.الخسارة عند الخطوة t هي L_CE = -log ŷ_t[w_{t+1}]، أي سالب لوغاريتم الاحتمال الذي أعطاه النموذج للكلمة التالية الصحيحة.
- Generation is autoregressive: start at <s>, pick a word from ŷ, feed its embedding back, stop at </s> or a length limit. Greedy is deterministic; temperature reshapes the softmax.التوليد انحداري ذاتي (autoregressive): ابدأ من <s>، اختر كلمة من ŷ، أعد تضمينها مدخلًا، وتوقّف عند </s> أو عند حد أقصى للطول. الاختيار الجشع (greedy) حتمي، ودرجة الحرارة (temperature) تعيد تشكيل softmax.
- Slide 16 uses CS224n names: W_e is input, W_hh is recurrent, U is OUTPUT. On slide 14, U is recurrent and V is output.الشريحة 16 تستخدم أسماء CS224n: W_e مصفوفة الإدخال، وW_hh المصفوفة التكرارية، وU هي مصفوفة الإخراج. أما في الشريحة 14 فـ U هي التكرارية وV هي الإخراج.
- Advantages: any input length, fixed model size, long history in theory, shared weights. Problems: slow sequential computation, long-range information hard in practice.المزايا: أي طول للمدخل، وحجم نموذج ثابت، وتاريخ طويل نظريًا، وأوزان مشتركة. المشكلات: حساب تتابعي بطيء، وصعوبة الاستفادة من المعلومات البعيدة عمليًا.
- A two-layer RNN LM feeds h_t^(1) into the second layer as its input; only the top layer feeds the softmax.نموذج RNN LM من طبقتين يُدخل h_t^(1) إلى الطبقة الثانية بوصفه مدخلها، والطبقة العليا وحدها تغذّي softmax.
Sourcesالمصادر
- Speech and Language Processing, 3rd ed. draft, Chapter 14: RNNs and LSTMsBookJurafsky and Martin, StanfordSection 14.2: RNN LM equations 14.4 to 14.6, shapes, training, weight tying. Section 14.3.3: autoregressive generation. Section 14.4.1: stacked RNNs.القسم 14.2: معادلات RNN LM من 14.4 إلى 14.6، والأبعاد، والتدريب، وweight tying. القسم 14.3.3: التوليد الانحداري الذاتي (autoregressive generation). القسم 14.4.1: شبكات RNN المكدّسة.(opens in a new tab)(يفتح في تبويب جديد)
- Speech and Language Processing, 3rd ed. draft, Chapter 7: Transformers and PretrainingBookJurafsky and Martin, StanfordSection 7.6: greedy decoding, random sampling and temperature sampling.القسم 7.6: فك الترميز الجشع (greedy decoding)، والاختيار العشوائي (random sampling)، والاختيار بدرجة الحرارة (temperature sampling).(opens in a new tab)(يفتح في تبويب جديد)
- CS224N Lecture 5: Language Models and Recurrent Neural NetworksDocsChristopher Manning, StanfordSource of the slide 16 and 17 figures and notation; RNN advantages and disadvantages; fixed-window LM limits.مصدر رسمي الشريحتين 16 و17 وترميزهما؛ مزايا RNN وعيوبها؛ حدود نموذج اللغة ذي النافذة الثابتة.(opens in a new tab)(يفتح في تبويب جديد)
- A Neural Probabilistic Language ModelPaperBengio, Ducharme, Vincent and Jauvin, JMLR 3 (2003)Distributed word representations as the answer to the curse of dimensionality in n-gram modeling.التمثيلات الموزَّعة للكلمات بوصفها حلًا للعنة الأبعاد (curse of dimensionality) في نمذجة n-gram.(opens in a new tab)(يفتح في تبويب جديد)
- Recurrent neural network based language modelPaperMikolov, Karafiát, Burget, Černocký and Khudanpur, Interspeech 2010The original RNN LM; a mixture of RNN LMs cut perplexity by about 50% against a state-of-the-art backoff model.نموذج RNN LM الأصلي؛ خليط من نماذج RNN LM خفّض الـ perplexity بنحو 50% مقارنة بنموذج backoff كان الأفضل في وقته.(opens in a new tab)(يفتح في تبويب جديد)