Majid Al-RaimiSequence task shapes and the recurrent cellأشكال مهام التسلسل والخلية المتكررة

ICS 582Lecture 06المحاضرة 06Part 01الجزء 01

Sequence 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 واحدة.

Conceptsالمفاهيم
3
Slidesالشرائح
1-5
Readingمدة القراءة
18 min18 دقيقة
Understoodما فهمته
0/3 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

Up to now every language model in this course counted things. This lecture replaces counting with learned functions, and it starts with the most basic question a neural model of text must answer: how do you feed a sequence of unknown length into a network and get the right kind of answer out?

This part builds the vocabulary the rest of the lecture uses. First, the map of the lecture so you know where each later idea plugs in. Second, the five input/output shapes a sequence model can take, each tied to a real NLP task. Third, the recurrent cell itself: one small equation, reused at every word, whose weakness (it forgets) drives everything that comes after, from the LSTM to attention. Expect exam questions of two kinds: "which shape fits task X" and "write and explain the RNN recurrence".

حتى الآن، كان كل نموذج لغة في هذا المقرر يعتمد على العدّ. هذه المحاضرة تستبدل العدّ بدوال متعلَّمة، وتبدأ بأبسط سؤال يجب أن يجيب عنه أي نموذج عصبي للنص: كيف تُدخل تسلسلًا مجهول الطول إلى شبكة، وتحصل منها على النوع الصحيح من الإجابة؟

يبني هذا الجزء المفردات التي تستخدمها بقية المحاضرة. أولًا، خريطة المحاضرة حتى تعرف أين تندرج كل فكرة لاحقة. ثانيًا، أشكال الإدخال والإخراج الخمسة التي يمكن أن يتخذها نموذج التسلسل، وكل شكل مربوط بمهمة NLP حقيقية. ثالثًا، الخلية التكرارية نفسها: معادلة صغيرة واحدة يُعاد استخدامها عند كل كلمة، ونقطة ضعفها (أنها تنسى) هي ما يقود كل ما يأتي بعدها، من الـ LSTM إلى الانتباه (attention). توقّع أسئلة اختبار من نوعين: "أي شكل يناسب المهمة X" و"اكتب معادلة التكرار في الـ RNN واشرحها".

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Name the four arcs of this lecture and say what problem each one solves for the next.سمِّ المحاور الأربعة لهذه المحاضرة، وبيّن المشكلة التي يحلّها كل محور للمحور الذي يليه.
  2. Draw the five input/output shapes of a sequence model and match a concrete task to each.ارسم أشكال الإدخال والإخراج الخمسة لنموذج التسلسل، وطابق كل شكل مع مهمة محددة.
  3. Explain why machine translation needs the delayed many-to-many shape and tagging needs the synchronized one.اشرح لماذا تحتاج الترجمة الآلية إلى شكل many-to-many المؤجَّل (delayed)، بينما يحتاج الوسم (tagging) إلى الشكل المتزامن (synchronized).
  4. Write the simple RNN recurrence and output equation from memory and name every symbol.اكتب معادلة التكرار ومعادلة الإخراج للـ RNN البسيطة من الذاكرة، وسمِّ كل رمز فيهما.
  5. Explain parameter sharing across time steps and why it makes model size independent of sentence length.اشرح مشاركة المعاملات (parameter sharing) عبر الخطوات الزمنية، ولماذا تجعل حجم النموذج مستقلًا عن طول الجملة.
  6. Run a scalar RNN by hand for three steps and use the numbers to explain why simple RNNs forget.شغّل RNN عددية (scalar) يدويًا لثلاث خطوات، واستخدم الأرقام لتشرح لماذا تنسى الـ RNN البسيطة.

Take the sentence "The students who sat the midterm on Monday ___". A trigram model sees only "on Monday" and has no idea the blank belongs to a plural subject six words back. Every idea in this lecture is a better answer to that one problem: how to let the prediction depend on the whole history, not a fixed window of counts.

خذ الجملة "The students who sat the midterm on Monday ___". نموذج الـ trigram لا يرى إلا "on Monday"، وليست لديه أي فكرة أن الفراغ يعود إلى فاعل جمع يقع قبل ست كلمات. كل فكرة في هذه المحاضرة هي إجابة أفضل عن هذه المشكلة الواحدة: كيف نجعل التنبؤ يعتمد على التاريخ كله، لا على نافذة ثابتة من الأعداد.

The lecture is organized in four arcs, and each one fixes a limit of the one before it.

