Majid Al-RaimiReference sheetالورقة المرجعية

ICS 582Lecture 06المحاضرة 06Referenceالمرجع

Reference sheetالورقة المرجعية

Neural Language Models compressed onto one page: the definitions, formulas and numbers to have in your head before a quiz or exam.محاضرة «نماذج اللغة العصبية» مضغوطة في صفحة واحدة: التعريفات والمعادلات والأرقام التي يجب أن تكون حاضرة في ذهنك قبل الاختبار القصير أو الامتحان.

Sequence shapes and the simple RNNأشكال التسلسل وRNN البسيطة

Match the task to its input and output shape first. Part 01: Sequence shapes and RNNs

طابِق المهمة مع شكل مدخلها ومخرجها أولًا. الجزء 01: أشكال التسلسل وشبكات RNN

ShapeالشكلInput and outputالمدخل والمخرجExamplesأمثلةRememberتذكّر
One to oneواحد إلى واحد1 input, 1 outputمدخل واحد، ومخرج واحدImage classificationتصنيف الصورNo sequence, the feedforward baselineلا تسلسل، وهي حالة الشبكة الأمامية الأساسية
One to manyواحد إلى متعدد1 input, a sequence outمدخل واحد، وتسلسل في المخرجImage captioningتوليد وصف للصورInput seeds the state, outputs feed forwardالمدخل يهيّئ الحالة، والمخرجات تُغذّى إلى الأمام
Many to oneمتعدد إلى واحدA sequence in, 1 outputتسلسل في المدخل، ومخرج واحدSentiment, spam detectionتحليل المشاعر، كشف الرسائل المزعجةOnly the final hidden state is readلا تُقرأ إلا الحالة المخفية الأخيرة
Many to many, delayedمتعدد إلى متعدد، مؤجَّلRead all, then emitاقرأ الكل، ثم أخرِجMachine translation, summarizationالترجمة الآلية، التلخيصLengths and order may differ: encoder-decoderقد يختلف الطول والترتيب: encoder-decoder
Many to many, synchronizedمتعدد إلى متعدد، متزامنOne output per tokenمخرج واحد لكل tokenPOS tagging, NERوسم أقسام الكلام (POS)، NERSame length, same orderالطول نفسه، والترتيب نفسه
Five shapesخمسة أشكال
ht=tanh⁡(Whh ht−1+Wxh xt+b)h_t = \tanh\left(W_{hh}\,h_{t-1} + W_{xh}\,x_t + b\right)
Recurrenceالعلاقة التكرارية
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
∂ht∂ht−1=whh (1−ht2)\frac{\partial h_t}{\partial h_{t-1}} = w_{hh}\,\bigl(1 - h_t^{2}\bigr)
Scalar local factor; the product of these shrinks over many stepsالعامل المحلي العددي؛ وحاصل ضربها يتقلص عبر خطوات كثيرة

Scalar RNN worked numbersأرقام مثال RNN العددية

Setupالإعداد
w_hh = 0.5, input pulse x = [1, 0, 0, 0]w_hh = 0.5، ونبضة مدخل x = [1, 0, 0, 0]
Hidden statesالحالات المخفية
h = 0.762, 0.363, 0.180, 0.090h = 0.762, 0.363, 0.180, 0.090
Local factorsالعوامل المحلية
0.434, 0.484, 0.4960.434, 0.484, 0.496
Gradient back to h_1التدرج العائد إلى h_1
∂h_4/∂h_1 ≈ 0.104∂h_4/∂h_1 ≈ 0.104
With w_hh = 1مع w_hh = 1
h = 0.762, 0.642, 0.566, 0.513h = 0.762, 0.642, 0.566, 0.513

LSTM, variants and the encoder-decoderLSTM ومتغيراتها وencoder-decoder

Every gate is σ(W[h_(t-1), x_t] + b), a value between 0 and 1. Part 02: LSTM, variants and encoder-decoder

كل بوابة هي σ(W[h_(t-1), x_t] + b)، أي قيمة بين 0 و1. الجزء 02: LSTM ومتغيراتها وencoder-decoder

