ICS 582Lecture 06المحاضرة 06Part 02الجزء 02
LSTM gates, RNN variants and encoder-decoder RNNsبوابات LSTM ومتغيرات RNN وشبكات Encoder-decoder
How 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) إلى جملة الهدف.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 6-11
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Part 01 ended with the weakness of the simple RNN: the same hidden state has to carry long-range memory and make the current prediction, and the gradient that should teach it shrinks every time it is multiplied back through a step. This part is the fix and what it unlocked.
First, the LSTM: a separate memory line with three gates that decide what to erase, what to write and what to show. Second, three variants the slides name in one line each: the GRU, bidirectional RNNs and stacked RNNs. Third, the encoder-decoder idea, which turns a sentence in one language into a sentence in another. Fourth, the concrete RNN version of it from the textbook figure on slide 11, and the single-vector bottleneck that motivates attention in part 05. For the exam, the six LSTM equations and the role of each gate are the core: expect to write them and reason about extreme gate values.
انتهى الجزء 01 بنقطة ضعف RNN البسيطة: الحالة المخفية نفسها مطلوب منها أن تحمل الذاكرة بعيدة المدى وأن تصنع التنبؤ الحالي في الوقت ذاته، والتدرج الذي يفترض أن يعلّمها ذلك يصغر في كل مرة يُضرب فيها راجعًا عبر خطوة. هذا الجزء هو الحل، وما أتاحه هذا الحل.
أولًا، LSTM: خط ذاكرة منفصل له ثلاث بوابات تقرر ماذا تمحو، وماذا تكتب، وماذا تُظهر. ثانيًا، ثلاثة أشكال مشتقة تذكرها الشرائح في سطر واحد لكل منها: GRU وشبكات RNN ثنائية الاتجاه وشبكات RNN المكدسة. ثالثًا، فكرة encoder-decoder التي تحوّل جملة في لغة إلى جملة في لغة أخرى. رابعًا، نسختها العملية المبنية على RNN من شكل الكتاب في الشريحة 11، وعنق الزجاجة الناتج عن المتجه الواحد الذي يمهّد للانتباه (attention) في الجزء 05. للامتحان، معادلات LSTM الست ودور كل بوابة هي الأساس: توقع أن تكتبها وأن تستدل على ما يحدث عند القيم القصوى للبوابات.
By the end you canفي نهاية هذا الجزء ستستطيع
- Write the six LSTM equations from slide 7 from memory and say what each gate decides.تكتب معادلات LSTM الست من الشريحة 7 من الذاكرة، وتشرح ما تقرره كل بوابة.
- Compute one LSTM step by hand and predict what happens when the forget gate is near 0 or near 1.تحسب خطوة LSTM واحدة يدويًا، وتتوقع ما يحدث عندما تكون بوابة النسيان (forget gate) قريبة من 0 أو من 1.
- Explain why the additive cell update lets information and gradients survive many time steps.تشرح لماذا يسمح التحديث الجمعي (additive) لحالة الخلية بأن تبقى المعلومات والتدرجات (gradients) عبر خطوات زمنية كثيرة.
- Contrast the GRU, bidirectional RNNs and stacked RNNs, and say which of them can generate text left to right.تقارن بين GRU وشبكات RNN ثنائية الاتجاه (bidirectional) وشبكات RNN المكدسة (stacked)، وتحدد أيها يستطيع توليد النص من اليسار إلى اليمين.
- Describe the RNN encoder-decoder: what the context is, how <s> and </s> are used, and why one vector is a bottleneck.تصف نموذج RNN encoder-decoder: ما هو السياق (context)، وكيف يُستخدم <s> و</s>، ولماذا يمثل المتجه الواحد عنق زجاجة (bottleneck).
Read this sentence word by word: “The keys that I left on the kitchen table yesterday aregone.” To choose are rather than is, a model must remember that the subject, keys, was plural, and hold that fact across eight intervening words while ignoring table, which is singular. A simple RNN rewrites its whole Hidden state at every step, so the fact “plural subject” has to survive eight full rewrites. In practice it fades, and during training the signal that should teach it to survive fades too: this is the Vanishing gradient problem.
اقرأ هذه الجملة كلمة كلمة: “The keys that I left on the kitchen table yesterday are gone.” (المفاتيح التي تركتها على طاولة المطبخ أمس اختفت). لكي يختار النموذج are بدلًا من is، يجب أن يتذكر أن الفاعل keys جمع، وأن يحتفظ بهذه المعلومة عبر ثماني كلمات فاصلة متجاهلًا كلمة table وهي مفردة. شبكة RNN البسيطة تعيد كتابة hidden state (الحالة المخفية) بالكامل في كل خطوة، لذلك يجب أن تنجو معلومة “الفاعل جمع” من ثماني عمليات إعادة كتابة كاملة. عمليًا تتلاشى هذه المعلومة، وأثناء التدريب تتلاشى أيضًا الإشارة التي يفترض أن تعلّمها البقاء: هذه هي مشكلة vanishing gradient (تلاشي التدرج).
The Long short-term memory (LSTM) changes the design rather than the training. It adds a second vector that runs beside the hidden state, the Cell state C(t), and it only lets three small learned switches touch it. One switch decides what to erase, one decides what to write, and one decides how much of the memory to reveal. When nothing needs to change, the switches can leave the memory almost untouched for many steps. The diagram on slide 6 is Christopher Olah’s drawing: the horizontal line across the top of each block is the cell state, the yellow boxes are learned layers, and the pink circles are elementwise operations.
تغيّر LSTM التصميم نفسه لا طريقة التدريب. فهي تضيف متجهًا ثانيًا يسير بجانب الحالة المخفية، وهو cell state (حالة الخلية) C(t)، ولا تسمح بلمسه إلا لثلاثة مفاتيح صغيرة متعلَّمة. مفتاح يقرر ماذا يُمحى، ومفتاح يقرر ماذا يُكتب، ومفتاح يقرر كم يُكشف من الذاكرة. عندما لا يلزم تغيير شيء، تستطيع المفاتيح أن تترك الذاكرة شبه سليمة لخطوات كثيرة. المخطط في الشريحة 6 من رسم Christopher Olah: الخط الأفقي أعلى كل كتلة هو حالة الخلية، والمربعات الصفراء طبقات متعلَّمة، والدوائر الوردية عمليات تُجرى عنصرًا بعنصر.
Every gate is the same recipeكل البوابات تتبع الوصفة نفسها
All three gates on slide 7 share one form. Take the previous hidden state and the current input, glue them into one longer vector [h(t-1), x(t)] (this bracket is concatenation, not a list of two things fed separately), multiply by the gate’s own weight matrix, add its bias, and squash with the sigmoid σ. The sigmoid output lies between 0 and 1. That is all the arrow in “(0 → 1)” on the slide means: the range of the gate value, not a transition from 0 to 1 over time.
البوابات الثلاث في الشريحة 7 لها الشكل نفسه. خذ الحالة المخفية السابقة والمدخل الحالي، وألصقهما في متجه واحد أطول [h(t-1), x(t)] (هذا القوس يعني الضمّ concatenation، وليس قائمة من شيئين يُمرَّر كل منهما وحده)، ثم اضربه في مصفوفة الأوزان الخاصة بالبوابة، وأضف الانحياز (bias)، ثم اضغطه بدالة sigmoid σ. ناتج sigmoid يقع بين 0 و1. وهذا كل ما يعنيه السهم في “(0 → 1)” على الشريحة: إنه مدى قيمة البوابة، وليس انتقالًا من 0 إلى 1 مع مرور الزمن.
A fourth layer has the same shape but uses tanh instead of σ. It proposes the new content, the candidate C̃(t), with values between -1 and 1 so it can push memory up or down:
طبقة رابعة لها الشكل نفسه لكنها تستخدم tanh بدلًا من σ. هي تقترح المحتوى الجديد، أي المرشح C̃(t)، بقيم بين -1 و1 حتى تستطيع رفع الذاكرة أو خفضها:
Then the two update lines, which are the heart of the LSTM:
ثم يأتي سطرا التحديث، وهما قلب LSTM:
The slide writes the products with a dot, f(t) · C(t-1). Here the dot means the elementwise (Hadamard) product, written ⊙ above: component k of the result is component k of f(t) times component k of C(t-1). It is not a dot product, which would collapse two vectors into one number. Because each component has its own gate value, the LSTM can keep dimension 3 (say, “subject is plural”) while erasing dimension 7 in the same step.
تكتب الشريحة حواصل الضرب بنقطة، f(t) · C(t-1). النقطة هنا تعني الضرب عنصرًا بعنصر (elementwise أو Hadamard product)، المكتوب ⊙ في المعادلات أعلاه: المكوّن k من الناتج يساوي المكوّن k من f(t) مضروبًا في المكوّن k من C(t-1). إنه ليس الضرب النقطي (dot product) الذي يختزل متجهين في رقم واحد. ولأن لكل مكوّن قيمة بوابة خاصة به، تستطيع LSTM أن تحتفظ بالبُعد 3 (مثلًا “الفاعل جمع”) وتمحو البُعد 7 في الخطوة نفسها.
What each gate does at its extremesماذا تفعل كل بوابة عند قيمها القصوى
- Forget gate near 1: the old memory passes through almost unchanged. Near 0: it is wiped. A language model might drop the subject’s number once the verb has been produced.forget gate (بوابة النسيان) قريبة من 1: تمر الذاكرة القديمة شبه دون تغيير. قريبة من 0: تُمسح. قد يتخلص نموذج لغة من معلومة عدد الفاعل بعد أن يكون قد أنتج الفعل.
- Input gate near 0: nothing new is written, whatever the candidate says. Near 1: the full candidate is added.input gate (بوابة الإدخال) قريبة من 0: لا يُكتب شيء جديد مهما كان المرشح. قريبة من 1: يُضاف المرشح كاملًا.
- Output gate near 0: the memory is kept privately and h(t) is close to 0, so this step’s prediction ignores it. Near 1: the whole squashed memory tanh(C(t)) is shown.output gate (بوابة الإخراج) قريبة من 0: تُحفظ الذاكرة سرًّا وتكون h(t) قريبة من 0، فيتجاهلها تنبؤ هذه الخطوة. قريبة من 1: تظهر الذاكرة المضغوطة كاملة tanh(C(t)).
Note the separation: the output gate changes only h(t), never C(t). The LSTM can hide a fact for ten words and still have it intact when it becomes useful.
لاحظ هذا الفصل: بوابة الإخراج تغيّر h(t) فقط، ولا تغيّر C(t) أبدًا. تستطيع LSTM أن تخفي معلومة لعشر كلمات وتبقى سليمة لديها عندما تصبح مفيدة.
Worked exampleمثال محلول
One LSTM step with scalar valuesخطوة LSTM واحدة بقيم عددية مفردة
The settingالمعطيات
Use one-dimensional vectors so every product is ordinary multiplication. Old memory C(t-1) = 2. Suppose the gate layers produced f = 0.9, i = 0.5, o = 0.6 and the candidate is C̃ = 0.8.نستخدم متجهات أحادية البعد حتى يكون كل ضرب ضربًا عاديًا. الذاكرة القديمة C(t-1) = 2. لنفترض أن طبقات البوابات أنتجت f = 0.9 وi = 0.5 وo = 0.6، وأن المرشح C̃ = 0.8.Keep part of the old memoryاحتفظ بجزء من الذاكرة القديمة
f · C(t-1) = 0.9 × 2 = 1.8.f · C(t-1) = 0.9 × 2 = 1.8.Write part of the candidateاكتب جزءًا من المرشح
i · C̃ = 0.5 × 0.8 = 0.4.i · C̃ = 0.5 × 0.8 = 0.4.Add themاجمعهما
C(t) = 1.8 + 0.4 = 2.2.C(t) = 1.8 + 0.4 = 2.2.Expose a gated viewاكشف نسخة مرّت عبر البوابة
tanh(2.2) ≈ 0.976, so h(t) = 0.6 × 0.976 ≈ 0.585.tanh(2.2) ≈ 0.976، إذن h(t) = 0.6 × 0.976 ≈ 0.585.Now set the forget gate to 0الآن اجعل بوابة النسيان 0
C(t) = 0 × 2 + 0.4 = 0.4. Everything stored before is gone, and only the new write remains. Then h(t) = 0.6 × tanh(0.4) ≈ 0.6 × 0.380 ≈ 0.228.C(t) = 0 × 2 + 0.4 = 0.4. كل ما خُزّن سابقًا اختفى، ولم يبقَ إلا ما كُتب للتو. ثم h(t) = 0.6 × tanh(0.4) ≈ 0.6 × 0.380 ≈ 0.228.Resultالنتيجة
With f = 0.9 the cell grows to 2.2 and h(t) ≈ 0.585. With f = 0 the past is erased and C(t) = 0.4. With f = 1 and i = 0, C(t) = C(t-1) = 2 exactly: a perfect copy.مع f = 0.9 تكبر الخلية إلى 2.2 وh(t) ≈ 0.585. مع f = 0 يُمحى الماضي وC(t) = 0.4. مع f = 1 وi = 0 تكون C(t) = C(t-1) = 2 تمامًا: نسخة مطابقة.
Real gate values come out of a sigmoid, so they approach 0 and 1 but never reach them exactly. The presets use the limits to make the behavior obvious. Try holding f at 1 and i at 0, then move the output gate: h(t) changes, C(t) does not.
قيم البوابات الحقيقية تخرج من sigmoid، لذلك تقترب من 0 و1 لكنها لا تصل إليهما تمامًا. الإعدادات الجاهزة تستخدم الحدود لتجعل السلوك واضحًا. جرّب تثبيت f عند 1 وi عند 0، ثم حرّك بوابة الإخراج: تتغير h(t) ولا تتغير C(t).
Why the additive update helpsلماذا يساعد التحديث الجمعي
In a simple RNN, h(t) = g(U h(t-1) + W x(t)): the old state goes through a weight matrix and a squashing function at every step, and backpropagation multiplies by those factors again and again on the way back, so the gradient tends to shrink toward zero. In the LSTM, the direct path from C(t-1) to C(t) is just “multiply by f(t), then add something.” There is no weight matrix and no squashing on that path, so its local derivative is simply f(t). When the forget gate stays near 1, information flows forward and gradient flows backward along the cell line with little loss. This is often called a gradient highway. It eases the vanishing gradient problem; it does not abolish it, because a forget gate below 1 still shrinks the signal over very long spans.
في RNN البسيطة، h(t) = g(U h(t-1) + W x(t)): تمر الحالة القديمة عبر مصفوفة أوزان ودالة ضاغطة في كل خطوة، والانتشار العكسي (backpropagation) يضرب في هذه العوامل مرة بعد مرة في طريق العودة، فيميل التدرج إلى الانكماش نحو الصفر. أما في LSTM فالمسار المباشر من C(t-1) إلى C(t) هو ببساطة “اضرب في f(t) ثم أضف شيئًا”. لا توجد على هذا المسار مصفوفة أوزان ولا دالة ضاغطة، لذلك مشتقته المحلية هي f(t) فقط. عندما تبقى بوابة النسيان قريبة من 1، تتدفق المعلومات إلى الأمام والتدرج إلى الخلف على خط الخلية بخسارة قليلة. يُسمى هذا غالبًا الطريق السريع للتدرج (gradient highway). إنه يخفف مشكلة تلاشي التدرج ولا يلغيها، لأن بوابة نسيان أقل من 1 ما زالت تقلّص الإشارة عبر مسافات طويلة جدًا.
Recall
Why does information survive many steps on the LSTM cell line but fade in a simple RNN’s hidden state?لماذا تبقى المعلومات خطوات كثيرة على خط الخلية في LSTM بينما تتلاشى في الحالة المخفية لـ RNN البسيطة؟
Recall
Without looking, write the six LSTM equations and name the activation used in each.دون النظر، اكتب معادلات LSTM الست وسمِّ دالة التنشيط المستخدمة في كل منها.
Quick checkاختبار سريع
An LSTM step has forget gate near 1 and input gate near 0. What happens to the cell state?في خطوة LSTM، بوابة النسيان قريبة من 1 وبوابة الإدخال قريبة من 0. ماذا يحدث لحالة الخلية؟
Quick checkاختبار سريع
On slide 7, what does the dot in f(t) · C(t-1) denote?في الشريحة 7، علامَ تدل النقطة في f(t) · C(t-1)؟
Slide 8 names three ideas with one bullet each. They answer three different questions: can the gating be simpler, can a position see words to its right, and can the network be deeper?
تذكر الشريحة 8 ثلاث أفكار بنقطة واحدة لكل منها. وهي تجيب عن ثلاثة أسئلة مختلفة: هل يمكن أن تكون البوابات أبسط؟ هل يمكن لموضع ما أن يرى الكلمات التي على يمينه؟ وهل يمكن أن تكون الشبكة أعمق؟
The GRU: two gates, no separate cellGRU: بوابتان ولا خلية منفصلة
The Gated recurrent unit (GRU) merges the LSTM’s cell state and hidden state into one vector h(t) and uses two gates instead of three. The reset gate r(t) decides how much of the past to use when proposing new content. The update gate z(t) decides how to blend old and new, playing the role of the LSTM’s forget and input gates together:
تدمج GRU حالة الخلية والحالة المخفية في LSTM في متجه واحد h(t)، وتستخدم بوابتين بدلًا من ثلاث. بوابة إعادة الضبط (reset gate) r(t) تقرر كم يُستخدم من الماضي عند اقتراح المحتوى الجديد. بوابة التحديث (update gate) z(t) تقرر كيف يُمزج القديم بالجديد، فتؤدي دور بوابتي النسيان والإدخال في LSTM معًا:
With z(t) near 0 the old state is copied, the same carry-forward behavior as an LSTM with its forget gate open and input gate closed. With fewer parameters per unit, a GRU is a little cheaper and often performs about as well; neither one wins on every task.
عندما تكون z(t) قريبة من 0 تُنسخ الحالة القديمة، وهو سلوك الحمل إلى الأمام نفسه في LSTM عندما تكون بوابة النسيان مفتوحة وبوابة الإدخال مغلقة. ولأن عدد معاملات كل وحدة أقل، فإن GRU أرخص قليلًا وكثيرًا ما تؤدي بمستوى مقارب؛ ولا تتفوق أي منهما في كل المهام.
Bidirectional RNNs: read both waysشبكات RNN ثنائية الاتجاه: القراءة في الاتجاهين
Consider tagging bank in “I sat by the bank of the river.” A left-to-right RNN at bank has seen only “I sat by the,” but the deciding word, river, comes later. A Bidirectional RNN runs two independent RNNs over the sentence: a forward one from the first word to the last and a backward one from the last word to the first. At each position it concatenates the two states, so the representation of bank carries information from both sides. The top figure on slide 8 shows exactly that: RNN 1 forward, RNN 2 backward, outputs concatenated.
فكّر في وسم كلمة bank في “I sat by the bank of the river.” (جلست عند ضفة النهر). شبكة RNN تقرأ من اليسار إلى اليمين لم ترَ عند bank إلا “I sat by the”، لكن الكلمة الحاسمة، river، تأتي لاحقًا. شبكة bidirectional RNN (ثنائية الاتجاه) تشغّل شبكتي RNN مستقلتين على الجملة: أمامية من الكلمة الأولى إلى الأخيرة، وخلفية من الكلمة الأخيرة إلى الأولى. وعند كل موضع تضم الحالتين، فيحمل تمثيل bank معلومات من الجانبين. الشكل العلوي في الشريحة 8 يبيّن ذلك تمامًا: RNN 1 أمامية، وRNN 2 خلفية، والمخرجات مضمومة.
The price is that the backward RNN cannot start until the last word exists. That is fine for tagging, classification and encoding a sentence that is already complete. It is impossible for generating text left to right, where the future words are exactly what you are trying to produce.
الثمن أن الشبكة الخلفية لا تستطيع البدء حتى توجد الكلمة الأخيرة. هذا مقبول في الوسم والتصنيف وترميز جملة مكتملة أصلًا. لكنه مستحيل عند توليد النص من اليسار إلى اليمين، حيث الكلمات المستقبلية هي بالضبط ما تحاول إنتاجه.
Stacked RNNs: depth over timeشبكات RNN المكدسة: عمق فوق الزمن
A Stacked (multi-layer) RNN places RNN layers on top of each other. Layer 1 reads the word embeddings x(1) … x(n) and produces a sequence of hidden states; that entire output sequence becomes the input sequence of layer 2, and so on. The final output is taken from the top layer. As in deep feedforward networks, lower layers tend to capture local, surface patterns and higher layers more abstract ones. The bottom figure on slide 8 shows three stacked layers. More layers cost more computation and are harder to train, so the right depth is found by experiment.
شبكة stacked RNN (المكدسة) تضع طبقات RNN بعضها فوق بعض. الطبقة 1 تقرأ تضمينات الكلمات (word embeddings) x(1) … x(n) وتنتج تسلسلًا من الحالات المخفية؛ ويصبح تسلسل المخرجات هذا بأكمله تسلسل مدخلات الطبقة 2، وهكذا. ويؤخذ المخرج النهائي من الطبقة العليا. وكما في الشبكات الأمامية العميقة، تميل الطبقات السفلى إلى التقاط أنماط محلية سطحية، والطبقات العليا إلى أنماط أكثر تجريدًا. الشكل السفلي في الشريحة 8 يبيّن ثلاث طبقات مكدسة. الطبقات الإضافية تكلّف حسابًا أكثر وتجعل التدريب أصعب، لذلك يُحدَّد العمق المناسب بالتجربة.
| Variantالشكل | What changesما الذي يتغير | What it buysما الذي يكسبه | Can generate left to right?هل يستطيع التوليد من اليسار إلى اليمين؟ |
|---|---|---|---|
| GRUGRU | Two gates (update z, reset r), no separate cell stateبوابتان (التحديث z وإعادة الضبط r)، ولا توجد حالة خلية منفصلة | Cheaper drop-in replacement for an LSTM unitبديل أرخص يحل مباشرة محل وحدة LSTM | Yesنعم |
| Bidirectionalثنائية الاتجاه | A forward RNN and a backward RNN; outputs concatenated per positionشبكة RNN أمامية وشبكة RNN خلفية، وتُضم مخرجاتهما عند كل موضع | Every position sees left and right contextكل موضع يرى السياق من اليسار ومن اليمين | No, the backward pass needs future wordsلا، فالمرور الخلفي يحتاج الكلمات المستقبلية |
| Stackedمكدسة | Layer k’s output sequence is layer k + 1’s input sequenceتسلسل مخرجات الطبقة k هو تسلسل مدخلات الطبقة k + 1 | Higher layers build more abstract representationsالطبقات العليا تبني تمثيلات أكثر تجريدًا | Yes, if every layer is left to rightنعم، إذا كانت كل طبقة من اليسار إلى اليمين |
These ideas combine. LSTM or GRU units can be placed into any of these layouts, which is why slide 11 can speak of a stacked bidirectional LSTM, a “stacked BLSTM.”
هذه الأفكار تتجمع معًا. يمكن وضع وحدات LSTM أو GRU في أي من هذه التركيبات، ولهذا تتحدث الشريحة 11 عن LSTM مكدسة ثنائية الاتجاه، أي “stacked BLSTM”.
Recall
What is the difference between a bidirectional RNN and a two-layer stacked RNN? Both have two RNNs.ما الفرق بين RNN ثنائية الاتجاه وRNN مكدسة من طبقتين؟ كلتاهما فيها شبكتا RNN.
Quick checkاختبار سريع
Why can a bidirectional RNN not serve as a left-to-right generator?لماذا لا تصلح RNN ثنائية الاتجاه لتكون مولّدًا من اليسار إلى اليمين؟
Slide 9 translates “Zaid went to his house” into Arabic: ذهب زيد إلى بيته (dhahab zayd ’iilaa baytihi), glossed word by word as “went Zaid to his-house.” The verb moves to the front, because Arabic often uses verb, subject, object order. The two words his house become one word, بيته, so five English words come out as four Arabic words.
تترجم الشريحة 9 الجملة “Zaid went to his house” إلى العربية: ذهب زيد إلى بيته (dhahab zayd ’iilaa baytihi)، ومقابلها كلمة بكلمة بالإنجليزية “went Zaid to his-house.” ينتقل الفعل إلى البداية، لأن العربية تستخدم كثيرًا ترتيب الفعل ثم الفاعل ثم المفعول. والكلمتان his house تصبحان كلمة واحدة، بيته، فتخرج خمس كلمات إنجليزية على شكل أربع كلمات عربية.
Try to do this with a tagger that emits one output per input as it reads. At the first English word, Zaid, the tagger would have to emit the first Arabic word, went, which it has not seen yet. The output at each position depends on the whole input, not on the word at the same position, and the output length need not match the input length. That is what slide 9 means by “the output sequence is a complex function of the entire input sequence.”
حاول أن تفعل ذلك بأداة وسم (tagger) تُخرج مخرجًا واحدًا لكل مدخل أثناء القراءة. عند أول كلمة إنجليزية، Zaid، سيتعين على الأداة إخراج أول كلمة عربية، “ذهب” (went)، وهي لم ترها بعد. المخرج عند كل موضع يعتمد على المدخل كله، لا على الكلمة في الموضع نفسه، ولا يلزم أن يساوي طول المخرج طول المدخل. هذا ما تقصده الشريحة 9 بقولها “تسلسل المخرجات دالة معقدة لتسلسل المدخلات بأكمله”.
Two networks with one job eachشبكتان، لكل منهما مهمة واحدة
The Encoder-decoder (seq2seq) architecture, also called sequence to sequence, splits the work. An encoder reads the entire input x(1) … x(n) and builds a contextualized representation of it, which the slide calls the context. A decoder receives that context and generates the output y(1) … y(m) one token at a time, with m free to differ from n. Slide 10’s figure is that idea and nothing more: encoder, context, decoder. It does not yet say what the encoder and decoder are made of; LSTMs, CNNs and transformers can all fill those boxes.
معمارية encoder-decoder، وتسمى أيضًا sequence to sequence، تقسم العمل. المُرمِّز (encoder) يقرأ المدخل كاملًا x(1) … x(n) ويبني له تمثيلًا سياقيًا، تسميه الشريحة context (السياق). والمُفكِّك (decoder) يستقبل هذا السياق ويولّد المخرج y(1) … y(m) رمزًا (token) بعد رمز، مع حرية أن يختلف m عن n. شكل الشريحة 10 هو هذه الفكرة لا أكثر: مُرمِّز، سياق، مُفكِّك. ولا يحدد بعد مما يُصنع المُرمِّز والمُفكِّك؛ فـLSTM وCNN وtransformers كلها يمكن أن تملأ هذين الصندوقين.
Recall
Give two reasons, using slide 9’s example, why a one-output-per-input tagger cannot translate English to Arabic.باستخدام مثال الشريحة 9، اذكر سببين يجعلان أداة وسم تُخرج مخرجًا واحدًا لكل مدخل عاجزة عن الترجمة من الإنجليزية إلى العربية.
Slide 11 is figure 14.17 from the textbook. It translates the English “the green witch arrived” into the Spanish “llegó la bruja verde,” glossed “arrived the witch green”: the verb moves to the front and the adjective moves after the noun, the same kind of reordering as the Arabic example.
الشريحة 11 هي الشكل 14.17 من الكتاب. يترجم الجملة الإنجليزية “the green witch arrived” (وصلت الساحرة الخضراء) إلى الإسبانية “llegó la bruja verde,” ومقابلها كلمة بكلمة “arrived the witch green”: ينتقل الفعل إلى البداية وتنتقل الصفة إلى ما بعد الاسم، وهو النوع نفسه من إعادة الترتيب في المثال العربي.
Walking through the figureالمرور على الشكل خطوة خطوة
- Encode. The source words the, green, witch, arrived are embedded and fed through the RNN one by one. The figure notes that the output of the source is ignored: no softmax prediction is used while reading the English.الترميز. كلمات المصدر the, green, witch, arrived تُحوَّل إلى تضمينات وتُمرَّر عبر RNN واحدة تلو الأخرى. يوضح الشكل أن مخرجات المصدر تُتجاهل: لا يُستخدم أي تنبؤ softmax أثناء قراءة الإنجليزية.
- Hand over the context. The final hidden state, h(n) (the green box), becomes the context c, and it initializes the decoder: h(0) of the decoder = c. In the textbook’s fuller version, c is also fed to every decoder step so its influence does not fade.تسليم السياق. الحالة المخفية الأخيرة h(n) (الصندوق الأخضر) تصبح السياق c، وتُستخدم لتهيئة المُفكِّك: h(0) of the decoder = c. وفي النسخة الأكمل في الكتاب، تُمرَّر c أيضًا إلى كل خطوة من خطوات المُفكِّك حتى لا يتلاشى أثرها.
- Start decoding. The separator <s> is the first decoder input. The decoder computes a softmax over the Spanish vocabulary and produces llegó.بدء فك الترميز. الفاصل <s> هو أول مدخل للمُفكِّك. يحسب المُفكِّك softmax على مفردات الإسبانية ويُنتج llegó.
- Feed back. At inference time, each generated word is the next input, which is Autoregressive generation: llegó produces la, la produces bruja, and so on. During training, the correct previous word is fed instead, which is Teacher forcing.التغذية الراجعة. وقت الاستدلال (inference) تكون كل كلمة مولَّدة هي المدخل التالي، وهذا هو autoregressive generation (التوليد الانحداري الذاتي): llegó تُنتج la، وla تُنتج bruja، وهكذا. أما أثناء التدريب فتُمرَّر الكلمة السابقة الصحيحة بدلًا من ذلك، وهذا هو teacher forcing.
- Stop. Decoding ends when the model emits </s>.التوقف. ينتهي فك الترميز عندما يُخرج النموذج </s>.
Each target word is predicted from the target words already produced and the source, which reaches the decoder only through c. In other words, the decoder is the RNN language model of part 03 with its starting state replaced by a summary of the source sentence.
كل كلمة هدف يُتنبأ بها من كلمات الهدف التي أُنتجت سابقًا ومن المصدر، والمصدر لا يصل إلى المُفكِّك إلا عبر c. بعبارة أخرى، المُفكِّك هو نموذج اللغة المبني على RNN من الجزء 03، مع استبدال حالته الابتدائية بملخص لجملة المصدر.
What real encoders look likeكيف تبدو المُرمِّزات الحقيقية
The slide’s two bullets describe practice, not the drawing. Stacked architectures are the norm, and the states of the top layer are taken as the encoder’s output. A widely used encoder is a stacked bidirectional LSTM, and at each position the forward and backward top-layer states are concatenated. This is the same combination of the three slide-8 ideas: LSTM units, bidirectional reading (allowed here, because the source sentence is complete before encoding starts), and stacking. The decoder, which generates, stays left to right.
النقطتان في الشريحة تصفان الممارسة الفعلية، لا الرسم. المعماريات المكدسة هي القاعدة، وتؤخذ حالات الطبقة العليا على أنها مخرج المُرمِّز. ومن المُرمِّزات الواسعة الاستخدام LSTM مكدسة ثنائية الاتجاه، وعند كل موضع تُضم الحالتان الأمامية والخلفية للطبقة العليا. هذا هو الجمع نفسه بين أفكار الشريحة 8 الثلاث: وحدات LSTM، والقراءة ثنائية الاتجاه (مسموحة هنا لأن جملة المصدر مكتملة قبل بدء الترميز)، والتكديس. أما المُفكِّك، الذي يولّد، فيبقى من اليسار إلى اليمين.
The bottleneckعنق الزجاجة
Everything the decoder knows about the source sentence must pass through one fixed-size vector, c = h(n). For four words that is manageable. For a forty-word sentence, the same vector must hold every name, number and clause, and the first words have been through the most updates before the encoder finishes, so they tend to be represented least well. Making the vector larger helps only a little. This is the bottleneck problem, and its solution, letting the decoder look at all encoder states with weights that change at every output step, is attention, which part 05 introduces.
كل ما يعرفه المُفكِّك عن جملة المصدر يجب أن يمر عبر متجه واحد ثابت الحجم، c = h(n). مع أربع كلمات يمكن التعامل مع ذلك. أما مع جملة من أربعين كلمة، فيجب أن يحمل المتجه نفسه كل اسم ورقم وعبارة، والكلمات الأولى تكون قد مرت بأكبر عدد من التحديثات قبل أن ينتهي المُرمِّز، لذلك تميل إلى أن تكون الأسوأ تمثيلًا. وتكبير المتجه لا يساعد إلا قليلًا. هذه هي مشكلة عنق الزجاجة (bottleneck)، وحلها هو السماح للمُفكِّك بالنظر إلى كل حالات المُرمِّز بأوزان تتغير عند كل خطوة إخراج، وهذا هو الانتباه (attention) الذي يقدمه الجزء 05.
Recall
In slide 11, which encoder outputs does the decoder use, and what happens at <s> and </s>?في الشريحة 11، أي مخرجات المُرمِّز يستخدمها المُفكِّك، وماذا يحدث عند <s> و</s>؟
Quick checkاختبار سريع
In the basic RNN encoder-decoder on slide 11, what does the decoder receive from the source?في نموذج RNN encoder-decoder الأساسي في الشريحة 11، ماذا يستقبل المُفكِّك من المصدر؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Every LSTM gate is σ(W[h(t-1), x(t)] + b): a learned layer on the concatenated previous hidden state and current input, squashed into the range 0 to 1.كل بوابة في LSTM هي σ(W[h(t-1), x(t)] + b): طبقة متعلَّمة تعمل على ضمّ (concatenation) الحالة المخفية السابقة مع المدخل الحالي، ثم تُضغط قيمتها إلى المدى من 0 إلى 1.
- C(t) = f(t) · C(t-1) + i(t) · C̃(t) and h(t) = o(t) · tanh(C(t)), where · is the elementwise (Hadamard) product.C(t) = f(t) · C(t-1) + i(t) · C̃(t) و h(t) = o(t) · tanh(C(t))، حيث · هو الضرب عنصرًا بعنصر (elementwise أو Hadamard).
- Forget gate near 1 keeps the old memory, near 0 erases it. Input gate controls how much of the candidate is written. Output gate controls how much of the memory is exposed as h(t).بوابة النسيان القريبة من 1 تحتفظ بالذاكرة القديمة، والقريبة من 0 تمحوها. بوابة الإدخال تتحكم في مقدار ما يُكتب من المرشح (candidate). بوابة الإخراج تتحكم في مقدار ما يُكشف من الذاكرة على شكل h(t).
- The cell update is additive, so the direct path from C(t-1) to C(t) is scaled only by f(t). That path is the gradient highway that eases vanishing gradients.تحديث الخلية جمعي، لذلك فإن المسار المباشر من C(t-1) إلى C(t) لا يُضرب إلا في f(t). هذا المسار هو الطريق السريع للتدرج (gradient highway) الذي يخفف مشكلة تلاشي التدرج.
- GRU (beyond the slides): two gates, update z and reset r, and no separate cell state.GRU (خارج الشرائح): بوابتان، بوابة التحديث z وبوابة إعادة الضبط r، ولا توجد حالة خلية منفصلة.
- Bidirectional RNN: forward and backward RNNs, outputs concatenated at each position. It needs the whole sentence, so it cannot generate left to right.RNN ثنائية الاتجاه: شبكة أمامية وشبكة خلفية، وتُضم مخرجاتهما عند كل موضع. تحتاج الجملة كاملة، لذلك لا تستطيع التوليد من اليسار إلى اليمين.
- Stacked RNN: the output sequence of layer k is the input sequence of layer k + 1.RNN المكدسة: تسلسل مخرجات الطبقة k هو تسلسل مدخلات الطبقة k + 1.
- Encoder-decoder: the encoder reads the source, its final hidden state h(n) is the context c, the decoder starts from c, begins at <s> and stops at </s>. One fixed vector for the whole source is the bottleneck that attention removes.Encoder-decoder: المُرمِّز (encoder) يقرأ المصدر، وحالته المخفية الأخيرة h(n) هي السياق c، والمُفكِّك (decoder) ينطلق من c، ويبدأ عند <s> ويتوقف عند </s>. المتجه الثابت الواحد لكامل المصدر هو عنق الزجاجة الذي يزيله الانتباه (attention).
Sourcesالمصادر
- Speech and Language Processing, 3rd ed. draft, Chapter 14: RNNs and LSTMsBookJurafsky and Martin, StanfordSections 14.4 (stacked and bidirectional RNNs), 14.5 (the LSTM, Hadamard product), 14.7 (encoder-decoder, Fig. 14.17, c = h(n) = h(0) of the decoder) and 14.8 (the bottleneck).الأقسام 14.4 (شبكات RNN المكدسة وثنائية الاتجاه)، و14.5 (LSTM وضرب Hadamard)، و14.7 (encoder-decoder، الشكل 14.17، c = h(n) = h(0) للمُفكِّك)، و14.8 (عنق الزجاجة).(opens in a new tab)(يفتح في تبويب جديد)
- Understanding LSTM NetworksArticleChristopher OlahSource of the diagram on slide 6; the cell state as a conveyor belt, gates as a sigmoid layer plus pointwise multiplication.مصدر المخطط في الشريحة 6؛ حالة الخلية كسير ناقل، والبوابات طبقة sigmoid يتبعها ضرب نقطة بنقطة.(opens in a new tab)(يفتح في تبويب جديد)
- Dive into Deep Learning, Section 10.1: Long Short-Term MemoryBookZhang, Lipton, Li and SmolaForget gate 1 and input gate 0 keep the memory cell constant; why this eases vanishing gradients.بوابة نسيان 1 وبوابة إدخال 0 تُبقيان خلية الذاكرة ثابتة؛ ولماذا يخفف هذا مشكلة تلاشي التدرج.(opens in a new tab)(يفتح في تبويب جديد)
- Dive into Deep Learning, Section 10.2: Gated Recurrent UnitsBookZhang, Lipton, Li and SmolaReset and update gate equations, no separate cell state. Uses the opposite z convention to this page.معادلات بوابتي إعادة الضبط والتحديث، دون حالة خلية منفصلة. يستخدم اصطلاح z المعاكس لاصطلاح هذه الصفحة.(opens in a new tab)(يفتح في تبويب جديد)
- Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine TranslationPaperCho et al., EMNLP 2014The RNN encoder-decoder: one RNN encodes a sequence into a fixed-length vector, another decodes it.نموذج RNN encoder-decoder: شبكة RNN ترمّز التسلسل في متجه ثابت الطول، وأخرى تفك ترميزه.(opens in a new tab)(يفتح في تبويب جديد)
- Sequence to Sequence Learning with Neural NetworksPaperSutskever, Vinyals and Le, NIPS 2014Multilayer LSTM encoder to a fixed-dimensional vector; reversing the source sentence helped.مُرمِّز LSTM متعدد الطبقات يحوّل المدخل إلى متجه ثابت الأبعاد؛ وعكس جملة المصدر حسّن النتائج.(opens in a new tab)(يفتح في تبويب جديد)