تنتظم المحاضرة في أربعة محاور، وكل محور يعالج قيدًا في المحور الذي قبله.

  1. Deep learning architectures: The Recurrent neural network (RNN) reads a sequence one token at a time and carries a memory forward. The Long short-term memory (LSTM) adds gates so that memory survives longer, and the Encoder-decoder (seq2seq) joins two RNNs so input and output sequences can have different lengths.معماريات التعلم العميق: تقرأ recurrent neural network التسلسل token تلو الآخر وتحمل معها ذاكرة إلى الأمام. ويضيف الـ LSTM بوابات (gates) حتى تصمد تلك الذاكرة مدة أطول، ويربط الـ encoder-decoder شبكتي RNN معًا حتى يمكن أن يختلف طول تسلسل الإدخال عن طول تسلسل الإخراج.
  2. Neural language models: A Neural language model uses an RNN to predict the next word, replacing the sparse counts of a Count-based (n-gram) language model with learned vectors, and is trained with cross-entropy and teacher forcing.نماذج اللغة العصبية: يستخدم neural language model شبكة RNN للتنبؤ بالكلمة التالية، فيستبدل الأعداد المتناثرة (sparse counts) في count-based LM بمتجهات متعلَّمة، ويُدرَّب باستخدام cross-entropy وteacher forcing.
  3. Transformers: The Transformer drops recurrence entirely and lets every position look at every other position directly through Self-attention.المحوّلات: يتخلى الـ transformer عن التكرار تمامًا، ويسمح لكل موضع بأن ينظر إلى كل موضع آخر مباشرة عبر self-attention.
  4. Transformer language models: Stacks of transformer blocks become decoder-only models such as GPT and encoder-only models such as BERT, with a language modeling head on top.نماذج اللغة المبنية على المحوّل: تتحول طبقات متراصّة من كتل المحوّل إلى نماذج من نوع decoder-only مثل GPT، ونماذج من نوع encoder-only مثل BERT، وفوقها رأس لنمذجة اللغة (language modeling head).
The four arcs of the lecture. This part sits at the start of the first arc, the recurrent architectures.المحاور الأربعة للمحاضرة. يقع هذا الجزء في بداية المحور الأول، أي المعماريات التكرارية.

Keep the thread in mind as you read: RNNs process tokens in order, so they are natural for sequences but slow to train and forgetful over long distances. Transformers answer both complaints. You cannot appreciate why attention matters until you have seen exactly where recurrence breaks, and that is what the rest of this part sets up.

أبقِ هذا الخيط في ذهنك وأنت تقرأ: تعالج الـ RNN الـ tokens بالترتيب، لذلك هي طبيعية للتسلسلات، لكنها بطيئة في التدريب وتنسى عبر المسافات الطويلة. المحوّلات تجيب عن الشكويين معًا. ولن تقدّر أهمية الانتباه (attention) حتى ترى بالضبط أين ينهار التكرار، وهذا ما تمهّد له بقية هذا الجزء.

Recall

Name the four arcs of the lecture in order, and say in one phrase what each adds to the previous one.سمِّ المحاور الأربعة للمحاضرة بالترتيب، وقل في عبارة واحدة ما يضيفه كل محور إلى ما قبله.

Architectures (RNN, LSTM, encoder-decoder: a memory that runs along the sequence), neural language models (use that memory to predict the next word instead of counting n-grams), transformers (replace recurrence with attention so every position sees every other), transformer language models (stack transformer blocks into GPT and BERT style models with a language modeling head).
المعماريات (RNN وLSTM وencoder-decoder: ذاكرة تسري على طول التسلسل)، ثم نماذج اللغة العصبية (تستخدم تلك الذاكرة للتنبؤ بالكلمة التالية بدل عدّ الـ n-grams)، ثم المحوّلات (تستبدل التكرار بالانتباه حتى يرى كل موضع كل موضع آخر)، ثم نماذج اللغة المبنية على المحوّل (ترصّ كتل المحوّل في نماذج على طراز GPT وBERT مع رأس لنمذجة اللغة).

Consider three jobs. Decide whether a movie review is positive. Tag every word of a sentence with its part of speech. Translate an English sentence into Arabic. All three take a sentence as input, yet they need very different networks, because what comes out differs: one label, one label per word, or a whole new sentence.

تأمّل ثلاث مهام. قرّر هل مراجعة فيلم إيجابية. ضع لكل كلمة في جملة وسمَ قسمها الكلامي (part of speech). ترجم جملة إنجليزية إلى العربية. المهام الثلاث كلها تأخذ جملة مدخلًا، ومع ذلك تحتاج إلى شبكات مختلفة جدًا، لأن ما يخرج مختلف: تسمية واحدة، أو تسمية لكل كلمة، أو جملة جديدة كاملة.

These input/output configurations are the sequence task shapes. Slide 3 reproduces Andrej Karpathy's well-known figure of five of them. Red boxes are inputs, green boxes are the hidden state, blue boxes are outputs, and the horizontal green arrows are the recurrence that carries state from one step to the next. The slide gives no example task, so here is the mapping you should be able to reproduce in an exam.