ft,it,ot=σ(Wf,i,o[ht−1,xt]+bf,i,o),C~t=tanh⁡(WC[ht−1,xt]+bC)f_t, i_t, o_t = \sigma\left(W_{f,i,o}[h_{t-1}, x_t] + b_{f,i,o}\right),\qquad \tilde{C}_t = \tanh\left(W_C[h_{t-1}, x_t] + b_C\right)
Forget, input and output gates, and the candidateبوابات النسيان والإدخال والإخراج، والمرشح
Ct=ft⊙Ct−1+it⊙C~t,ht=ot⊙tanh⁡(Ct)C_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t,\qquad h_t = o_t \odot \tanh(C_t)
Additive cell update and gated hidden stateتحديث جمعي للخلية وحالة مخفية مبوَّبة

Reading the gatesقراءة البوابات

Forget gate near 1, input gate near 0بوابة النسيان قرب 1، وبوابة الإدخال قرب 0
Cell keeps its old value, C_t ≈ C_(t-1)تحتفظ الخلية بقيمتها القديمة، C_t ≈ C_(t-1)
Forget gate near 0بوابة النسيان قرب 0
Old memory erased; the cell holds only what the input gate writesتُمحى الذاكرة القديمة، ولا تحمل الخلية إلا ما تكتبه بوابة الإدخال
Output gate near 0بوابة الإخراج قرب 0
Memory kept but hidden, h_t ≈ 0الذاكرة محفوظة لكنها مخفية، h_t ≈ 0
Gradient highwayالطريق السريع للتدرج
∂C_t / ∂C_(t-1) = f_t along the direct path, no squashing weight matrix∂C_t / ∂C_(t-1) = f_t على المسار المباشر، دون مصفوفة أوزان تضغط القيم
zt=σ(Wz[ht−1,xt]),rt=σ(Wr[ht−1,xt]),ht=(1−zt)⊙ht−1+zt⊙tanh⁡(W[rt⊙ht−1,xt])z_t = \sigma(W_z[h_{t-1}, x_t]),\quad r_t = \sigma(W_r[h_{t-1}, x_t]),\quad h_t = (1 - z_t) \odot h_{t-1} + z_t \odot \tanh\left(W[r_t \odot h_{t-1}, x_t]\right)
GRU, beyond the slidesGRU، خارج نطاق الشرائح
VariantالمتغيرWhat changesما الذي يتغيرWhat it buysما الذي يكسبهGenerates left to right?هل يولّد من اليسار إلى اليمين؟
GRUGRUUpdate z and reset r, no separate cell stateبوابة تحديث z وبوابة إعادة ضبط r، دون حالة خلية منفصلةCheaper LSTM replacementبديل أرخص لـ LSTMYesنعم
Bidirectionalثنائية الاتجاهForward and backward RNNs, outputs concatenatedشبكتا RNN أمامية وخلفية، ومخرجاتهما موصولةLeft and right context everywhereسياق من اليسار ومن اليمين في كل موضعNo, needs future wordsلا، تحتاج إلى الكلمات المستقبلية
StackedمكدّسةLayer k output is layer k + 1 inputمخرج الطبقة k هو مدخل الطبقة k + 1More abstract representationsتمثيلات أكثر تجريدًاYes, if every layer is left to rightنعم، إذا كانت كل طبقة من اليسار إلى اليمين
RNN variantsمتغيرات RNN
p(y∣x)=∏t=1mp(yt∣y1,…,yt−1,x)p(y \mid x) = \prod_{t=1}^{m} p(y_t \mid y_1, \ldots, y_{t-1}, x)
The decoder is a language model conditioned on the sourceالمفكِّك (decoder) نموذج لغة مشروط بالمصدر
  • The encoder reads the source; its final state h_n is the context c.يقرأ الـ encoder النص المصدر، وحالته الأخيرة h_n هي السياق c.
  • The decoder starts from c, begins at <s> and stops at </s>.يبدأ الـ decoder من c، ويبدأ التوليد عند <s> ويتوقف عند </s>.
  • One fixed vector for the whole source is the bottleneck that attention removes.متجه واحد ثابت للنص المصدر كله هو عنق الزجاجة الذي يزيله الانتباه.

RNN language modelsنماذج اللغة المبنية على RNN

Part 03: RNN language models

الجزء 03: نماذج اللغة المبنية على RNN

