Lecture 04 · Word Embeddings

المحاضرة 04 · تضمينات الكلمات (Word Embeddings)

Training Skip-Gram

تدريب Skip-Gram

Slides 76–88 الشرائح 76–88 P2 SHOULD

How random vectors turn into meaningful ones: building the training set, writing the loss, taking the derivative, and doing one update by hand.

كيف تتحول المتجهات العشوائية إلى متجهات ذات معنى: بناء مجموعة التدريب، وكتابة دالة الخسارة، وأخذ المشتقة، وتنفيذ تحديث واحد باليد.

The previous page ended with a classifier that can score a pair and two matrices, W and C, full of numbers. Nobody has said where those numbers come from. They start out random. This page is the process that fixes them.

انتهت الصفحة السابقة بمصنِّف يستطيع إعطاء درجة لزوج، وبمصفوفتين W وC مليئتين بالأرقام. لكن لم يقل أحد من أين تأتي هذه الأرقام. إنها تبدأ عشوائية، وهذه الصفحة هي العملية التي تصلحها.

Slides 76 to 78: building the training set

الشرائح 76 إلى 78: بناء مجموعة التدريب

Three slides, each adding one piece to the same picture. The sentence is the one from slide 68, with apricot as target and a ±2 window.

ثلاث شرائح، كل واحدة تضيف قطعة إلى الصورة نفسها. الجملة هي جملة الشريحة 68، وapricot هي الهدف، والنافذة ±2.

Step one: the positives

الخطوة الأولى: الأمثلة الموجبة

Every context word in the window becomes a positive example, paired with the target. Four context words give four rows.

كل كلمة سياق في النافذة تصبح مثالاً موجباً مقترناً بالهدف. أربع كلمات سياق تعطي أربعة صفوف.

positive examples +أمثلة موجبة +
tc
apricottablespoon
apricotof
apricotjam
apricota

الأمثلة الموجبة الأربعة: (apricot, tablespoon) و(apricot, of) و(apricot, jam) و(apricot, a). العمود t هو الهدف والعمود c هو كلمة السياق.

Note what is not recorded: how far away the context word was, or which side it was on. tablespoon at distance 2 and jam at distance 1 produce identical rows. Position inside the window is thrown away.

لاحظ ما لا يُسجَّل: كم كانت كلمة السياق بعيدة، وفي أي جانب كانت. tablespoon على بُعد 2 وjam على بُعد 1 تنتجان صفّين متطابقين في الشكل. الموضع داخل النافذة يُرمى.

Step two: the negatives

الخطوة الثانية: الأمثلة السالبة

A classifier trained only on yes-examples would learn to say yes to everything. It needs things to say no to.

المصنِّف الذي يُدرَّب على أمثلة "نعم" فقط سيتعلّم أن يقول نعم لكل شيء. يحتاج إلى أشياء يقول لها لا.

Slide 77 states the rule in one sentence: for each positive example we will grab k negative examples, sampling by frequency.

تذكر الشريحة 77 القاعدة في جملة واحدة: لكل مثال موجب نأخذ k أمثلة سالبة، بسحب يعتمد على التكرار (sampling by frequency).

A negative example keeps the same target and swaps in a random word from the lexicon, called a noise word. The one constraint is that the noise word must not be the target itself.

المثال السالب يُبقي الهدف نفسه ويضع مكان كلمة السياق كلمة عشوائية من المعجم تُسمّى كلمة ضوضاء (noise word). والقيد الوحيد أن كلمة الضوضاء يجب ألّا تكون الهدف نفسه.

Slide 78 shows the full table with k = 2. Four positives times two negatives each gives eight negative rows.

تعرض الشريحة 78 الجدول الكامل مع k = 2. أربعة أمثلة موجبة، لكل منها مثالان سالبان، فيكون المجموع ثمانية صفوف سالبة.

negative examples − (k = 2)أمثلة سالبة − (k = 2)
tctc
apricotaardvarkapricotseven
apricotmyapricotforever
apricotwhereapricotdear
apricotcoaxialapricotif

الأمثلة السالبة الثمانية كلها تقرن apricot بكلمة ضوضاء: aardvark وmy وwhere وcoaxial وseven وforever وdear وif.

Look at the noise words the slide picked: aardvark, my, where, coaxial, seven, forever, dear, if. A mix of rare content words and extremely common function words. That mix is not an accident, and the next section is about why.

انظر إلى كلمات الضوضاء التي اختارتها الشريحة: aardvark وmy وwhere وcoaxial وseven وforever وdear وif. خليط من كلمات محتوى نادرة وكلمات وظيفية شائعة جداً. هذا الخليط ليس صدفة، والقسم التالي يشرح السبب.

Memory hook خطّاف للذاكرة

k is per positive, not per window.

k لكل مثال موجب، لا لكل نافذة.

4 positives and k = 2 gives 8 negatives, not 2. SGNS always has more negative examples than positive ones, which is the opposite of what the word "sampling" makes you expect.

4 أمثلة موجبة مع k = 2 تعطي 8 أمثلة سالبة، لا 2. في SGNS يكون عدد الأمثلة السالبة دائماً أكبر من الموجبة، وهذا عكس ما توحي به كلمة "sampling".

What "sampling by frequency" actually means

ماذا يعني فعلاً "أخذ العينات حسب التكرار" (sampling by frequency)

Source note ملاحظة عن المصدر

Slide 77 says only "sampling by frequency" and gives no formula. The α = 0.75 weighting below is spelled out in the textbook (SLP3 chapter 5, eq. 5.19 to 5.20) and the identical arithmetic already appeared in this very deck on slide 59, where it was used to reweight PMI. It is the same trick applied twice. Know it, and know that the slide in this section states it only in words.

الشريحة 77 تقول فقط "sampling by frequency" ولا تعطي معادلة. أما الوزن α = 0.75 أدناه فمشروح في الكتاب (SLP3 الفصل 5، المعادلتان 5.19 إلى 5.20)، والحساب نفسه ظهر سابقاً في هذا العرض نفسه في الشريحة 59، حيث استُخدم لإعادة وزن PMI. إنها الحيلة نفسها مطبّقة مرتين. اعرفها، واعرف أن الشريحة في هذا القسم تذكرها بالكلمات فقط.