هذه التشكيلات من الإدخال والإخراج هي sequence task shapes (أشكال مهام التسلسل). تعيد الشريحة 3 رسم الشكل المشهور لـ Andrej Karpathy الذي يعرض خمسة منها. المربعات الحمراء هي المدخلات، والخضراء هي الحالة المخفية (hidden state)، والزرقاء هي المخرجات، والأسهم الخضراء الأفقية هي التكرار الذي ينقل الحالة من خطوة إلى التالية. لا تعطي الشريحة أي مهمة مثالًا، لذلك إليك الربط الذي يجب أن تستطيع إعادة كتابته في الاختبار.

ShapeالشكلInput and outputالإدخال والإخراجExample tasksأمثلة على المهامWhat to rememberما يجب تذكّره
One to oneOne to one1 input, 1 output, no recurrenceإدخال واحد، وإخراج واحد، بلا تكرارImage classificationتصنيف الصورThere is no sequence at all; this is the feedforward baseline the others extend.لا يوجد تسلسل أصلًا؛ هذا هو خط الأساس الأمامي (feedforward) الذي توسّعه الأشكال الأخرى.
One to manyOne to many1 input, a sequence outإدخال واحد، ويخرج تسلسلImage captioning; generating text from a single prompt vectorوصف الصور (image captioning)؛ توليد نص من متجه موجِّه (prompt) واحدThe input seeds the hidden state; each output step feeds the next.الإدخال يهيّئ الحالة المخفية؛ وكل خطوة إخراج تغذّي الخطوة التالية.
Many to oneMany to oneA sequence in, 1 outputيدخل تسلسل، ويخرج مخرج واحدSentiment classification; spam detectionتصنيف المشاعر (sentiment)؛ كشف الرسائل المزعجة (spam)Only the final hidden state is read, so it must summarize the whole input.لا تُقرأ إلا الحالة المخفية الأخيرة، لذلك يجب أن تلخّص الإدخال كله.
Many to many, delayedMany to many، مؤجَّل (delayed)Read all input, then emit a sequenceاقرأ الإدخال كله، ثم أخرج تسلسلًاMachine translation; summarizationالترجمة الآلية؛ التلخيصOutput length and order are free to differ from the input. This is the encoder-decoder.طول الإخراج وترتيبه يمكن أن يختلفا بحرية عن الإدخال. هذا هو الـ encoder-decoder.
Many to many, synchronizedMany to many، متزامن (synchronized)One output per input tokenمخرج واحد لكل token في الإدخالPart-of-speech tagging; named entity recognitionوسم أقسام الكلام (part-of-speech tagging)؛ التعرف على الكيانات المسماة (NER)Same length, same order; each label is emitted as its token is read.نفس الطول ونفس الترتيب؛ كل تسمية تُخرَج عند قراءة الـ token الخاص بها.
The five shapes, with concrete tasksالأشكال الخمسة مع مهام محددة

Pick a shape below and try to name a task for it before revealing the examples. The diagram redraws with the same color code as the slide: grey for inputs, the accent color for hidden states, teal for outputs.

اختر شكلًا أدناه وحاول أن تسمّي مهمة له قبل أن تكشف الأمثلة. يُعاد رسم المخطط بنفس ترميز الألوان في الشريحة: الرمادي للمدخلات، ولون التمييز (accent) للحالات المخفية، والأزرق المخضرّ (teal) للمخرجات.

InteractiveتفاعليFive sequence task shapesخمسة أشكال لمهام التسلسل

1 to 1: one fixed input, one output, no recurrence. Inputs drawn: 1, outputs drawn: 1. Nothing is a sequence, so a plain feedforward network is enough.من 1 إلى 1: مدخل ثابت واحد ومخرج واحد، دون تكرار (recurrence). المدخلات المرسومة: 1، والمخرجات المرسومة: 1. لا شيء هنا تسلسل، لذا تكفي شبكة أمامية التغذية (feedforward) عادية.

Read the figure by asking two questionsاقرأ الشكل بطرح سؤالين

You do not need to memorize five pictures. Ask two questions of any task. First, is the input a sequence, and is the output a sequence? That separates one to one, one to many, many to one and many to many. Second, if both are sequences, does each output line up with one input, at the same moment it is read? If yes, the shape is synchronized. If the output can only start once the whole input has been seen, it is delayed.