n-gramn-gramNeuralعصبي
Assumptionالافتراض(n-1)-word Markov windowنافذة ماركوف من (n-1) كلمةWhole prefix summarized in a hidden stateالبادئة كاملة ملخّصة في حالة مخفية
ProbabilitiesالاحتمالاتCounting, then smoothingالعدّ، ثم التنعيمComputed by a trained networkتحسبها شبكة مدرَّبة
Word representationتمثيل الكلمةAtomic symbol, a table indexرمز ذري، مجرد فهرس في جدولDense learned vectorمتجه كثيف متعلَّم
Unseen n-gramn-gram غير مرئيZero count, only smoothing massعدد صفري، ولا يبقى إلا ما يمنحه التنعيمSensible probability from similar wordsاحتمال معقول مستمد من كلمات مشابهة
Size as context growsالحجم مع زيادة السياقGrows as |V|^nينمو بمقدار |V|^nFixed, the same matrices at every stepثابت، المصفوفات نفسها عند كل خطوة
Count-based versus neuralالنماذج القائمة على العدّ مقابل النماذج العصبية
et=E xtht=g(U ht−1+W et)y^t=softmax⁡(V ht)LCE=−log⁡y^t[wt+1]\begin{aligned} e_t &= E\,x_t \\ h_t &= g\left(U\,h_{t-1} + W\,e_t\right) \\ \hat{y}_t &= \operatorname{softmax}\left(V\,h_t\right) \\ L_{CE} &= -\log \hat{y}_t\left[w_{t+1}\right] \end{aligned}
RNN LM in SLP3 notationنموذج لغة RNN بترميز SLP3

Shapesالأبعاد

x_t, one-hot inputx_t، مدخل one-hot
|V| × 1|V| × 1
E, embeddingE، مصفوفة embedding
d_e × |V|d_e × |V|
W, input to hiddenW، من المدخل إلى المخفي
d_h × d_ed_h × d_e
U, hidden to hiddenU، من المخفي إلى المخفي
d_h × d_hd_h × d_h
V, hidden to outputV، من المخفي إلى المخرج
|V| × d_h|V| × d_h
h_t
d_h × 1
V h_t and ŷ_tV h_t و ŷ_t
|V| × 1|V| × 1
Example countمثال على العدّ
|V| = 10,000, d = 512: 5,120,000 + 524,288 + 5,120,000 = 10,764,288, over 95% in E and V|V| = 10,000، d = 512: 5,120,000 + 524,288 + 5,120,000 = 10,764,288، أكثر من 95% منها في E وV
RoleالدورSlide 14 (SLP3)الشريحة 14 (SLP3)Slide 16 (CS224n)الشريحة 16 (CS224n)
Embedding lookupالبحث في embeddingEEEE
Input to hiddenمن المدخل إلى المخفيWWW_eW_e
Hidden to hiddenمن المخفي إلى المخفيUUW_hhW_hh
Hidden to outputمن المخفي إلى المخرجVVUU
Nonlinearityالدالة اللاخطيةggσσ
Time indexفهرس الزمنh_th_th^(t)h^(t)
Slide 14 versus slide 16 namesأسماء الشريحة 14 مقابل الشريحة 16
AdvantagesالمزاياRemaining problemsالمشكلات المتبقية
Any input lengthأي طول للمدخلRecurrent computation is slow: h_t waits for the previous stateالحساب التكراري بطيء: h_t تنتظر الحالة السابقة
Model size does not grow with lengthحجم النموذج لا يكبر مع الطولLong-range information is hard in practice: gradients fadeالمعلومات بعيدة المدى صعبة عمليًا: التدرجات تتلاشى
Long history in theoryتاريخ طويل من الناحية النظرية
Weights shared across timeالأوزان مشتركة عبر الزمن
Two lists to memorizeقائمتان للحفظ
RuleالقاعدةHowالطريقةEffectالأثر
Greedyالجشع (Greedy)Take the argmax every stepخذ argmax عند كل خطوةDeterministic, generic and often repetitiveحتمي، وعام، وكثيرًا ما يتكرر
Random samplingأخذ العينات العشوائيDraw from ŷاسحب من ŷVaried, can wander into unlikely wordsمتنوّع، وقد ينحرف إلى كلمات غير مرجّحة
Temperatureدرجة الحرارة (Temperature)Sample from softmax(V h_t / τ)اسحب من softmax(V h_t / τ)τ < 1 sharpens, τ > 1 flattens, τ → 0 is greedyτ < 1 تزيد الحدة، وτ > 1 تسطّح التوزيع، وτ → 0 تساوي الجشع
Generation from <s> to </s>التوليد من <s> إلى </s>