The naive reading of "sampling by frequency" would be: pick noise words with probability P(w), their plain unigram probability. Draw the with probability P(the), draw aardvark with probability P(aardvark), and so on.

القراءة الساذجة لـ "sampling by frequency" هي: اسحب كلمات الضوضاء باحتمال P(w)، أي احتمالها الأحادي (unigram) العادي. اسحب the باحتمال P(the)، واسحب aardvark باحتمال P(aardvark)، وهكذا.

That is almost right, and it fails for a specific reason. Word frequency is brutally skewed. If you sample purely by P(w), almost every negative example you ever generate is a function word, and genuinely rare words like aardvark or coaxial will essentially never be picked. Their context vectors in C would then never receive a gradient and would stay at their random initial values forever.

هذا صحيح تقريباً، لكنه يفشل لسبب محدد. تكرار الكلمات منحرف بشدة. إذا سحبت بحسب P(w) فقط، فكل مثال سالب تولّده تقريباً سيكون كلمة وظيفية، والكلمات النادرة فعلاً مثل aardvark أو coaxial لن تُختار عملياً أبداً. وعندها لن تتلقى متجهات سياقها في C أي تدرّج، وستبقى على قيمها العشوائية الأولية إلى الأبد.

The fix is to flatten the distribution by raising every count to a power less than one before normalizing:

العلاج هو تسطيح التوزيع برفع كل عدّ إلى أُسّ أقل من واحد قبل التطبيع:

Pα(w) = count(w)αΣw' count(w')αwith α = 0.75مع α = 0.75

تُقرأ: Pα(w) يساوي عدّ الكلمة مرفوعاً إلى α، مقسوماً على مجموع عدّات كل الكلمات مرفوعة إلى α، مع α = 0.75.

Why does an exponent below 1 flatten things? Because raising to a fractional power shrinks large numbers much harder than small ones. 1000.75 = 31.6, so 100 shrank by a factor of about 3.2. But 10.75 = 1, which did not shrink at all. The big counts get pulled down toward the small ones, and after renormalizing, the rare words have gained probability mass at the expense of the frequent ones.

لماذا يسطّح الأُسّ الأقل من 1 التوزيع؟ لأن الرفع إلى أُسّ كسري يقلّص الأعداد الكبيرة أكثر بكثير من الصغيرة. 1000.75 = 31.6، أي أن 100 تقلّصت بمعامل يقارب 3.2. لكن 10.75 = 1، فلم تتقلّص إطلاقاً. العدّات الكبيرة تُسحب نحو الصغيرة، وبعد إعادة التطبيع تكسب الكلمات النادرة كتلة احتمال على حساب الكلمات الشائعة.

Setting α = 1 would give you back plain frequency sampling. Setting α = 0 would give you uniform sampling, every word equally likely. α = 0.75 sits between them, closer to frequency than to uniform, and is the value that works best in practice.

وضع α = 1 يعيدك إلى السحب بالتكرار العادي. ووضع α = 0 يعطيك سحباً منتظماً (uniform) تكون فيه كل كلمة متساوية الاحتمال. وα = 0.75 يقع بينهما، أقرب إلى التكرار منه إلى المنتظم، وهو القيمة الأفضل عملياً.

raw frequency, α = 1 flattened, α = 0.75 .6211 .3106 .0621 .0062 .5543 .3296 .0986 .0175 the of apricot aardvark the of apricot aardvark ^0.75 aardvark goes from .0062 to .0175, about 2.8 times more likely to be sampled
The α = 0.75 exponent flattens the sampling distribution. The tallest bar loses mass and the shortest bar gains it, so rare words still end up in the negative set often enough to receive gradient updates. Counts behind this chart are the illustrative ones in the worked example below.
الأس α = 0.75 يسطّح توزيع أخذ العينات. يفقد العمود الأطول جزءاً من كتلته ويكسبه العمود الأقصر، فتدخل الكلمات النادرة مجموعة الأمثلة السالبة بما يكفي لتتلقى تحديثات gradient. والعدّات خلف هذا الرسم هي العدّات التوضيحية في المثال المحلول أدناه.

الأُسّ α = 0.75 يسطّح توزيع السحب. العمود الأطول يخسر كتلة والعمود الأقصر يكسبها، فتدخل الكلمات النادرة مجموعة الأمثلة السالبة بما يكفي لتتلقى تحديثات التدرّج. والعدّات وراء هذا الرسم هي العدّات التوضيحية في المثال المحلول أدناه.

Worked مثال محلول

First, the exact example from slide 59 and SLP3 eq. 5.20, with two events.

أولاً، المثال نفسه من الشريحة 59 ومن SLP3 المعادلة 5.20، بحدثين.

P(a) = 0.99    a very frequent word
P(b) = 0.01    a rare word

RAISE TO 0.75
  .99^0.75 = 0.992491
  .01^0.75 = 0.031623        (because .01 = 10^-2, so .01^.75 = 10^-1.5)

NORMALIZE
  denominator = 0.992491 + 0.031623 = 1.024114

  P_a(a) = 0.992491 / 1.024114 = 0.969122  ->  0.97
  P_a(b) = 0.031623 / 1.024114 = 0.030878  ->  0.03

  check: 0.969122 + 0.030878 = 1.000000     good

RESULT
  the rare event went from 0.01 to 0.03, three times more likely
  the frequent event went from 0.99 to 0.97, barely touched

خطوات الحل: P(a) = 0.99 كلمة شائعة جداً وP(b) = 0.01 كلمة نادرة. بعد الرفع إلى 0.75: 0.992491 و0.031623. والمقام 1.024114، فيصبح Pα(a) = 0.97 وPα(b) = 0.03. الحدث النادر صار أرجح بثلاثة أضعاف، والحدث الشائع بالكاد تأثّر.

Now the same idea on four words with actual counts. These counts are illustrative, invented for clean arithmetic.

والآن الفكرة نفسها على أربع كلمات بعدّات فعلية. هذه العدّات توضيحية، مخترعة لتكون الحسابات نظيفة.

counts:  the = 100,  of = 50,  apricot = 10,  aardvark = 1
total = 100 + 50 + 10 + 1 = 161