لا تحتاج إلى حفظ خمس صور. اطرح سؤالين على أي مهمة. الأول: هل الإدخال تسلسل، وهل الإخراج تسلسل؟ هذا يفصل بين one to one وone to many وmany to one وmany to many. الثاني: إذا كان كلاهما تسلسلًا، فهل يقابل كل مخرج مدخلًا واحدًا، في نفس اللحظة التي يُقرأ فيها؟ إذا كانت الإجابة نعم، فالشكل متزامن (synchronized). وإذا كان الإخراج لا يمكن أن يبدأ إلا بعد رؤية الإدخال كاملًا، فهو مؤجَّل (delayed).

Sentiment is many to one: the review is a sequence, the verdict is one label, and the network reads every word before deciding from its final hidden state. Spam detection is the same shape. Part-of-speech tagging and named entity recognition are synchronized many to many: the word "book" gets its tag when it is read, and the output has exactly as many labels as the input has tokens. Image captioning is one to many: one image vector in, a sentence out.

تحليل المشاعر (sentiment) هو many to one: المراجعة تسلسل، والحكم تسمية واحدة، والشبكة تقرأ كل كلمة قبل أن تقرر من حالتها المخفية الأخيرة. كشف الرسائل المزعجة (spam detection) له نفس الشكل. أما وسم أقسام الكلام (part-of-speech tagging) والتعرف على الكيانات المسماة (named entity recognition) فهما many to many متزامن: الكلمة "book" تأخذ وسمها عند قراءتها، وعدد التسميات في الإخراج يساوي تمامًا عدد الـ tokens في الإدخال. ووصف الصور (image captioning) هو one to many: متجه صورة واحد يدخل، وجملة تخرج.

Why translation needs the delayed shapeلماذا تحتاج الترجمة إلى الشكل المؤجَّل

Slide 9 later in this lecture gives the example that makes the point. English "Zaid went to his house" is five tokens. The Arabic ذهب زيد إلى بيته is four words, glossed "went Zaid to his-house". Two things break a synchronized model here. The lengths differ (five against four, because "his house" becomes one word), and the order differs (Arabic puts the verb first, verb-subject-object, while English puts the subject first). A synchronized network must emit the Arabic verb at the moment it reads "Zaid", before it has even seen "went". The only safe plan is to read the whole source first and then generate: the delayed shape, which is exactly the Encoder-decoder (seq2seq) of the next part.

تعطي الشريحة 9 لاحقًا في هذه المحاضرة المثال الذي يوضح الفكرة. الجملة الإنجليزية "Zaid went to his house" فيها خمسة tokens. والعربية ذهب زيد إلى بيته أربع كلمات، ومقابلها الحرفي "went Zaid to his-house". هناك أمران يكسران النموذج المتزامن هنا. الأطوال مختلفة (خمسة مقابل أربعة، لأن "his house" تصبح كلمة واحدة)، والترتيب مختلف (العربية تقدّم الفعل، فعل ثم فاعل ثم مفعول، بينما تقدّم الإنجليزية الفاعل). على الشبكة المتزامنة أن تُخرج الفعل العربي في اللحظة التي تقرأ فيها "Zaid"، قبل أن ترى حتى "went". الخطة الآمنة الوحيدة هي قراءة المصدر كاملًا أولًا ثم التوليد: أي الشكل المؤجَّل، وهو بالضبط الـ encoder-decoder في الجزء التالي.

Alignment between the English tokens and the Arabic gloss: the teal lines cross because the verb moves to the front, and two English tokens merge into one Arabic word.المحاذاة بين الكلمات الإنجليزية ومقابلها العربي: تتقاطع الخطوط الزرقاء المخضرّة لأن الفعل ينتقل إلى البداية، وتندمج كلمتان إنجليزيتان في كلمة عربية واحدة.

Quick checkاختبار سريع

Which shape fits named entity recognition, where every token receives a tag such as PERSON or O?أي شكل يناسب التعرف على الكيانات المسماة (NER)، حيث يأخذ كل token وسمًا مثل PERSON أو O؟

Quick checkاختبار سريع

Why does English to Arabic translation need the delayed many-to-many shape rather than the synchronized one?لماذا تحتاج الترجمة من الإنجليزية إلى العربية إلى شكل many-to-many المؤجَّل بدل المتزامن؟

Recall

Spam detection uses which shape, and which hidden state does the classifier read?أي شكل يستخدمه كشف الرسائل المزعجة، وأي حالة مخفية يقرؤها المصنِّف؟

Many to one. The network reads every token of the email and the classifier reads only the final hidden state, which therefore has to summarize the whole message.
Many to one. تقرأ الشبكة كل token في البريد، ولا يقرأ المصنِّف إلا الحالة المخفية الأخيرة، لذلك يجب أن تلخّص تلك الحالة الرسالة كلها.

Read the sentence "the movie was not good" one word at a time and keep a single running note in your head. After "not" the note says "something negative may be coming"; after "good" it says "negative overall". You never reread the sentence. You only update the note with the new word. That running note is the Hidden state, and the rule that updates it is the recurrent cell.