Softmax of [2.0, 1.0, 0.1] is [0.659, 0.242, 0.099]. A two-layer RNN LM feeds h_t^(1) into layer 2; only the top layer feeds the softmax.

ناتج softmax للمتجه [2.0, 1.0, 0.1] هو [0.659, 0.242, 0.099]. نموذج اللغة المبني على RNN بطبقتين يمرر h_t^(1) إلى الطبقة 2، والطبقة العليا وحدها هي التي تغذي softmax.

Cross-entropy and teacher forcingCross-entropy وteacher forcing

Self-supervised: the target at step t is word t + 1 of the same text. Part 04: Training with cross-entropy

تعلّم ذاتي الإشراف (self-supervised): الهدف عند الخطوة t هو الكلمة رقم t + 1 من النص نفسه. الجزء 04: التدريب بـ cross-entropy

LCE=−∑w∈Vyt[w] log⁡y^t[w]=−log⁡y^t[wt+1]L_{CE} = -\sum_{w \in V} y_t[w]\,\log \hat{y}_t[w] = -\log \hat{y}_t[w_{t+1}]
The one-hot target leaves only the true word's termالهدف one-hot لا يُبقي إلا حد الكلمة الصحيحة
L=−1T∑t=1Tlog⁡y^t[wt+1],PP=eLL = -\frac{1}{T}\sum_{t=1}^{T}\log \hat{y}_t[w_{t+1}],\qquad PP = e^{L}
Average loss over a sequence, and perplexity in natsمتوسط الخسارة على تسلسل، والحيرة بوحدة nats
InputالمدخلTargetالهدفŷ[target]ŷ[target]-ln ŷ-ln ŷ
sosolonglong0.60.60.5110.511
longlongandand0.50.50.6930.693
andandthanksthanks0.20.21.6091.609
thanksthanksforfor0.40.40.9160.916
forforallall0.80.80.2230.223
Worked sequence: sum 3.953, average 0.791, perplexity ≈ 2.20تسلسل محلول: المجموع 3.953، والمتوسط 0.791، والحيرة (perplexity) ≈ 2.20
Teacher forcingTeacher forcingFree runningالتشغيل الحر (free running)
Input at step t+1المدخل عند الخطوة t+1Gold word w_(t+1) from the textالكلمة الصحيحة w_(t+1) من النصThe model's own predicted wordالكلمة التي تنبأ بها النموذج نفسه
Used duringيُستخدم أثناءTrainingالتدريبGenerationالتوليد
Effect of one mistakeأثر خطأ واحدContained, next step sees the true historyمحصور، فالخطوة التالية ترى التاريخ الصحيحCompounds through later stepsيتراكم عبر الخطوات اللاحقة
Targets known in advanceالأهداف معروفة مسبقًاYesنعمNoلا
Teacher forcing versus free runningTeacher forcing مقابل التشغيل الحر

From RNN limits to the transformer blockمن قيود RNN إلى كتلة transformer

Part 05: Why attention and the transformer block

الجزء 05: لماذا الانتباه، وكتلة transformer