RAW UNIGRAM PROBABILITIES (alpha = 1)
  P(the)      = 100 / 161 = 0.6211
  P(of)       =  50 / 161 = 0.3106
  P(apricot)  =  10 / 161 = 0.0621
  P(aardvark) =   1 / 161 = 0.0062

RAISE EACH COUNT TO 0.75
  100^0.75 = 31.6228
   50^0.75 = 18.8030
   10^0.75 =  5.6234
    1^0.75 =  1.0000
  new denominator = 31.6228 + 18.8030 + 5.6234 + 1.0000 = 57.0492

NORMALIZE
  P_a(the)      = 31.6228 / 57.0492 = 0.5543
  P_a(of)       = 18.8030 / 57.0492 = 0.3296
  P_a(apricot)  =  5.6234 / 57.0492 = 0.0986
  P_a(aardvark) =  1.0000 / 57.0492 = 0.0175

  check: 0.5543 + 0.3296 + 0.0986 + 0.0175 = 1.0000   good

WHO WON AND WHO LOST
  the       0.6211 -> 0.5543     lost mass
  of        0.3106 -> 0.3296     gained slightly
  apricot   0.0621 -> 0.0986     gained, about 1.6x
  aardvark  0.0062 -> 0.0175     gained, about 2.8x

خطوات الحل: العدّات the = 100 وof = 50 وapricot = 10 وaardvark = 1، والمجموع 161. الاحتمالات الخام: 0.6211 و0.3106 و0.0621 و0.0062. بعد رفع كل عدّ إلى 0.75 يصبح المقام 57.0492، والاحتمالات الجديدة 0.5543 و0.3296 و0.0986 و0.0175. فـ the خسرت كتلة، وof كسبت قليلاً، وapricot كسبت قرابة 1.6 ضعف، وaardvark كسبت قرابة 2.8 ضعف.

The rarest word gained the most. That is the entire purpose of the exponent.

الكلمة الأندر كسبت الأكثر. هذا هو الغرض الكامل من الأُسّ.

Trap فخ

Negative sampling is not uniform sampling. If it were, you would write P(w) = 1/|V| and the exponent would be pointless. It is frequency-based sampling with the frequencies flattened. Frequent words are still more likely to be drawn as noise, just less overwhelmingly so.

Negative sampling ليس سحباً منتظماً. لو كان كذلك لكتبت P(w) = 1/|V| ولكان الأُسّ بلا فائدة. إنه سحب بحسب التكرار بعد تسطيح التكرارات. الكلمات الشائعة تبقى أرجح في السحب كضوضاء، لكن بهيمنة أقل.

Second trap: α = 0.75 does not mean "multiply by 0.75" and it does not mean "sample 75 percent of the time". It is an exponent applied to the count before the normalizing division.

الفخ الثاني: α = 0.75 لا يعني "اضرب في 0.75"، ولا يعني "اسحب 75 بالمئة من الوقت". إنه أُسّ يُطبَّق على العدّ قبل قسمة التطبيع.

Slide 79: what "learning" means here

الشريحة 79: ماذا يعني "التعلّم" هنا

You now have a pile of positive pairs and a bigger pile of negative pairs, plus an initial set of embedding vectors that are random. The goal of learning is to adjust those word vectors so that we:

لديك الآن كومة من الأزواج الموجبة وكومة أكبر من الأزواج السالبة، ومجموعة أولية من متجهات التمثيل العشوائية. وهدف التعلّم هو ضبط متجهات الكلمات هذه بحيث:

Two goals, pulling in opposite directions. Every formula on the rest of this page is a mechanical consequence of those two bullets.

هدفان يشدّان في اتجاهين متعاكسين. وكل معادلة في بقية هذه الصفحة نتيجة آلية لهاتين النقطتين.

Slide 80: the loss function, line by line

الشريحة 80: دالة الخسارة (loss function)، سطراً سطراً

The slide shows four equal signs. They are not four different ideas, they are one idea rewritten four times. Here is each step and the single move that produces it.

تعرض الشريحة أربع علامات مساواة. ليست أربع أفكار مختلفة، بل فكرة واحدة أُعيدت كتابتها أربع مرات. إليك كل خطوة والحركة الوحيدة التي تنتجها.

LCE = − log [ P(+|w, cpos) Πi=1k P(−|w, cnegi) ]

Line 1. Inside the bracket is the probability of getting everything right at once: the real neighbour scored as a +, and every one of the k noise words scored as a −. They are multiplied because we assume independence, same assumption as slide 72. The minus log in front turns "make this big" into "make this small", which is what a loss has to be.

السطر 1. داخل القوس احتمال أن يكون كل شيء صحيحاً معاً: الجار الحقيقي حصل على +، وكل واحدة من كلمات الضوضاء الـ k حصلت على −. تُضرب لأننا نفترض الاستقلال، وهو الافتراض نفسه في الشريحة 72. وسالب اللوغاريتم في المقدمة يحوّل "اجعل هذا كبيراً" إلى "اجعل هذا صغيراً"، وهذا ما يجب أن تكون عليه الخسارة.

= − [ log P(+|w, cpos) + Σi=1k log P(−|w, cnegi) ]

Line 2. The log of a product is the sum of the logs. Pure algebra, no new assumption.

السطر 2. لوغاريتم حاصل الضرب هو مجموع اللوغاريتمات. جبر خالص، بلا افتراض جديد.

= − [ log P(+|w, cpos) + Σi=1k log (1 − P(+|w, cnegi)) ]

Line 3. Substitute P(−) = 1 − P(+), which is slide 72's definition.

السطر 3. عوّض P(−) = 1 − P(+)، وهو تعريف الشريحة 72.

= − [ log σ(cpos · w) + Σi=1k log σ(−cnegi · w) ]

Line 4. Substitute P(+|w, c) = σ(c · w), and use the identity σ(−x) = 1 − σ(x) to collapse the 1 − P(+) term into a single sigmoid with a flipped sign. This is the form you should be able to write from memory.

السطر 4. عوّض P(+|w, c) = σ(c · w)، واستخدم المتطابقة σ(−x) = 1 − σ(x) لتحويل الحد 1 − P(+) إلى sigmoid واحد بإشارة مقلوبة. هذه هي الصيغة التي يجب أن تستطيع كتابتها من الذاكرة.

Read the final line as two instructions

اقرأ السطر الأخير كتعليمتين