اقرأ الجملة "the movie was not good" كلمة كلمة، واحتفظ في ذهنك بملاحظة جارية واحدة. بعد "not" تقول الملاحظة "قد يأتي شيء سلبي"؛ وبعد "good" تقول "سلبية في المجمل". أنت لا تعيد قراءة الجملة أبدًا، بل تحدّث الملاحظة بالكلمة الجديدة فقط. تلك الملاحظة الجارية هي hidden state (الحالة المخفية)، والقاعدة التي تحدّثها هي الخلية التكرارية.

Slide 4 opens the architectures arc with its two families, RNNs and transformers. This part covers the first. Slide 5 draws the simplest Recurrent neural network (RNN) cell, taken from Christopher Olah's diagram: the previous hidden state h_(t-1) enters from the left, the current input x_t from below, both are combined and passed through one tanh layer, and the result leaves to the right as the new state h_t and upward as the output o_t.

تفتتح الشريحة 4 محور المعماريات بعائلتيه: RNNs والمحوّلات. يغطي هذا الجزء الأولى. وترسم الشريحة 5 أبسط خلية recurrent neural network، مأخوذة من مخطط Christopher Olah: تدخل الحالة المخفية السابقة h_(t-1) من اليسار، ويدخل الإدخال الحالي x_t من الأسفل، ويُدمجان ويمرّان عبر طبقة tanh واحدة، وتخرج النتيجة إلى اليمين بوصفها الحالة الجديدة h_t وإلى الأعلى بوصفها المخرج o_t.

The recurrence, written outمعادلة التكرار مكتوبة بالتفصيل

ht=tanh⁡(Whh ht−1+Wxh xt+b)h_t = \tanh\left(W_{hh}\,h_{t-1} + W_{xh}\,x_t + b\right)
Hidden state update of a simple (Elman) RNNتحديث الحالة المخفية في RNN بسيطة (Elman)
ot=Why ht,y^t=softmax⁡(ot)o_t = W_{hy}\,h_t,\qquad \hat{y}_t = \operatorname{softmax}(o_t)
Output at step tالمخرج عند الخطوة t
  • x_t is the input at step t, for text usually a word embedding of size d_x.x_t هو الإدخال عند الخطوة t، وهو في النصوص عادة word embedding بحجم d_x.
  • h_t is the hidden state of size d_h; h_0 is usually a zero vector.h_t هي الحالة المخفية بحجم d_h؛ وh_0 عادة متجه أصفار.
  • W_xh (d_h × d_x) maps the input into the hidden space, W_hh (d_h × d_h) maps the old state into the new one, b is a bias.W_xh (d_h × d_x) تنقل الإدخال إلى الفضاء المخفي، وW_hh (d_h × d_h) تنقل الحالة القديمة إلى الحالة الجديدة، وb هو الانحياز (bias).
  • W_hy maps the state to output scores, and a Softmax turns them into a probability distribution when the task needs one.W_hy تنقل الحالة إلى درجات الإخراج، ثم تحوّلها softmax إلى توزيع احتمالي عندما تحتاج المهمة إلى ذلك.

Jurafsky and Martin write the same equations as h_t = g(U h_(t-1) + W x_t) and y_t = f(V h_t), so in the textbook U is the recurrent matrix, W the input matrix and V the output matrix. The letters change between sources; the roles do not. The tanh keeps every component of h_t between -1 and 1, so the state cannot grow without bound as the sequence gets longer.

يكتب Jurafsky وMartin نفس المعادلات على الصورة h_t = g(U h_(t-1) + W x_t) وy_t = f(V h_t)، فتكون U في الكتاب هي المصفوفة التكرارية، وW مصفوفة الإدخال، وV مصفوفة الإخراج. الحروف تتغير بين المصادر، أما الأدوار فلا. وتُبقي tanh كل مكوّن من h_t بين -1 و1، فلا تستطيع الحالة أن تنمو بلا حد كلما طال التسلسل.

One set of weights, reused at every stepمجموعة أوزان واحدة يُعاد استخدامها في كل خطوة

The single most important property of the cell is that W_hh, W_xh, b and W_hy are the same at every time step. When you unroll the network over a four-word sentence you draw four boxes, but they are four copies of one cell, not four networks. The textbook says it directly: the weights are shared across all time steps, while new values of h and y are computed at each step.

أهم خاصية على الإطلاق في الخلية أن W_hh وW_xh وb وW_hy هي نفسها في كل خطوة زمنية. عندما تفرد (unroll) الشبكة على جملة من أربع كلمات ترسم أربعة صناديق، لكنها أربع نسخ من خلية واحدة، لا أربع شبكات. ويقولها الكتاب صراحة: الأوزان مشتركة عبر كل الخطوات الزمنية، بينما تُحسب قيم جديدة لـ h وy في كل خطوة.