RNNRNNSelf-attentionSelf-attention
Steps for all N outputs of a layerعدد الخطوات لكل مخرجات الطبقة الـ NN, in sequenceN، بالتتابع1 parallel stepخطوة متوازية واحدة (1)
Hops from token i to token jعدد القفزات من token i إلى token j|i - j||i - j|11
Where long-range information livesأين تعيش المعلومات بعيدة المدىOne hidden state vectorمتجه حالة مخفية واحدRead directly from the other tokenتُقرأ مباشرة من الـ token الآخر
Main costالتكلفة الرئيسيةSequential and forgetfulتسلسلي وكثير النسيانN² pairs per layerN² زوجًا في كل طبقة
RNN versus self-attentionRNN مقابل self-attention
z=LayerNorm⁡(x+SelfAttn⁡(x)),y=LayerNorm⁡(z+FFN⁡(z))z = \operatorname{LayerNorm}\big(x + \operatorname{SelfAttn}(x)\big),\qquad y = \operatorname{LayerNorm}\big(z + \operatorname{FFN}(z)\big)
Slide 26, post-norm: write this on the examالشريحة 26، post-norm: اكتب هذه في الاختبار
FFN⁡(z)=max⁡(0, zW1+b1) W2+b2\operatorname{FFN}(z) = \max(0,\, zW_1 + b_1)\,W_2 + b_2
Per-position feedforwardتغذية أمامية لكل موضع
PieceالقطعةComputesما تحسبهWhyلماذا
SelfAttnSelfAttnEach token reads the othersكل token يقرأ البقيةThe only piece that mixes positionsالقطعة الوحيدة التي تمزج المواضع
+ (residual)+ (الوصلة المتبقية)Adds the input backتعيد إضافة المدخلKeeps the signal, direct gradient pathتحافظ على الإشارة، ومسار مباشر للتدرج
LayerNormLayerNormZero mean, unit variance per token, then γ and βمتوسط صفري وتباين واحد لكل token، ثم γ وβKeeps values in a trainable rangeتبقي القيم في مدى قابل للتدريب
FFNFFNTwo linear layers with ReLU, per positionطبقتان خطيتان مع ReLU، لكل موضع على حدةSame weights at every position, inner size 4dالأوزان نفسها في كل موضع، والحجم الداخلي 4d
The four piecesالقطع الأربع

Block numbersأرقام الكتلة

FFN parameters (d = 512, d_ff = 2048)معاملات FFN (d = 512, d_ff = 2048)
2,099,712 ≈ 2.1 M2,099,712 ≈ 2.1 M
Attention projectionsإسقاطات الانتباه
4 × 512² ≈ 1.05 M4 × 512² ≈ 1.05 M
LayerNorm on [2, 4, 6, 8]LayerNorm على [2, 4, 6, 8]
μ = 5, σ = √5, gives [-1.342, -0.447, 0.447, 1.342]μ = 5، σ = √5، وتعطي [-1.342, -0.447, 0.447, 1.342]

Attention mechanicsآلية الانتباه

Part 06: Attention mechanics

الجزء 06: آلية الانتباه

scoreij=qi⋅kjdk,αij=exp⁡(scoreij)∑kexp⁡(scoreik),yi=∑jαijvj\text{score}_{ij} = \frac{q_i \cdot k_j}{\sqrt{d_k}}, \qquad \alpha_{ij} = \frac{\exp(\text{score}_{ij})}{\sum_{k}\exp(\text{score}_{ik})}, \qquad y_i = \sum_{j} \alpha_{ij} v_j
Per tokenلكل رمز
Attention(Q,K,V)=softmax⁡ ⁣(QK⊤dk)V\text{Attention}(Q, K, V) = \operatorname{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}}\right) V
Matrix formصيغة المصفوفات

Shapesالأبعاد

