ICS 582Lecture 06المحاضرة 06
Neural Language Modelsنماذج اللغة العصبية
From recurrence to attention: RNN and LSTM sequence models, encoder-decoder RNNs, RNN language models trained with cross-entropy and teacher forcing, the transformer block (self-attention, multi-head, positional encoding, cross and causal attention), and transformer language models with the language modeling head and weight tying.من التكرار (recurrence) إلى الانتباه (attention): نماذج التسلسل RNN وLSTM، وشبكات RNN بنمط المشفر-المفكك (encoder-decoder)، ونماذج اللغة RNN المدربة بالإنتروبيا المتقاطعة والتغذية القسرية (teacher forcing)، وكتلة المحوِّل (transformer) (الانتباه الذاتي، والرؤوس المتعددة، والترميز الموضعي، والانتباه المتقاطع والسببي)، ونماذج اللغة بالمحوِّلات مع رأس نمذجة اللغة وربط الأوزان (weight tying).
- Partsالأجزاء
- 8
- Conceptsالمفاهيم
- 32
- Slidesالشرائح
- 43
- Readingمدة القراءة
- 192 min192 دقيقة
AOverviewنظرة عامة
This lecture replaces counting with learning. The n-gram models of lecture 3 looked up a fixed window of words in a count table. Here a network reads the whole prefix, turns it into vectors, and outputs a probability for every word in the vocabulary. The lecture builds that idea twice: first with recurrence, then with attention, which is the design behind every modern large language model.
تستبدل هذه المحاضرة العدّ بالتعلّم. نماذج n-gram في المحاضرة 3 كانت تبحث عن نافذة ثابتة من الكلمات في جدول عدّ. أما هنا فتقرأ الشبكة البادئة كاملة، وتحوّلها إلى متجهات، وتُخرج احتمالًا لكل كلمة في المفردات. تبني المحاضرة هذه الفكرة مرتين: أولًا بالتكرار (recurrence)، ثم بالانتباه (attention)، وهو التصميم الذي تقوم عليه كل نماذج اللغة الكبيرة الحديثة.
As a PhD student you need it for three reasons. Exam questions here are mechanical and exact: write the RNN or LSTM equations, trace the shapes through attention, compute a cross-entropy loss or a softmax by hand, and pick the right architecture for a task. Your own research will almost certainly use a pretrained transformer, and you cannot choose between a decoder-only, an encoder-only and an encoder-decoder model without knowing what each one is trained to do. Finally, papers in the field assume this vocabulary on page one, so fluency here is what lets you read them quickly.
كطالب دكتوراه تحتاجها لثلاثة أسباب. أسئلة الامتحان هنا آلية ودقيقة: اكتب معادلات RNN أو LSTM، وتتبّع الأبعاد (shapes) عبر الانتباه، واحسب خسارة cross-entropy أو softmax يدويًا، واختر البنية المناسبة للمهمة. وبحثك الخاص سيستخدم في الغالب transformer مدرّبًا مسبقًا، ولا يمكنك الاختيار بين نموذج decoder-only وencoder-only وencoder-decoder دون أن تعرف ما الذي يُدرَّب كل منها على فعله. وأخيرًا، الأوراق البحثية في المجال تفترض هذه المصطلحات من الصفحة الأولى، فإتقانها هنا هو ما يتيح لك قراءتها بسرعة.
The path has five stops. A simple RNN carries a hidden state through time and forgets because its gradient shrinks at every step. The LSTM adds a gated, additive cell state so memory and gradients survive, and an encoder-decoder pair of RNNs handles translation through a single context vector. An RNN language model predicts the next word at every position and is trained by minimizing -log ŷ[w_true] with teacher forcing. Attention then removes the two RNN limits, sequential computation and the fixed-size bottleneck, and the transformer block stacks it with a feedforward layer, residuals and layer norm. The last stop builds three model families from that block and closes the loop with the language modeling head.
للمسار خمس محطات. شبكة RNN البسيطة تحمل حالة مخفية (hidden state) عبر الزمن، وتنسى لأن تدرّجها (gradient) يتقلّص عند كل خطوة. تضيف LSTM حالة خلية (cell state) مبوّبة وتجميعية حتى تبقى الذاكرة والتدرجات، ويتعامل زوج encoder-decoder من شبكات RNN مع الترجمة عبر متجه سياق (context vector) واحد. نموذج اللغة المبني على RNN يتنبأ بالكلمة التالية عند كل موضع، ويُدرَّب بتقليل -log ŷ[w_true] مع teacher forcing. بعد ذلك يزيل الانتباه قيدَي RNN، أي الحساب التسلسلي وعنق الزجاجة ثابت الحجم، وتجمع كتلة transformer الانتباه مع طبقة feedforward ووصلات متبقية (residuals) وlayer norm. وتبني المحطة الأخيرة ثلاث عائلات من النماذج من هذه الكتلة، وتغلق الدائرة برأس نمذجة اللغة (language modeling head).
Success looks likeهكذا يبدو النجاح
- Write the simple RNN and the six LSTM equations from memory, name every symbol, and explain why the additive cell update eases vanishing gradients.اكتب معادلة RNN البسيطة ومعادلات LSTM الست من الذاكرة، وسمِّ كل رمز، واشرح لماذا يخفف تحديث الخلية التجميعي من تلاشي التدرجات (vanishing gradients).
- Match a task to its sequence shape and explain why translation needs an encoder-decoder with a context that becomes a bottleneck.طابِق المهمة مع شكل تسلسلها، واشرح لماذا تحتاج الترجمة إلى encoder-decoder بسياق يصبح عنق زجاجة.
- Write the RNN language model with the shape of every matrix, and compute the cross-entropy loss, the average loss and the perplexity of a short sentence.اكتب نموذج اللغة المبني على RNN مع أبعاد كل مصفوفة، واحسب خسارة cross-entropy ومتوسط الخسارة والحيرة (perplexity) لجملة قصيرة.
- Compute scaled dot-product attention by hand, justify the √d_k, and trace the shapes through eight heads of 64 dimensions.احسب scaled dot-product attention يدويًا، وبرّر القسمة على √d_k، وتتبّع الأبعاد عبر ثمانية رؤوس (heads) بأبعاد 64.
- Draw the causal mask, say where queries, keys and values come from in cross-attention, and evaluate a sinusoidal positional encoding.ارسم القناع السببي (causal mask)، وحدّد من أين تأتي queries وkeys وvalues في cross-attention، واحسب ترميزًا موضعيًا جيبيًا (sinusoidal positional encoding).
- Compare decoder-only, encoder-only and encoder-decoder models by attention pattern, objective, use and example.قارن بين نماذج decoder-only وencoder-only وencoder-decoder من حيث نمط الانتباه، والهدف التدريبي، والاستخدام، والمثال.
- Trace a final hidden vector through the tied unembedding and softmax, and explain why training runs in parallel while generation stays sequential.تتبّع المتجه المخفي النهائي عبر unembedding المربوط (tied) ثم softmax، واشرح لماذا يجري التدريب بالتوازي بينما يبقى التوليد تسلسليًا.
How to study this lectureكيف تذاكر هذه المحاضرة
- Choose your route. Read the full guide in one sitting for the RNN to transformer story, or take one part at a time when you want depth on the equations.اختر طريقك. اقرأ الدليل الكامل في جلسة واحدة لقصة الانتقال من RNN إلى transformer، أو خذ جزءًا واحدًا في كل مرة حين تريد التعمق في المعادلات.
- Redo every worked example on paper before reading its steps: the scalar RNN, the LSTM step, the softmax, the loss table and the attention calculation. The numbers are small on purpose.أعِد حل كل مثال محلول على الورق قبل قراءة خطواته: RNN العددية، وخطوة LSTM، وsoftmax، وجدول الخسارة، وحساب الانتباه. الأرقام صغيرة عن قصد.
- Answer every recall prompt in your head before revealing it, and take each quiz even when you feel sure. Read the explanation either way.أجب عن كل سؤال استرجاع في ذهنك قبل كشف إجابته، وحلّ كل اختبار قصير حتى لو كنت واثقًا. واقرأ الشرح في الحالتين.
- Use the interactive simulators to test a prediction, not to watch. Decide what the forget gate or a changed key will do first, then move the slider.استخدم المحاكيات التفاعلية لاختبار توقّع، لا للمشاهدة فقط. قرّر أولًا ماذا ستفعل بوابة النسيان (forget gate) أو تغيير key، ثم حرّك المنزلق.
- Read every slide erratum. Several slides drop a hat, a √d_k or a head index, and the exam answer should use the corrected form.اقرأ كل تصحيح لأخطاء الشرائح. عدة شرائح تُسقط علامة القبعة أو √d_k أو فهرس الرأس، ويجب أن تستخدم إجابة الامتحان الصيغة المصححة.
- Mark a concept as understood only when you can explain it without looking, then use the reference sheet for a last pass on formulas, shapes and comparisons. Retry the quizzes cold a few days later.لا تضع علامة "فهمته" على أي مفهوم إلا حين تستطيع شرحه دون النظر، ثم استخدم ورقة المراجعة لمرور أخير على المعادلات والأبعاد والمقارنات. وأعد حل الاختبارات القصيرة من الصفر بعد بضعة أيام.
Sourcesالمصادر
- Speech and Language Processing, 3rd ed. draft, Chapter 14: RNNs and LSTMsBookJurafsky and Martin, Stanford UniversityThe primary text for the first half: the simple RNN, RNN language models and their training, LSTMs, stacked and bidirectional RNNs, and the encoder-decoderالمرجع الأساسي للنصف الأول: شبكة RNN البسيطة، ونماذج اللغة المبنية على RNN وتدريبها، وLSTM، وشبكات RNN المكدّسة وثنائية الاتجاه، وencoder-decoder(opens in a new tab)(يفتح في تبويب جديد)
- Attention Is All You NeedPaperVaswani et al., NIPS 2017The original transformer: scaled dot-product and multi-head attention, sinusoidal positions, masking and the encoder-decoder layoutورقة transformer الأصلية: انتباه scaled dot-product والانتباه متعدد الرؤوس، والمواضع الجيبية، والأقنعة، وتصميم encoder-decoder(opens in a new tab)(يفتح في تبويب جديد)
BThe 8 partsالأجزاء الـ8
- 01Sequence task shapes and the recurrent cellأشكال مهام التسلسل والخلية المتكررةWhy different tasks need different input/output shapes, and how a simple RNN carries a hidden state through time with one tanh layer.لماذا تحتاج المهام المختلفة أشكالا مختلفة للمدخلات والمخرجات، وكيف تحمل شبكة RNN البسيطة حالة مخفية عبر الزمن بطبقة tanh واحدة.3 concepts3 مفاهيمSlides 1-5الشرائح 1-518 min18 دقيقة
- 1.1From recurrence to transformer language models: the lecture mapمن التكرار إلى نماذج اللغة المبنية على المحوّل: خريطة المحاضرة
- 1.2Five sequence task shapes and which task fits eachخمسة أشكال لمهام التسلسل وأي مهمة تناسب كل شكل
- 1.3The recurrent cell: one tanh layer reused at every stepالخلية التكرارية: طبقة tanh واحدة يُعاد استخدامها في كل خطوة
- 02LSTM gates, RNN variants and encoder-decoder RNNsبوابات LSTM ومتغيرات RNN وشبكات Encoder-decoderHow the LSTM's forget, input and output gates control a cell state, what GRUs, bidirectional and stacked RNNs add, and how an encoder compresses a source sentence into a context that a decoder unrolls into a target sentence.كيف تتحكم بوابات النسيان والإدخال والإخراج في LSTM بحالة الخلية، وما تضيفه GRU وشبكات RNN ثنائية الاتجاه والمكدسة، وكيف يضغط المشفر (encoder) الجملة المصدر في سياق يفرده المفكك (decoder) إلى جملة الهدف.4 concepts4 مفاهيمSlides 6-11الشرائح 6-1124 min24 دقيقة
- 2.1The LSTM: a cell state guarded by forget, input and output gatesLSTM: حالة خلية تحرسها بوابات النسيان والإدخال والإخراج
- 2.2GRUs, bidirectional RNNs and stacked RNNsGRU وشبكات RNN ثنائية الاتجاه وشبكات RNN المكدسة
- 2.3Why some tasks need an encoder and a decoderلماذا تحتاج بعض المهام إلى مُرمِّز (encoder) ومُفكِّك (decoder)
- 2.4The RNN encoder-decoder and its single context vectorنموذج RNN encoder-decoder ومتجه السياق الوحيد فيه
- 03RNN 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 متعددة الطبقات.6 concepts6 مفاهيمSlides 12-18الشرائح 12-1836 min36 دقيقة
- 3.1From counted n-grams to learned word vectorsمن عدّ الـ n-grams إلى متجهات كلمات متعلَّمة
- 3.2The RNN language model forward pass, matrix by matrixالتمرير الأمامي في نموذج لغة RNN، مصفوفةً مصفوفة
- 3.3Autoregressive generation: feeding the model its own wordsالتوليد الانحداري الذاتي: تغذية النموذج بكلماته هو
- 3.4Reading the second notation without getting lostقراءة الترميز الثاني دون أن تضيع
- 3.5What recurrence buys and what it costsماذا يكسب التكرار وماذا يكلّف
- 3.6Stacking RNN layers for language modelingتكديس طبقات RNN لنمذجة اللغة
- 04Self-supervised training: cross-entropy and teacher forcingالتدريب الذاتي الإشراف: الإنتروبيا المتقاطعة والتغذية القسريةHow a language model learns from raw text by predicting the next word, why the cross-entropy loss collapses to minus log of the true word's probability, and why training feeds the gold word instead of the model's guess.كيف يتعلم نموذج اللغة من النص الخام بالتنبؤ بالكلمة التالية، ولماذا تختزل خسارة الإنتروبيا المتقاطعة إلى سالب لوغاريتم احتمال الكلمة الصحيحة، ولماذا يغذي التدريب الكلمة الصحيحة (gold) بدلا من تخمين النموذج (teacher forcing).3 concepts3 مفاهيمSlides 19-22الشرائح 19-2218 min18 دقيقة
- 4.1Self-supervised training: the text is its own labelالتدريب ذاتي الإشراف: النص هو تسمية نفسه
- 4.2Cross-entropy collapses to minus log of the true wordالإنتروبيا المتقاطعة تختزل إلى سالب لوغاريتم الكلمة الصحيحة
- 4.3Teacher forcing: always condition on the gold historyTeacher forcing: اشترط دائمًا على التاريخ الصحيح (gold)
- 05From RNN limits to the transformer blockمن حدود RNN إلى كتلة المحوِّل (Transformer)Why RNNs struggle with coreference and a fixed-size context bottleneck, how self-attention and cross-attention fix that, and what a transformer block (attention, feedforward, residuals, layer norm) computes.لماذا تعاني RNN مع الإحالة المرجعية (coreference) وعنق الزجاجة لسياق ثابت الحجم، وكيف يعالج الانتباه الذاتي والانتباه المتقاطع ذلك، وما الذي تحسبه كتلة المحوِّل (الانتباه، والتغذية الأمامية، والوصلات المتبقية residuals، وتطبيع الطبقة layer norm).3 concepts3 مفاهيمSlides 23-26الشرائح 23-2618 min18 دقيقة
- 5.1Why RNNs struggle and what self-attention fixesلماذا تتعثر شبكات RNN وما الذي يصلحه self-attention
- 5.2The encoder bottleneck and the need for cross-attentionعنق الزجاجة في الـ encoder والحاجة إلى cross-attention
- 5.3The transformer block: attention, add, norm, feedforwardكتلة الـ transformer: الانتباه، ثم الجمع، ثم التطبيع، ثم الـ feedforward
- 06Self-attention, multi-head, positional, cross and causal attentionالانتباه الذاتي والرؤوس المتعددة والموضعي والمتقاطع والسببيThe query, key, value computation in full, scaled dot-product attention in matrix form, why we use several heads, how positional encodings inject word order, how decoders cross-attend to the encoder, and how the causal mask hides the future.حساب الاستعلام والمفتاح والقيمة (query وkey وvalue) كاملا، والانتباه بالضرب النقطي المُقيَّس (scaled dot-product) بصيغة المصفوفات، ولماذا نستخدم عدة رؤوس، وكيف يُدخل الترميز الموضعي ترتيب الكلمات، وكيف ينتبه المفكك بشكل متقاطع إلى المشفر، وكيف يخفي القناع السببي (causal mask) المستقبل.5 concepts5 مفاهيمSlides 27-31الشرائح 27-3130 min30 دقيقة
- 6.1Queries, keys, values and scaled dot-product attentionالـ queries والـ keys والـ values والـ scaled dot-product attention
- 6.2Multi-head attention: several relations in parallelالـ multi-head attention: عدة علاقات بالتوازي
- 6.3Positional encoding: giving attention a sense of orderالـ positional encoding: منح الـ attention إحساسًا بالترتيب
- 6.4Cross-attention: the decoder reading the encoderالـ cross-attention: الـ decoder يقرأ الـ encoder
- 6.5The causal mask: hiding the future with minus infinityالـ causal mask: إخفاء المستقبل بسالب ما لا نهاية
- 07Decoder-only, encoder-only and encoder-decoder modelsنماذج Decoder-only وEncoder-only وEncoder-decoderThe three ways to build a language model from transformer blocks, what each is trained to do and used for, and why transformer LM training runs in parallel across positions.الطرق الثلاث لبناء نموذج لغة من كتل المحوِّل، وما يُدرَّب عليه كل منها وما يُستخدم له، ولماذا يجري تدريب نماذج اللغة بالمحوِّلات بالتوازي عبر المواضع.5 concepts5 مفاهيمSlides 32-37الشرائح 32-3730 min30 دقيقة
- 7.1Three ways to wire transformer blocks into a language modelثلاث طرق لتوصيل كتل الـ transformer في نموذج لغة
- 7.2Encoder-only models: masked language models and BERTنماذج encoder-only: نماذج اللغة المقنّعة (masked language models) و BERT
- 7.3Encoder-decoder transformers for sequence-to-sequence tasksنماذج transformer من نوع encoder-decoder لمهام تحويل تسلسل إلى تسلسل
- 7.4Decoder-only models: causal, autoregressive, left-to-rightنماذج decoder-only: سببية، ذاتية الانحدار (autoregressive)، من اليسار إلى اليمين
- 7.5Why training runs in parallel but generation does notلماذا يجري التدريب بالتوازي بينما لا يجري التوليد كذلك
- 08The language modeling head, weight tying and end-to-end trainingرأس نمذجة اللغة وربط الأوزان والتدريب من الطرف إلى الطرفHow the final hidden vector becomes a probability over the vocabulary through an unembedding layer tied to the embedding matrix, how a token is sampled, and how the full stacked model is trained with averaged cross-entropy.كيف يتحول المتجه المخفي الأخير إلى احتمال على المفردات عبر طبقة فك التضمين (unembedding) المربوطة بمصفوفة التضمين، وكيف يُسحب رمز (token)، وكيف يُدرَّب النموذج المكدس كاملا بمتوسط الإنتروبيا المتقاطعة.3 concepts3 مفاهيمSlides 38-43الشرائح 38-4318 min18 دقيقة
- 8.1The language modeling head: from the last hidden vector to a distribution over the vocabularyرأس نمذجة اللغة: من المتجه المخفي الأخير إلى توزيع احتمالي على المفردات
- 8.2Closing the loop: sampling the next token and feeding it backإغلاق الحلقة: سحب الرمز التالي وإعادته مدخلًا للنموذج
- 8.3Training the whole stack end to end with averaged cross-entropyتدريب النموذج كاملًا من البداية إلى النهاية بمتوسط الإنتروبيا المتقاطعة
CGlossary and referenceالمصطلحات والمرجع
- GlossaryالمصطلحاتEvery term from the lecture, defined once, with links back to the slides that use it.كل مصطلح في المحاضرة معرّف مرة واحدة، مع روابط تعيدك إلى الشرائح التي تستخدمه.
- Reference sheetالورقة المرجعيةThe lecture on one printable page, for revision before a quiz or exam.المحاضرة كاملة في صفحة واحدة قابلة للطباعة، للمراجعة قبل الاختبار القصير أو الامتحان.