Unrolled over four steps: one bank of weights feeds every copy of the cell, while the hidden state flows left to right.الشبكة مفرودة على أربع خطوات: مخزن أوزان واحد يغذّي كل نسخة من الخلية، بينما تسري الحالة المخفية من اليسار إلى اليمين.

Parameter sharing buys three things. The model handles sentences of any length with the same network. The number of parameters is fixed: with d_x = 100 and d_h = 128, the recurrent layer has 128 × 128 + 128 × 100 + 128 = 29,312 parameters whether the sentence has five words or five hundred. And whatever the cell learns about a pattern such as "not" followed by an adjective applies at every position, not only at the position where it was seen in training.

تمنحنا مشاركة المعاملات (parameter sharing) ثلاثة أشياء. يتعامل النموذج مع جمل بأي طول بنفس الشبكة. وعدد المعاملات ثابت: مع d_x = 100 وd_h = 128 تحتوي الطبقة التكرارية على 128 × 128 + 128 × 100 + 128 = 29,312 معاملًا سواء كانت الجملة خمس كلمات أو خمسمئة. وكل ما تتعلمه الخلية عن نمط ما، مثل "not" متبوعة بصفة، ينطبق في كل موضع، لا في الموضع الذي رأته فيه أثناء التدريب فقط.

Quick checkاختبار سريع

An RNN reads a 10-token sentence and later a 200-token sentence. How many recurrent matrices W_hh does it use?تقرأ RNN جملة من 10 tokens ثم لاحقًا جملة من 200 token. كم مصفوفة تكرارية W_hh تستخدم؟

Recall

Write the simple RNN equations from memory and say which quantities change with t and which do not.اكتب معادلات الـ RNN البسيطة من الذاكرة، وقل أي الكميات تتغير مع t وأيها لا تتغير.

h_t = tanh(W_hh h_(t-1) + W_xh x_t + b) and o_t = W_hy h_t (then softmax). The inputs x_t, the states h_t and the outputs o_t change with t. The weights W_hh, W_xh, W_hy and the bias b are shared and never change with t.
h_t = tanh(W_hh h_(t-1) + W_xh x_t + b) وo_t = W_hy h_t (ثم softmax). المدخلات x_t والحالات h_t والمخرجات o_t تتغير مع t. أما الأوزان W_hh وW_xh وW_hy والانحياز b فهي مشتركة ولا تتغير مع t أبدًا.

Running the cell by handتشغيل الخلية يدويًا

The vectors hide what the recurrence does, so shrink everything to scalars. Use w_hh = 0.5, w_xh = 1, b = 0, start from h_0 = 0, and feed a single pulse followed by silence: x = [1, 0, 0, 0].

المتجهات تخفي ما يفعله التكرار، لذلك صغّر كل شيء إلى قيم عددية (scalars). استخدم w_hh = 0.5 وw_xh = 1 وb = 0، وابدأ من h_0 = 0، وأدخل نبضة واحدة يتبعها صمت: x = [1, 0, 0, 0].

Worked exampleمثال محلول

A scalar RNN with one input pulseRNN عددية مع نبضة إدخال واحدة

  1. t = 1t = 1

    h_1 = tanh(0.5 × 0 + 1 × 1) = tanh(1) ≈ 0.762. The pulse enters the state.
    h_1 = tanh(0.5 × 0 + 1 × 1) = tanh(1) ≈ 0.762. تدخل النبضة إلى الحالة.
  2. t = 2t = 2

    h_2 = tanh(0.5 × 0.762 + 0) = tanh(0.381) ≈ 0.363. No new input, so the state is only the old state, halved and squashed.
    h_2 = tanh(0.5 × 0.762 + 0) = tanh(0.381) ≈ 0.363. لا يوجد إدخال جديد، لذلك الحالة هي الحالة القديمة فقط، بعد تنصيفها وضغطها.
  3. t = 3t = 3

    h_3 = tanh(0.5 × 0.363) = tanh(0.182) ≈ 0.180. One more step gives h_4 ≈ 0.090.
    h_3 = tanh(0.5 × 0.363) = tanh(0.182) ≈ 0.180. وخطوة أخرى تعطي h_4 ≈ 0.090.
  4. Resultالنتيجة

    The trace of the input roughly halves every step: 0.762, 0.363, 0.180, 0.090. After a dozen words it is effectively gone.
    أثر الإدخال ينخفض إلى النصف تقريبًا في كل خطوة: 0.762, 0.363, 0.180, 0.090. وبعد دزينة من الكلمات يكون قد اختفى فعليًا.