X
N × d
W^Q, W^K
d × d_k
W^V
d × d_v
Q, K
N × d_k
V
N × d_v
QKᵀ and the weightsQKᵀ والأوزان
N × NN × N
Outputالمخرج
N × d_vN × d_v
MultiHeadAttn(X)=(head1⊕⋯⊕headh) WO,headi=SelfAttention(XWiQ,XWiK,XWiV)\text{MultiHeadAttn}(X) = (\text{head}_1 \oplus \cdots \oplus \text{head}_h)\, W^{O},\qquad \text{head}_i = \text{SelfAttention}(XW_i^Q, XW_i^K, XW_i^V)
Multi-headمتعدد الرؤوس
TermالحدShapeالأبعادMeaningالمعنى
W_i^Q, W_i^K, W_i^VW_i^Q, W_i^K, W_i^V512 × 64512 × 64One set per head, eight setsمجموعة لكل رأس، أي ثماني مجموعات
Q_i, K_i, V_iQ_i, K_i, V_iN × 64N × 64Each head's own small spaceفضاء صغير خاص بكل رأس
Q_i K_iᵀQ_i K_iᵀN × NN × NEach head has its own patternلكل رأس نمطه الخاص
head_1 ⊕ … ⊕ head_8head_1 ⊕ … ⊕ head_8N × 512N × 5128 × 64 = 5128 × 64 = 512
WᴼWᴼ512 × 512512 × 512Mixes heads, returns to dتمزج الرؤوس وتعود إلى d
MultiHeadAttn(X)MultiHeadAttn(X)N × 512N × 512Same as X, so blocks stackمثل X، لذلك تتكدّس الكتل
Shapes with d = 512, h = 8الأبعاد مع d = 512 و h = 8
xi=ewi+pi,PE(pos, 2i)=sin⁡ ⁣(pos100002i/d),PE(pos, 2i+1)=cos⁡ ⁣(pos100002i/d)x_i = e_{w_i} + p_i,\qquad PE_{(pos,\,2i)} = \sin\!\left(\frac{pos}{10000^{2i/d}}\right),\quad PE_{(pos,\,2i+1)} = \cos\!\left(\frac{pos}{10000^{2i/d}}\right)
Positional vector is added, not concatenatedمتجه الموضع يُضاف ولا يُضم
posposPaper formulaصيغة الورقةSlide 29 figureرسم الشريحة 29
00[0, 1, 0, 1][0, 1, 0, 1][0, 0, 1, 1][0, 0, 1, 1]
11[0.841, 0.540, 0.010, 1.000][0.841, 0.540, 0.010, 1.000][0.84, 0.0001, 0.54, 1][0.84, 0.0001, 0.54, 1]
22[0.909, -0.416, 0.020, 1.000][0.909, -0.416, 0.020, 1.000][0.91, 0.0002, -0.42, 1][0.91, 0.0002, -0.42, 1]
Positional vectors, d = 4المتجهات الموضعية، d = 4
LayerالطبقةQueries fromمصدر queriesKeys and values fromمصدر keys و valuesMaskالقناعScoresالدرجات
Encoder self-attentionself-attention في الـ encoderEncoder layer belowطبقة الـ encoder السفلىEncoder layer belowطبقة الـ encoder السفلىNoneلا يوجدn × nn × n
Decoder causal self-attentionself-attention السببي في الـ decoderDecoder layer belowطبقة الـ decoder السفلىDecoder layer belowطبقة الـ decoder السفلىCausal, j > i set to -∞سببي، j > i تُضبط على -∞m × mm × m
Cross-attentionCross-attentionDecoder layer belowطبقة الـ decoder السفلىFinal encoder outputالمخرج النهائي للـ encoderNoneلا يوجدm × nm × n
Three attention layers in an encoder-decoderثلاث طبقات انتباه في encoder-decoder
softmax⁡ ⁣(QK⊤dk+M)V,Mij={0j≤i−∞j>i\operatorname{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}} + M\right) V, \qquad M_{ij} = \begin{cases} 0 & j \le i \\ -\infty & j > i \end{cases}
Causal maskالقناع السببي (causal mask)
k1k2k3k4
q1q1·k1-∞-∞-∞
q2q2·k1q2·k2-∞-∞
q3q3·k1q3·k2q3·k3-∞
q4q4·k1q4·k2q4·k3q4·k4
Causal scores for N = 4, before scalingالدرجات السببية مع N = 4، قبل التحجيم

Transformer language model familiesعائلات نماذج اللغة المبنية على transformer

Same block, different allowed links. Part 07: Transformer LM families

الكتلة نفسها، لكن الروابط المسموح بها مختلفة. الجزء 07: عائلات نماذج اللغة المبنية على transformer

ArchitectureالبنيةAttentionالانتباهObjectiveالهدف التدريبيOutput per positionالمخرج لكل موضعTasksالمهامExamplesأمثلة
Decoder-onlyDecoder-onlyCausal, i sees 1 to iسببي، i يرى من 1 إلى iPredict the next wordالتنبؤ بالكلمة التاليةVocabulary distribution per positionتوزيع على المفردات لكل موضعGeneration, chatالتوليد، المحادثةGPT familyعائلة GPT
Encoder-onlyEncoder-onlyFull, every positionكامل، كل المواضعPredict masked wordsالتنبؤ بالكلمات المقنّعةContextual vector per tokenمتجه سياقي لكل tokenClassification, tagging after fine-tuningالتصنيف والوسم بعد الضبط الدقيق (fine-tuning)BERT, RoBERTaBERT, RoBERTa
Encoder-decoderEncoder-decoderEncoder full, decoder causal plus crossالـ encoder كامل، والـ decoder سببي مع cross-attentionMap input sequence to output sequenceتحويل تسلسل المدخل إلى تسلسل المخرجOutput words, left to rightكلمات المخرج، من اليسار إلى اليمينTranslation, speech recognition, summarizationالترجمة، التعرف على الكلام، التلخيصOriginal transformer, T5, BART, Whispertransformer الأصلي، T5، BART، Whisper
Three architecturesثلاث بنى
  • Decoder-only is also called causal, autoregressive or left-to-right.يُسمّى decoder-only أيضًا سببيًا (causal) أو انحداريًا ذاتيًا (autoregressive) أو من اليسار إلى اليمين (left-to-right).
  • Two different masks: the causal mask hides future positions inside attention; the [MASK] token hides input words in BERT training (15% chosen, split 80/10/10).قناعان مختلفان: القناع السببي يخفي المواضع المستقبلية داخل الانتباه، أما رمز [MASK] فيخفي كلمات المدخل أثناء تدريب BERT (يُختار 15%، وتُقسَّم 80/10/10).
  • Causal attention keeps N(N+1)/2 of the N² scores.الانتباه السببي يحتفظ بـ N(N+1)/2 من الدرجات البالغ عددها N².