Positive termالحد الموجبNegative termالحد السالب
What it isما هوlog σ(cpos · w)Σ log σ(−cnegi · w)
Sign inside σالإشارة داخل σdot product as isdot product كما هوdot product negateddot product مع عكس إشارته
How many termsعدد الحدود1k
Made small byيصغر عندماcpos · w going upيرتفع cpos · wcneg · w going downينخفض cneg · w
Geometric effectالأثر الهندسيpull w and cpos togetherاجذب w وcpos نحو بعضهماpush w and each cneg apartادفع w وكل cneg بعيداً عن بعضهما

اقرأ الجدول: الحد الموجب هو log σ(cpos · w)، والإشارة داخل σ هي dot product كما هو، وعدده حدّ واحد، ويصغر عندما يرتفع cpos · w، وأثره الهندسي سحب w وcpos نحو بعضهما. والحد السالب هو Σ log σ(−cnegi · w)، والإشارة داخل σ هي dot product مقلوباً، وعدده k حدود، ويصغر عندما ينخفض cneg · w، وأثره دفع w وكل cneg بعيداً عن بعضهما.

Memory hook خطّاف للذاكرة

One pull, k pushes.

سحبة واحدة، وk دفعات.

And for the sign: the negatives wear a minus inside the sigmoid. If both terms in your loss have the same sign inside σ, you have written it wrong, because then the loss would be asking you to make the noise words look like neighbours.

وبالنسبة للإشارة: السوالب تلبس علامة سالب داخل الـ sigmoid. إذا كان للحدّين في خسارتك الإشارة نفسها داخل σ فقد كتبتها خطأً، لأن الخسارة حينها ستطلب منك أن تجعل كلمات الضوضاء تبدو كجيران.

Slide 81: how to minimize it

الشريحة 81: كيف نصغّرها

Stochastic gradient descent. The slide's plain-English version: we will adjust the word weights to make the positive pairs more likely and the negative pairs less likely, over the entire training set.

الانحدار التدرّجي العشوائي (Stochastic gradient descent). والصياغة البسيطة في الشريحة: سنضبط أوزان الكلمات لنجعل الأزواج الموجبة أرجح والأزواج السالبة أقل ترجيحاً، على مجموعة التدريب كلها.

"Stochastic" means we do not compute the loss over the whole corpus before moving. We take one training instance (one w with its cpos and its k noise words), compute the gradient for just that instance, take a small step, then move on to the next instance.

"Stochastic" تعني أننا لا نحسب الخسارة على النص كله قبل أن نتحرك. نأخذ مثال تدريب واحداً (w واحدة مع cpos الخاصة بها وكلمات الضوضاء الـ k)، ونحسب التدرّج لهذا المثال فقط، ونخطو خطوة صغيرة، ثم ننتقل إلى المثال التالي.

Slide 82: one step of gradient descent, in pictures

الشريحة 82: خطوة واحدة من gradient descent، بالصور

The slide draws θ again, with W on top and C below, and marks three rows. The corpus fragment is "…apricot jam…", so w = apricot (from W), cpos = jam (from C), and with k = 2 the two noise words are matrix and Tolstoy (also from C).

ترسم الشريحة θ مجدداً، وW في الأعلى وC في الأسفل، وتميّز ثلاثة صفوف. مقطع النص هو "…apricot jam…"، فتكون w = apricot (من W)، وcpos = jam (من C)، ومع k = 2 تكون كلمتا الضوضاء matrix وTolstoy (من C أيضاً).

Three arrows come out of the apricot row:

تخرج ثلاثة أسهم من صف apricot:

Note that the arrows are bidirectional in effect. The update moves the apricot row in W and moves the jam, matrix and Tolstoy rows in C. Four vectors change on every single training instance.

لاحظ أن الأسهم ثنائية الاتجاه في أثرها. التحديث يحرّك صف apricot في W ويحرّك صفوف jam وmatrix وTolstoy في C. أربعة متجهات تتغير في كل مثال تدريب.

apricot w, from W jam cpos pull closer matrix cneg1 Tolstoy cneg2 push apart push apart 0
Slide 82 drawn as geometry. All four vectors start from the origin. The gradient rotates cpos toward w (raising the dot product) and rotates each cneg away from w (lowering it), while w itself shifts to the compromise position. "…apricot jam…" with k = 2.
الشريحة 82 مرسومة هندسياً. المتجهات الأربعة كلها تبدأ من نقطة الأصل. يدير gradient المتجه cpos نحو w (فيرفع dot product) ويدير كل cneg بعيداً عن w (فيخفضه)، بينما ينتقل w نفسه إلى موضع التسوية. المثال "…apricot jam…" مع k = 2.

الشريحة 82 مرسومة هندسياً. المتجهات الأربعة كلها تنطلق من نقطة الأصل. التدرّج يدير cpos نحو w (فيرفع dot product)، ويدير كل cneg بعيداً عن w (فيخفضه)، بينما تنتقل w نفسها إلى موضع وسط. المقطع "…apricot jam…" مع k = 2.

Slide 83: the gradient descent reminder

الشريحة 83: تذكير بـ gradient descent

Two properties of every gradient step, stated on the slide:

خاصيتان لكل خطوة تدرّج، كما تذكرهما الشريحة:

wt+1 = wt − η ddw L(f(x; w), y)

تُقرأ: w الجديدة تساوي w القديمة ناقص η مضروبة في مشتقة الخسارة بالنسبة إلى w.

That is the generic form from logistic regression. The next two slides just plug the skip-gram loss into it.

هذه هي الصيغة العامة من logistic regression. والشريحتان التاليتان تعوّضان فيها خسارة skip-gram فقط.

Slide 84: the three derivatives

الشريحة 84: المشتقات الثلاث

Differentiate the final line of slide 80 with respect to each of the three kinds of vector involved. The slide gives the answers:

اشتقّ السطر الأخير من الشريحة 80 بالنسبة لكل نوع من الأنواع الثلاثة من المتجهات. والشريحة تعطي الأجوبة:

∂LCE∂cpos = [ σ(cpos · w) − 1 ] w ∂LCE∂cneg = [ σ(cneg · w) ] w ∂LCE∂w = [ σ(cpos · w) − 1 ] cpos + Σi=1k [ σ(cnegi · w) ] cnegi