With w_hh = 0.5 the single input at t = 1 leaves a trace that roughly halves at every step.مع w_hh = 0.5 يترك الإدخال الوحيد عند t = 1 أثرًا ينخفض إلى النصف تقريبًا في كل خطوة.

Step through the same network below, and try the other recurrent weights. With w_hh = 1 the trace decays more slowly (0.762, 0.642, 0.566, 0.513); with w_hh = 2 the state saturates near 0.95 and holds itself there with no input at all, while the flat tanh makes every local factor small.

تنقّل خطوة بخطوة في نفس الشبكة أدناه، وجرّب الأوزان التكرارية الأخرى. مع w_hh = 1 يتلاشى الأثر ببطء أكبر (0.762, 0.642, 0.566, 0.513)؛ ومع w_hh = 2 تتشبّع الحالة قرب 0.95 وتثبّت نفسها هناك دون أي إدخال، بينما يجعل الجزء المسطّح من tanh كل عامل محلي صغيرًا.

InteractiveتفاعليUnrolled scalar RNN, one step at a timeRNN عددية مفرودة، خطوة في كل مرة
w_xh = 1, b = 0, x = [1, 0, 0, 0], h_0 = 0
  1. t = 1h = ?x = 1
  2. t = 2h = ?x = 0
  3. t = 3h = ?x = 0
  4. t = 4h = ?x = 0
Pre-activation-a_tpress Nextاضغط التالي
Hidden state-h_t = tanh(a_t)
Local factor-w_hh(1 - h_t²)
Gradient to h_1-∂h_t / ∂h_1

Before the first step, h is 0قبل الخطوة الأولى، قيمة h تساوي 0

stepالخطوة 0 / 4

The same three numbers, w_hh, w_xh and b, are used at every step. Only the input pulse at t = 1 is non-zero, so everything the later cells hold is an echo of it. The last readout multiplies the local factors together: it is how strongly a change in h_1 still moves h_t, which is the quantity backpropagation through time has to carry backwards.الأعداد الثلاثة نفسها، w_hh وw_xh وb، تُستخدم في كل خطوة. النبضة المدخلة عند t = 1 هي وحدها غير الصفرية، لذا فكل ما تحمله الخلايا اللاحقة هو صدى لها. القراءة الأخيرة تضرب العوامل المحلية بعضها في بعض: فهي تقيس إلى أي حد ما زال تغيّرٌ في h_1 يحرّك h_t، وهي الكمية التي يجب أن يحملها الانتشار العكسي عبر الزمن (backpropagation through time) إلى الخلف.

What the slides imply but never say: vanishing gradientsما تلمّح إليه الشرائح ولا تقوله أبدًا: تلاشي التدرّج (vanishing gradients)

Fading in the forward pass has a twin in training. To learn that a word at step 1 matters for a prediction at step 4, backpropagation must send the error back through every intermediate state. Each step multiplies it by the local derivative

للتلاشي في التمرير الأمامي (forward pass) توأم في التدريب. لكي تتعلم الشبكة أن كلمة عند الخطوة 1 مهمة لتنبؤ عند الخطوة 4، يجب أن يرسل الانتشار العكسي (backpropagation) الخطأ عائدًا عبر كل حالة وسيطة. وكل خطوة تضربه في المشتقة المحلية

∂ht∂ht−1=whh (1−ht2)\frac{\partial h_t}{\partial h_{t-1}} = w_{hh}\,\bigl(1 - h_t^{2}\bigr)
Local factor of a scalar tanh RNNالعامل المحلي في RNN عددية بدالة tanh

In our example these factors are 0.434, 0.484 and 0.496, so ∂h_4 / ∂h_1 ≈ 0.434 × 0.484 × 0.496 ≈ 0.104. Because the derivative of tanh is at most 1, any |w_hh| < 1 makes every factor smaller than 1, and the product shrinks geometrically with distance. Large weights do not rescue it either: they push tanh into saturation, where 1 - h² is tiny. This is the Vanishing gradient problem. Jurafsky and Martin describe it as gradients being driven to zero by the repeated multiplications of backpropagation through time, and Pascanu, Mikolov and Bengio show that the opposite case, factors above 1, causes exploding gradients, which are usually handled by clipping.