P(w1:n)=∏t=1nP(wt∣w1:t−1)P(w_{1:n}) = \prod_{t=1}^{n} P(w_t \mid w_{1:t-1})
What a decoder-only model factorizesما يحلّله نموذج decoder-only إلى عوامل
PositionالموضعVisible contextالسياق المرئيTrue nextالكلمة التالية الصحيحةŷ[w_true]ŷ[w_true]-ln ŷ-ln ŷ
11SoSolonglong0.200.201.6091.609
22So longSo longandand0.500.500.6930.693
33So long andSo long andthanksthanks0.400.400.9160.916
44So long and thanksSo long and thanksforfor0.800.800.2230.223
55So long and thanks forSo long and thanks forallall0.250.251.3861.386
Parallel loss for a 6-word sentence: sum 4.827, average 0.965الخسارة المتوازية لجملة من 6 كلمات: المجموع 4.827، والمتوسط 0.965

Language modeling head and trainingرأس نمذجة اللغة والتدريب

Part 08: LM head and training

الجزء 08: رأس نمذجة اللغة (LM head) والتدريب

u=hNL E⊤,y=softmax⁡(u)u = h_N^L\,E^{\top}, \qquad y = \operatorname{softmax}(u)
SLP3 Eq. 7.47 and 7.48المعادلتان 7.47 و7.48 في SLP3

Shape trace with d = 768, |V| = 50,000تتبّع الأبعاد مع d = 768 و |V| = 50,000

h_N^L, final layer, last tokenh_N^L، الطبقة الأخيرة، آخر token
1 × d (1 × 768)1 × d (1 × 768)
E, embeddingE، مصفوفة embedding
|V| × d (50,000 × 768)|V| × d (50,000 × 768)
U = Eᵀ, unembeddingU = Eᵀ، مصفوفة unembedding
d × |V| (768 × 50,000)d × |V| (768 × 50,000)
u = h_N^L Eᵀ, logitsu = h_N^L Eᵀ، قيم logits
1 × |V|1 × |V|
y = softmax(u)
1 × |V|
Saved by tyingما يوفّره الربط
50,000 × 768 = 38.4 M parameters, about 154 MB at 4 bytes each50,000 × 768 = 38.4 M معامل، أي نحو 154 MB بواقع 4 bytes لكل معامل
PropertyالخاصيةLogits uLogits uProbabilities yالاحتمالات y
RangeالمدىAny real numberأي عدد حقيقي0 to 1من 0 إلى 1
Sum over vocabularyالمجموع على المفرداتAnythingأي قيمةExactly 1يساوي 1 تمامًا
Add a constant to allإضافة ثابت إلى الكلValues changeتتغير القيمUnchanged, softmax is shift invariantلا تتغير، فـ softmax لا يتأثر بالإزاحة
Produced byيُنتجهاUnembedding, linearunembedding، خطيةSoftmax, nonlinearsoftmax، غير خطية
Logits versus probabilitiesLogits مقابل الاحتمالات
LCE=1T∑t=1T−log⁡y^t[wt+1],xi=E[wi]+Epos[i]L_{CE} = \frac{1}{T}\sum_{t=1}^{T} -\log \hat{y}_t\left[w_{t+1}\right],\qquad x_i = E[w_i] + E_{\text{pos}}[i]
Training loss and input encodingخسارة التدريب وترميز المدخل
AspectالجانبTrainingالتدريبGenerationالتوليد
Where the head runsأين يعمل الرأسEvery position t = 1 … Tفي كل موضع t = 1 … TLast position onlyفي الموضع الأخير فقط
What feeds the next positionما الذي يغذي الموضع التاليTrue token (teacher forcing)الـ token الصحيح (teacher forcing)Token sampled from ytoken مسحوب من y
Forward passesالتمريرات الأماميةOne parallel pass for all T lossesتمريرة متوازية واحدة لكل الخسائر الـ TOne per generated tokenتمريرة لكل token مولَّد
Training versus generationالتدريب مقابل التوليد