المشتقات الثلاث: بالنسبة إلى cpos هي [σ(cpos · w) − 1] w، وبالنسبة إلى cneg هي [σ(cneg · w)] w، وبالنسبة إلى w هي [σ(cpos · w) − 1] cpos زائد مجموع [σ(cnegi · w)] cnegi على كلمات الضوضاء الـ k.

You do not need to derive these for the exam, but you do need to read them, because the structure is extremely regular and that is what makes them memorable.

لا تحتاج إلى اشتقاقها في الامتحان، لكنك تحتاج إلى قراءتها، لأن بنيتها منتظمة جداً وهذا ما يجعلها سهلة التذكّر.

Every bracket is the same shape: the model's predicted probability minus the true label. For a positive pair the true label is 1, so the bracket is σ(·) − 1. For a negative pair the true label is 0, so the bracket is σ(·) − 0, which is just σ(·). That is the standard logistic regression error term, (prediction − truth).

كل قوس له الشكل نفسه: الاحتمال الذي تنبّأ به النموذج ناقص الوسم الحقيقي. في الزوج الموجب الوسم الحقيقي 1، فيكون القوس σ(·) − 1. وفي الزوج السالب الوسم الحقيقي 0، فيكون القوس σ(·) − 0، أي σ(·) فقط. وهذا هو حد الخطأ المعتاد في logistic regression: (التنبؤ ناقص الحقيقة).

And whatever is outside the bracket is the other vector in the dot product. Differentiating with respect to c gives you w on the outside. Differentiating with respect to w gives you c on the outside. The third line has k+1 terms because w participates in k+1 dot products, one with cpos and one with each noise word.

وما هو خارج القوس هو المتجه الآخر في dot product. الاشتقاق بالنسبة إلى c يعطيك w في الخارج، والاشتقاق بالنسبة إلى w يعطيك c في الخارج. والسطر الثالث فيه k+1 حدود لأن w تشارك في k+1 من حواصل الضرب النقطي، واحد مع cpos وواحد مع كل كلمة ضوضاء.

Memory hook خطّاف للذاكرة

Error times the other guy.

الخطأ مضروباً في الطرف الآخر.

bracket = prediction − label. outside = whichever vector you did not differentiate by. Positives get the −1, negatives do not.

القوس = التنبؤ − الوسم. والخارج = المتجه الذي لم تشتقّ بالنسبة إليه. الموجبات تأخذ −1، والسوالب لا.

Slide 85: the SGD update equations

الشريحة 85: معادلات التحديث في SGD

Start with randomly initialized C and W, then incrementally apply these. Each one is "old value minus η times the matching derivative from slide 84".

ابدأ بـ C وW مُهيّأتين عشوائياً، ثم طبّق هذه التحديثات تدريجياً. كل واحد منها هو "القيمة القديمة ناقص η مضروبة في المشتقة المقابلة من الشريحة 84".

cpost+1 = cpost − η [ σ(cpost · wt) − 1 ] wt cnegt+1 = cnegt − η [ σ(cnegt · wt) ] wt wt+1 = wt − η [ [ σ(cpos · wt) − 1 ] cpos + Σi=1k [ σ(cnegi · wt) ] cnegi ]

التحديثات الثلاثة: cpos الجديدة تساوي القديمة ناقص η [σ(cpos · w) − 1] w، وcneg الجديدة تساوي القديمة ناقص η [σ(cneg · w)] w، وw الجديدة تساوي القديمة ناقص η مضروبة في مشتقة w كاملة. وكل ما في الطرف الأيمن يُقرأ من القيم القديمة عند الزمن t.

Sanity-check the signs before you trust yourself on an exam. For the positive pair, σ(·) is between 0 and 1, so the bracket σ(·) − 1 is negative. Minus η times a negative number is a plus, so cpos moves in the direction of w. Correct: that is the pull.

تحقّق من الإشارات قبل أن تثق بنفسك في الامتحان. في الزوج الموجب σ(·) بين 0 و1، فيكون القوس σ(·) − 1 سالباً. وسالب η مضروباً في عدد سالب يعطي موجباً، فتتحرك cpos في اتجاه w. صحيح: هذا هو السحب.

For a negative pair the bracket σ(·) is positive. Minus η times a positive number is a minus, so cneg moves away from w. Correct: that is the push.

وفي الزوج السالب يكون القوس σ(·) موجباً. وسالب η مضروباً في عدد موجب يعطي سالباً، فتتحرك cneg بعيداً عن w. صحيح: هذا هو الدفع.

Worked مثال محلول

One complete SGD step by hand. Illustrative two-dimensional vectors with k = 1 and η = 0.2, chosen so every number stays readable. The slide gives no numeric example of its own.

خطوة SGD كاملة باليد. متجهات توضيحية ثنائية الأبعاد مع k = 1 وη = 0.2، مختارة لتبقى كل الأرقام مقروءة. الشريحة لا تعطي مثالاً رقمياً خاصاً بها.

SETUP
  w     (apricot) = [0.5, 0.5]
  c_pos (jam)     = [1.0, 0.0]
  c_neg (matrix)  = [0.0, 1.0]
  eta = 0.2,  k = 1

DOT PRODUCTS
  c_pos . w = 1.0(0.5) + 0.0(0.5) = 0.5 + 0.0 = 0.5
  c_neg . w = 0.0(0.5) + 1.0(0.5) = 0.0 + 0.5 = 0.5

  both are 0.5, so right now the model cannot tell jam from matrix

SIGMOIDS
  exp(-0.5)       = 0.606531
  1 + 0.606531    = 1.606531
  sigma(0.5)      = 1 / 1.606531 = 0.622459
  sigma(-0.5)     = 1 - 0.622459 = 0.377541

LOSS BEFORE THE STEP
  L = -[ ln sigma(c_pos . w) + ln sigma(-c_neg . w) ]
    = -[ ln(0.622459)        + ln(0.377541)         ]
    = -[ -0.474078           + -0.974076            ]
    = 1.448154

UPDATE c_pos        bracket = sigma(0.5) - 1 = 0.622459 - 1 = -0.377541
  eta x bracket            = 0.2 x (-0.377541) = -0.075508
  (eta x bracket) x w      = -0.075508 x [0.5, 0.5] = [-0.037754, -0.037754]
  c_pos_new = [1.0, 0.0] - [-0.037754, -0.037754]
            = [1.037754, 0.037754]