في مثالنا هذه العوامل هي 0.434 و0.484 و0.496، لذلك ∂h_4 / ∂h_1 ≈ 0.434 × 0.484 × 0.496 ≈ 0.104. ولأن مشتقة tanh لا تتجاوز 1، فإن أي |w_hh| < 1 تجعل كل عامل أصغر من 1، فيتقلص حاصل الضرب هندسيًا مع المسافة. والأوزان الكبيرة لا تنقذ الموقف أيضًا: فهي تدفع tanh إلى التشبّع، حيث تكون 1 - h² صغيرة جدًا. هذه هي مشكلة vanishing gradient. يصفها Jurafsky وMartin بأنها تدرّجات تُدفع نحو الصفر بسبب الضرب المتكرر في الانتشار العكسي عبر الزمن (backpropagation through time)، ويبيّن Pascanu وMikolov وBengio أن الحالة المعاكسة، أي عوامل أكبر من 1، تسبب انفجار التدرّج (exploding gradients)، الذي يُعالج عادة بالقصّ (clipping).

Quick checkاختبار سريع

A scalar RNN has w_hh = 0.5, w_xh = 1, b = 0, h_0 = 0 and inputs x = [1, 0, 0]. What is h_3?لدى RNN عددية w_hh = 0.5 وw_xh = 1 وb = 0 وh_0 = 0، والمدخلات x = [1, 0, 0]. ما قيمة h_3؟

Recall

Why does a simple RNN struggle to connect a word to a prediction twenty steps later?لماذا تجد الـ RNN البسيطة صعوبة في ربط كلمة بتنبؤ يأتي بعد عشرين خطوة؟

The gradient from the prediction back to that word is a product of twenty local factors w_hh(1 - h_t²). With tanh their size is usually below 1, so the product shrinks geometrically toward zero (vanishing gradients) and the weights receive almost no signal about the long-range dependency. The forward trace of the word fades the same way.
التدرّج من التنبؤ عائدًا إلى تلك الكلمة هو حاصل ضرب عشرين عاملًا محليًا w_hh(1 - h_t²). ومع tanh يكون حجمها عادة أقل من 1، فيتقلص حاصل الضرب هندسيًا نحو الصفر (تلاشي التدرّج) ولا تصل إلى الأوزان تقريبًا أي إشارة عن الاعتماد بعيد المدى. وأثر الكلمة في التمرير الأمامي يتلاشى بنفس الطريقة.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • The lecture has four arcs: deep learning architectures (RNN, LSTM, encoder-decoder), neural language models, the transformer, and transformer language models.للمحاضرة أربعة محاور: معماريات التعلم العميق (RNN وLSTM وencoder-decoder)، ونماذج اللغة العصبية، والمحوّل (transformer)، ونماذج اللغة المبنية على المحوّل.
  • One to one: fixed input, fixed output, no sequence (image classification). One to many: one input, a sequence out (image captioning).One to one: إدخال ثابت وإخراج ثابت، بلا تسلسل (تصنيف الصور). One to many: إدخال واحد يخرج منه تسلسل (وصف الصور، image captioning).
  • Many to one: read the whole sequence, classify from the final hidden state (sentiment, spam detection).Many to one: اقرأ التسلسل كاملًا، ثم صنّف من الحالة المخفية الأخيرة (تحليل المشاعر، كشف الرسائل المزعجة).
  • Many to many delayed: read everything, then emit an output of a different length and order (machine translation, the encoder-decoder case).Many to many المؤجَّل: اقرأ كل شيء، ثم أخرج تسلسلًا بطول وترتيب مختلفين (الترجمة الآلية، وهي حالة encoder-decoder).
  • Many to many synchronized: one label per token, same length, same order (POS tagging, NER).Many to many المتزامن: تسمية واحدة لكل token، بنفس الطول ونفس الترتيب (وسم أقسام الكلام POS tagging، والتعرف على الكيانات المسماة NER).
  • Simple RNN: h_t = tanh(W_hh h_{t-1} + W_xh x_t + b), o_t = W_hy h_t, usually followed by softmax.الـ RNN البسيطة: h_t = tanh(W_hh h_{t-1} + W_xh x_t + b)، وo_t = W_hy h_t، ويتبعها عادة softmax.
  • The same W_hh, W_xh, b and W_hy are reused at every time step, so the parameter count does not depend on sequence length.تُستخدم نفس W_hh وW_xh وb وW_hy في كل خطوة زمنية، لذلك لا يعتمد عدد المعاملات على طول التسلسل.
  • With w_hh = 0.5 and one input pulse, h runs 0.762, 0.363, 0.180, 0.090: the input's influence fades, and the gradient back to h_1 falls to about 0.104 after three steps. This is the vanishing gradient problem that motivates the LSTM.مع w_hh = 0.5 ونبضة إدخال واحدة، تتوالى قيم h هكذا: 0.762، 0.363، 0.180، 0.090. يتلاشى أثر الإدخال، ويهبط التدرّج الراجع إلى h_1 إلى نحو 0.104 بعد ثلاث خطوات. هذه هي مشكلة تلاشي التدرّج (vanishing gradient) التي تدفع إلى الـ LSTM.

Sourcesالمصادر