Slide errataأخطاء الشرائح

Answer with the corrected form. Each erratum is explained in its part: Part 01 (slide 5), Part 02 (7, 11), Part 03 (14, 16), Part 05 (25, 26), Part 06 (27 to 30), Part 07 (33).

أجب بالصيغة المصححة. كل خطأ مشروح في جزئه: الجزء 01 (الشريحة 5)، الجزء 02 (7 و11)، الجزء 03 (14 و16)، الجزء 05 (25 و26)، الجزء 06 (من 27 إلى 30)، الجزء 07 (33).

What the slides get wrongما تخطئ فيه الشرائح

Slide 5الشريحة 5
The diagram reads as o_t = h_t. Write the output layer o_t = W_hy h_t and the bias b.يُقرأ الرسم كأن o_t = h_t. اكتب طبقة المخرج o_t = W_hy h_t والانحياز (bias) b.
Slide 7الشريحة 7
“·” is the elementwise product ⊙, and “(0 → 1)” is the output range of σ.الرمز “·” هو الضرب عنصرًا بعنصر ⊙، و“(0 → 1)” هو مدى مخرج σ.
Slide 11الشريحة 11
Bullets describe a stacked biLSTM encoder, the figure shows one left-to-right layer.النقاط تصف encoder من نوع biLSTM مكدّس، بينما يُظهر الرسم طبقة واحدة من اليسار إلى اليمين.
Slide 14الشريحة 14
The prediction is ŷ_t = softmax(V h_t), with a hat. y_t is the one-hot target.التنبؤ هو ŷ_t = softmax(V h_t)، بعلامة القبعة. أما y_t فهو الهدف بصيغة one-hot.
Slide 16الشريحة 16
Mixes h^(t) and h_(t-1); follow the figure: h^(t) reads w_t. Here U is the output matrix, not the recurrent one.تخلط بين h^(t) وh_(t-1)؛ اتبع الرسم: h^(t) تقرأ w_t. وهنا U هي مصفوفة المخرج، لا المصفوفة التكرارية.
Slide 25الشريحة 25
apfel should be Apfel; German capitalizes nouns.الصواب Apfel لا apfel، لأن الألمانية تكتب الأسماء بحرف كبير في أولها.
Slide 26الشريحة 26
The figure labels only y_n; the block outputs y₁ … y_n, one per position.يضع الرسم تسمية y_n فقط، بينما تُخرج الكتلة y₁ … y_n، مخرجًا لكل موضع.
Slide 27الشريحة 27
The per-token score drops √d_k. Use score_ij = q_i · k_j / √d_k.درجة كل token تُسقط √d_k. استخدم score_ij = q_i · k_j / √d_k.
Slide 28الشريحة 28
Projections need the head index: Q_i = XW_i^Q, head_i = SelfAttention(Q_i, K_i, V_i).الإسقاطات تحتاج إلى فهرس الرأس: Q_i = XW_i^Q، head_i = SelfAttention(Q_i, K_i, V_i).
Slide 29الشريحة 29
The figure uses the Tensor2Tensor layout (all sines, then all cosines), not the paper's interleaved formula.يستخدم الرسم ترتيب Tensor2Tensor (كل قيم الجيب أولًا، ثم كل قيم جيب التمام)، لا الصيغة المتداخلة في الورقة.
Slide 30الشريحة 30
Column form with weights on the left; [i-1] is the previous decoder layer, not the previous token.صيغة الأعمدة مع الأوزان على اليسار؛ و[i-1] هي طبقة decoder السابقة، لا الـ token السابق.
Slide 33الشريحة 33
The encoder-decoder panel omits the decoder's causal self-attention links.لوحة encoder-decoder تُغفل روابط self-attention السببية داخل الـ decoder.