UPDATE c_neg        bracket = sigma(0.5) = 0.622459
  eta x bracket            = 0.2 x 0.622459 = 0.124492
  (eta x bracket) x w      = 0.124492 x [0.5, 0.5] = [0.062246, 0.062246]
  c_neg_new = [0.0, 1.0] - [0.062246, 0.062246]
            = [-0.062246, 0.937754]

UPDATE w            two terms inside the bracket
  term 1 = (sigma(0.5) - 1) x c_pos = -0.377541 x [1.0, 0.0] = [-0.377541, 0.0]
  term 2 = (sigma(0.5))     x c_neg =  0.622459 x [0.0, 1.0] = [0.0, 0.622459]
  sum    = [-0.377541, 0.622459]
  eta x sum = 0.2 x [-0.377541, 0.622459] = [-0.075508, 0.124492]
  w_new  = [0.5, 0.5] - [-0.075508, 0.124492]
         = [0.575508, 0.375508]

DID IT WORK? recompute both dot products with the new vectors
  c_pos_new . w_new = 1.037754(0.575508) + 0.037754(0.375508)
                    = 0.597236 + 0.014178
                    = 0.611414      was 0.5, went UP

  c_neg_new . w_new = -0.062246(0.575508) + 0.937754(0.375508)
                    = -0.035823 + 0.352134
                    = 0.316311      was 0.5, went DOWN

LOSS AFTER THE STEP
  sigma(0.611414)  = 0.648264
  sigma(-0.316311) = 0.421574
  L = -[ ln(0.648264) + ln(0.421574) ]
    = -[ -0.433458    + -0.863760    ]
    = 1.297218

  1.448154 -> 1.297218, the loss went down. one step, four vectors moved.

ملخص الحل: البداية w = [0.5, 0.5] وcpos = [1.0, 0.0] وcneg = [0.0, 1.0]. حاصلا الضرب النقطي كلاهما 0.5، فالنموذج لا يميّز jam من matrix. σ(0.5) = 0.622459، والخسارة قبل الخطوة 1.448154. التحديث يعطي cpos = [1.037754, 0.037754] وcneg = [−0.062246, 0.937754] وw = [0.575508, 0.375508]. بعدها ارتفع cpos · w من 0.5 إلى 0.611414، وانخفض cneg · w من 0.5 إلى 0.316311، ونزلت الخسارة من 1.448154 إلى 1.297218. خطوة واحدة، وأربعة متجهات تحرّكت.

This is the entire training algorithm. Repeat it for every (target, context) pair in a corpus of a billion words and the vectors stop being random.

هذه هي خوارزمية التدريب كلها. كرّرها لكل زوج (هدف، سياق) في نص من مليار كلمة، فتتوقف المتجهات عن كونها عشوائية.

Trap فخ

Use wt, not wt+1, when you update the context vectors. All three updates on slide 85 read from the same old values on the right-hand side, which is why the superscript t appears everywhere inside the brackets. If you update w first and then feed the new w into the cpos update, you are not doing the gradient step the slide describes.

استخدم wt لا wt+1 عند تحديث متجهات السياق. التحديثات الثلاثة في الشريحة 85 تقرأ كلها من القيم القديمة نفسها في الطرف الأيمن، ولهذا يظهر الرمز العلوي t في كل مكان داخل الأقواس. إذا حدّثت w أولاً ثم أدخلت w الجديدة في تحديث cpos، فأنت لا تنفّذ خطوة التدرّج التي تصفها الشريحة.

Second trap: the −1 belongs only to the positive term. Writing σ(cneg · w) − 1 would push the noise word toward the target, exactly backwards.

الفخ الثاني: الـ −1 تخص الحد الموجب فقط. كتابة σ(cneg · w) − 1 ستدفع كلمة الضوضاء نحو الهدف، أي العكس تماماً.

Slide 86: so what do you do with C?

الشريحة 86: إذن ماذا تفعل بـ C؟

Training is over. You are holding two matrices and you only wanted one set of word vectors. Slide 86 resolves it.

انتهى التدريب. بين يديك مصفوفتان، وأنت أردت مجموعة واحدة فقط من متجهات الكلمات. والشريحة 86 تحسم ذلك.

SGNS learns two sets of embeddings: the target embedding matrix W and the context embedding matrix C. The slide's answer: it is common to just add them together, representing word i as the vector wi + ci.

SGNS يتعلّم مجموعتين من التمثيلات: مصفوفة تمثيلات الهدف W ومصفوفة تمثيلات السياق C. وجواب الشريحة: من الشائع ببساطة جمعهما، فتُمثَّل الكلمة i بالمتجه wi + ci.

Both matrices are |V| by d, so the addition is elementwise and the result is still d-dimensional. You do not concatenate, which would give 2d dimensions.

كلتا المصفوفتين بحجم |V| في d، فالجمع يتم عنصراً بعنصر والناتج يبقى بـ d بُعد. أنت لا تضمّهما متتاليين (concatenate)، فذلك يعطي 2d بُعد.

The textbook (SLP3 chapter 5) lists the other common option: throw away C entirely and represent word i by wi alone. Both appear in real implementations. What you must not do is leave the question unanswered, because W on its own and C on its own are two different embedding spaces.

ويذكر الكتاب (SLP3 الفصل 5) الخيار الشائع الآخر: ارمِ C كلياً ومثّل الكلمة i بـ wi وحدها. وكلا الخيارين موجود في تطبيقات حقيقية. أما ما يجب ألّا تفعله فهو ترك السؤال بلا جواب، لأن W وحدها وC وحدها فضاءا تمثيل مختلفان.

Memory hook خطّاف للذاكرة

Add C to W, or bin C. Never concatenate.

اجمع C إلى W، أو ارمِ C. لا تضمّهما متتاليين أبداً.

wi + ci keeps the dimension at d. Concatenating would give 2d, and no one does that.

wi + ci يُبقي البُعد عند d. والضمّ المتتالي سيعطي 2d، ولا أحد يفعل ذلك.

Slide 87: the whole method in five lines

الشريحة 87: الطريقة كلها في خمسة أسطر

  1. Start with V random d-dimensional vectors as initial embeddings.

    ابدأ بـ V متجهاً عشوائياً بـ d بُعد كتمثيلات أولية.

  2. Train a classifier based on embedding similarity. Take a corpus, and take pairs of words that co-occur as positive examples.

    درّب مصنِّفاً مبنياً على تشابه التمثيلات. خذ نصاً، وخذ أزواج الكلمات التي تظهر معاً كأمثلة موجبة.

  3. Take pairs of words that do not co-occur as negative examples.

    خذ أزواج الكلمات التي لا تظهر معاً كأمثلة سالبة.

  4. Train the classifier to distinguish these by slowly adjusting all the embeddings to improve the classifier performance.

    درّب المصنِّف على التمييز بينها بضبط كل التمثيلات ببطء لتحسين أداء المصنِّف.

  5. Throw away the classifier code and keep the embeddings.

    ارمِ كود المصنِّف واحتفظ بالتمثيلات.

Line 5 is the punchline that slide 66 promised. The classifier was never the point.

السطر 5 هو الخلاصة التي وعدت بها الشريحة 66. المصنِّف لم يكن الهدف أبداً.

Slide 88: the hyperparameters worth memorizing

الشريحة 88: المعاملات الفائقة (hyperparameters) التي تستحق الحفظ

These are short, numeric, and extremely easy to turn into an exam question.

هذه قصيرة، ورقمية، وسهل جداً تحويلها إلى سؤال امتحان.

Hyperparameterالمعامل الفائقTypical valueالقيمة المعتادةNote from the slideملاحظة من الشريحة
Modelالنموذجskip-gram with negative samplingskip-gram مع negative samplingSGNS
Number of negative examples, kعدد الأمثلة السالبة، k15–20 small data
2–5 huge data
15–20 للبيانات الصغيرة
2–5 للبيانات الضخمة
huge datasets are the usual case, so 2–5 in practiceالبيانات الضخمة هي الحالة المعتادة، فعملياً 2–5
Embedding dimensionality, dعدد أبعاد الـ embedding، d300100 or 50 also possible100 أو 50 ممكنة أيضاً
Sliding window (context) sizeحجم النافذة المنزلقة (السياق)5–10meaning ±5 to ±10أي من ±5 إلى ±10

القيم: النموذج skip-gram with negative sampling أي SGNS. وعدد الأمثلة السالبة k من 15 إلى 20 للبيانات الصغيرة، ومن 2 إلى 5 للبيانات الضخمة، والبيانات الضخمة هي الحالة المعتادة فيكون عملياً من 2 إلى 5. وبُعد التمثيل d = 300، و100 أو 50 ممكنان أيضاً. وحجم النافذة من 5 إلى 10، أي ±5 إلى ±10.

The k rule is the counter-intuitive one, so hold on to the reasoning: less data means you need more negatives per positive, because with a small corpus you simply do not have enough examples and the extra noise words are doing the work of regularizing the model. With a billion-word corpus you already have enough signal and k = 2 to 5 is plenty.

قاعدة k هي المخالفة للحدس، فتمسّك بالتعليل: بيانات أقل تعني أنك تحتاج أمثلة سالبة أكثر لكل مثال موجب، لأن النص الصغير لا يعطيك أمثلة كافية، وكلمات الضوضاء الإضافية تقوم بعمل تنظيم النموذج (regularizing). ومع نص من مليار كلمة لديك إشارة كافية أصلاً، وk = 2 إلى 5 يكفي تماماً.

Thirty-second recap

خلاصة في ثلاثين ثانية

every window word is a positive كل كلمة في النافذة مثال موجب k negatives per positive, sampled by frequency k أمثلة سالبة لكل مثال موجب، تُسحب حسب التكرار Pα(w) = count(w).75 / Σ count(w').75 L = −[ log σ(cpos·w) + Σ log σ(−cneg·w) ] bracket = σ(·) − label, outside = the other vector القوس = σ(·) − label، وخارجه = المتجه الآخر update = old − η × gradient التحديث = القديم − η × gradient final vector = wi + ci المتجه النهائي = wi + ci d = 300, window 5–10, k = 2–5 on big data d = 300، النافذة 5–10، k = 2–5 على البيانات الكبيرة

Check yourself

اختبر نفسك

On slide 78 the target apricot has 4 positive context words and k = 2. How many negative examples are generated?

في الشريحة 78 للهدف apricot أربع كلمات سياق موجبة وk = 2. كم مثالاً سالباً يتولّد؟

k is the number of negatives per positive example, so 4 × 2 = 8, which is exactly how many rows the slide's negative table has. A is the tempting answer because k = 2 reads like "two negatives", but that is two per positive, not two in total. SGNS always ends up with more negative examples than positive ones.

الجواب C. k هو عدد الأمثلة السالبة لكل مثال موجب، فيكون 4 × 2 = 8، وهو بالضبط عدد صفوف جدول الأمثلة السالبة في الشريحة. والخيار A هو الجواب المغري لأن k = 2 تُقرأ كأنها "مثالان سالبان"، لكنها اثنان لكل موجب، لا اثنان إجمالاً. وفي SGNS ينتهي الأمر دائماً بأمثلة سالبة أكثر من الموجبة.

Why is the exponent α = 0.75 used when sampling noise words, instead of sampling from the plain unigram distribution?

لماذا يُستخدم الأُسّ α = 0.75 عند سحب كلمات الضوضاء، بدلاً من السحب من التوزيع الأحادي العادي؟

Raising counts to a power below 1 shrinks big counts much more than small ones, so after renormalizing the rare words gain probability mass. In the worked example aardvark goes from 0.0062 to 0.0175. A is the most tempting wrong answer: 0.75 moves the distribution toward uniform but does not reach it. Uniform would be α = 0. D is a separate constraint that is enforced directly, not through the exponent.

الجواب B. رفع العدّات إلى أُسّ أقل من 1 يقلّص العدّات الكبيرة أكثر بكثير من الصغيرة، فبعد إعادة التطبيع تكسب الكلمات النادرة كتلة احتمال. في المثال المحلول تنتقل aardvark من 0.0062 إلى 0.0175. والخيار A هو الجواب الخاطئ الأكثر إغراءً: 0.75 تحرّك التوزيع نحو المنتظم لكنها لا تصل إليه، والمنتظم هو α = 0. والخيار D قيد منفصل يُفرض مباشرة، لا عبر الأُسّ.

In the loss L = −[ log σ(cpos · w) + Σ log σ(−cnegi · w) ], why does the second term have a minus sign inside the sigmoid?

في الخسارة L = −[ log σ(cpos · w) + Σ log σ(−cnegi · w) ]، لماذا يحمل الحد الثاني علامة سالب داخل الـ sigmoid؟

The sigmoid identity σ(−x) = 1 − σ(x) lets you write P(−) with the same function as P(+). The flipped sign is the "not a neighbour" probability. C is tempting because there genuinely is a minus sign in front of the whole bracket, but that outer minus comes from the negative log likelihood and is a different sign from the one inside σ.

الجواب A. متطابقة الـ sigmoid σ(−x) = 1 − σ(x) تسمح بكتابة P(−) بالدالة نفسها التي تُكتب بها P(+). الإشارة المقلوبة هي احتمال "ليس جاراً". والخيار C مغرٍ لأن هناك فعلاً علامة سالب أمام القوس كله، لكن هذه العلامة الخارجية تأتي من سالب لوغاريتم الأرجحية (negative log likelihood)، وهي إشارة مختلفة عن التي داخل σ.

Using the slide 85 update for the positive context vector, with σ(cpos · w) = 0.6, η = 0.1 and w = [2, 0], what is the change applied to cpos?

باستخدام تحديث الشريحة 85 لمتجه السياق الموجب، مع σ(cpos · w) = 0.6 وη = 0.1 وw = [2, 0]، ما التغيير المطبَّق على cpos؟

The bracket is σ(·) − 1 = 0.6 − 1 = −0.4. Then η × bracket × w = 0.1 × (−0.4) × [2, 0] = [−0.08, 0]. The update subtracts that, so cpos changes by −(−0.08) = +0.08 in the first component. A is the trap: it is the gradient term itself, before the minus sign of the descent step is applied. The direction matters, because a positive context word must move toward w, not away.

الجواب D. القوس σ(·) − 1 = 0.6 − 1 = −0.4. ثم η × bracket × w = 0.1 × (−0.4) × [2, 0] = [−0.08, 0]. والتحديث يطرح ذلك، فتتغير cpos بمقدار −(−0.08) = +0.08 في المكوّن الأول. والخيار A هو الفخ: إنه حد التدرّج نفسه قبل تطبيق علامة السالب في خطوة النزول. والاتجاه مهم، لأن كلمة السياق الموجبة يجب أن تتحرك نحو w لا بعيداً عنها.

Training has finished. You have W and C. Slide 86 says the common thing to do is

انتهى التدريب ولديك W وC. تقول الشريحة 86 إن الشائع هو أن

The slide says it is common to just add them together, giving wi + ci, which stays d-dimensional. The textbook also allows throwing C away and keeping W alone. A is the tempting wrong answer because concatenation is a real technique elsewhere, but it would double the dimensionality and is not what the slide says. D has the roles backwards: if you discard one, you discard C and keep the target matrix W.

الجواب B. تقول الشريحة إن الشائع ببساطة جمعهما، فتحصل على wi + ci الذي يبقى بـ d بُعد. والكتاب يسمح أيضاً برمي C والاحتفاظ بـ W وحدها. والخيار A هو الجواب الخاطئ المغري لأن الضمّ المتتالي تقنية حقيقية في سياقات أخرى، لكنه يضاعف عدد الأبعاد وليس ما تقوله الشريحة. والخيار D يعكس الأدوار: إذا رميت إحداهما فارمِ C واحتفظ بمصفوفة الهدف W.

Slide 88 says to use 15 to 20 negative examples on smaller datasets but only 2 to 5 on huge ones. Why more negatives when there is less data?

تقول الشريحة 88 استخدم من 15 إلى 20 مثالاً سالباً في البيانات الأصغر، ومن 2 إلى 5 فقط في البيانات الضخمة. لماذا أمثلة سالبة أكثر عندما تكون البيانات أقل؟

Each positive pair is one training instance; on a small corpus you have very few of them, and pairing each with many noise words squeezes more learning out of each one. B is the tempting answer because the compute claim is technically true, but the slide is giving a statistical recommendation about data quantity, not a budget tip. D is invented: k and the window size are independent hyperparameters.

الجواب C. كل زوج موجب مثال تدريب واحد، وفي النص الصغير لديك عدد قليل جداً منها، وإقران كل واحد بكلمات ضوضاء كثيرة يستخلص تعلّماً أكثر من كل مثال. والخيار B مغرٍ لأن ادعاء الحوسبة صحيح تقنياً، لكن الشريحة تعطي توصية إحصائية تخص كمية البيانات، لا نصيحة ميزانية. والخيار D مختلَق: k وحجم النافذة معاملان مستقلان.

In the derivative ∂L/∂w = [σ(cpos · w) − 1]cpos + Σ[σ(cnegi · w)]cnegi, why does the first bracket have a −1 and the others do not?

في المشتقة ∂L/∂w = [σ(cpos · w) − 1]cpos + Σ[σ(cnegi · w)]cnegi، لماذا يحمل القوس الأول −1 ولا تحمله البقية؟

This is standard logistic regression: the error term is always prediction minus truth. A positive pair should score 1, so the error is σ(·) − 1 and is negative, which pulls the vectors together. A negative pair should score 0, so the error is σ(·) − 0 = σ(·) and is positive, which pushes them apart. C is tempting because the counts 1 and k really are different, but that explains how many terms there are, not why one bracket carries a −1.

الجواب A. هذا logistic regression المعتاد: حد الخطأ دائماً التنبؤ ناقص الحقيقة. الزوج الموجب يجب أن يحصل على 1، فيكون الخطأ σ(·) − 1 وهو سالب، فيسحب المتجهين نحو بعضهما. والزوج السالب يجب أن يحصل على 0، فيكون الخطأ σ(·) − 0 = σ(·) وهو موجب، فيدفعهما بعيداً. والخيار C مغرٍ لأن العددين 1 وk مختلفان فعلاً، لكن ذلك يفسّر عدد الحدود، لا سبب حمل أحد الأقواس لـ −1.