Lecture 04 · Word Embeddings
المحاضرة 04 · تضمينات الكلمات (Word Embeddings)
Vector Semantics
دلالات المتجهات (Vector Semantics)
P3 SKIMHow "meaning comes from neighbours" plus "meaning is a point in space" combine into the one claim the rest of the course rests on: a word is a vector.
كيف تتحد فكرة "المعنى يأتي من الكلمات المجاورة" مع فكرة "المعنى نقطة في فضاء" لتُنتجا الادعاء الذي يقوم عليه بقية المقرر: الكلمة متجه (vector).
The question slide 18 asks
السؤال الذي تطرحه الشريحة 18
The last page produced a wish list: synonymy, similarity, relatedness, antonymy, connotation. Slide 18 asks whether we can build one theory of how to represent word meaning that accounts for at least some of those desiderata.
أنتجت الصفحة السابقة قائمة مطالب: synonymy، similarity، relatedness، antonymy، connotation. وتسأل الشريحة 18: هل يمكننا بناء نظرية واحدة لتمثيل معنى الكلمة تحقق بعض هذه المطالب على الأقل؟
The answer the lecture gives: vector semantics. The slide's own two claims about it:
الجواب الذي تعطيه المحاضرة: vector semantics. وللشريحة ادعاءان عنه:
- It is the standard model in language processing.
إنه النموذج القياسي في معالجة اللغة (the standard model in language processing).
- It handles many of our goals.
وإنه يحقق كثيراً من أهدافنا.
Where the idea comes from: two quotes
من أين جاءت الفكرة: اقتباسان
Ludwig Wittgenstein (1953)
Ludwig Wittgenstein (1953)
"The meaning of a word is its use in the language"
Philosophy, not NLP. But it reframes the problem usefully: stop asking what a word refers to and start looking at how it is used.
فلسفة لا NLP، لكنها تعيد صياغة المشكلة بشكل مفيد: توقف عن السؤال عمّا تشير إليه الكلمة، وابدأ بالنظر في كيف تُستخدم.
Zellig Harris (1954)
Zellig Harris (1954)
"If A and B have almost identical environments we say that they are synonyms"
Linguistics, and this one is operational. It turns Wittgenstein's slogan into something a computer can measure.
لسانيات، وهذه الصيغة قابلة للتطبيق: تحوّل شعار Wittgenstein إلى شيء يستطيع الحاسوب قياسه.
Slide 20 supplies the missing definition that links them. One way to define "usage" is: words are defined by their environments, meaning the words around them. Harris then says that identical environments imply synonymy.
تقدّم الشريحة 20 التعريف الناقص الذي يربط بينهما. إحدى طرق تعريف "الاستخدام" هي: الكلمات تُعرَّف بـ environments الخاصة بها، أي بالكلمات المحيطة بها. ثم يقول Harris إن تطابق البيئات يعني synonymy.
That chain is the distributional hypothesis. Learn the two names in order: Wittgenstein gives the slogan, Harris gives the testable version.
هذه السلسلة هي distributional hypothesis. احفظ الاسمين بالترتيب: Wittgenstein يعطي الشعار، وHarris يعطي الصيغة القابلة للاختبار.
The ongchoi example
مثال ongchoi
Slide 21. This is the best teaching device in the whole lecture. Work through it honestly; do not skip to the answer.
الشريحة 21، وهي أفضل أداة تعليمية في المحاضرة كلها. اعمل عليها بصدق ولا تقفز إلى الجواب.
Suppose you see these three sentences and you have never met the word before:
افترض أنك رأيت هذه الجمل الثلاث ولم تقابل الكلمة من قبل أبداً:
Ong choi is delicious sauteed with garlic. Ong choi is superb over rice. Ong choi leaves with salty sauces.
الأسطر الثلاثة كلها عن ong choi. ما يهمك فيها ليس معنى الجملة بل الكلمات المجاورة: delicious، sauteed، garlic، rice، leaves، salty.
And separately, in other texts, you have seen these:
وبشكل منفصل، في نصوص أخرى، رأيت هذه الأسطر:
...spinach sauteed with garlic over rice Chard stems and leaves are delicious Collard greens and other salty leafy greens
هذه الأسطر عن spinach وchard وcollard greens، وتحمل الكلمات المجاورة نفسها تقريباً. اقرأ المجموعتين وقارن بينهما كلمة بكلمة.
Line up the context words that appear on both sides.
رتّب كلمات السياق التي تظهر في الجهتين.
Ongchoi's neighbours are almost exactly spinach's, chard's and collard greens' neighbours. Harris says: almost identical environments implies near-synonymy.
الكلمات المجاورة لـ ongchoi هي تقريباً نفس مجاورات spinach وchard وcollard greens. ويقول Harris: تطابق البيئات تقريباً يعني تقارباً في المعنى.
Conclusion from slide 21: ongchoi is a leafy green like spinach, chard, or collard greens. The slide names the evidence explicitly: "leaves", "delicious", and "sauteed".
خلاصة الشريحة 21: ongchoi نبات ورقي أخضر مثل spinach أو chard أو collard greens. وتسمّي الشريحة الدليل صراحة: "leaves" و"delicious" و"sauteed".
Now notice what you did not use. No dictionary. No definition. No picture. No one told you it was a vegetable. You inferred the meaning purely from the company the word keeps. That is the distributional hypothesis, demonstrated rather than asserted.
لاحظ الآن ما لم تستخدمه: لا قاموس، ولا تعريف، ولا صورة، ولم يخبرك أحد أنها نبات. استنتجت المعنى من رفقة الكلمة فقط. هذه هي distributional hypothesis، مُبرهنة بالعمل لا مجرد مُدّعاة.
Slide 22 reveals the answer: ongchoi is Ipomoea aquatica, water spinach. It is also called kangkong, rau muong, or in Chinese 空心菜. You were right, and you were right without ever being told.
تكشف الشريحة 22 الجواب: ongchoi هو Ipomoea aquatica، أي water spinach، ويسمى أيضاً kangkong أو rau muong أو بالصينية 空心菜. كنت محقاً، ومحقاً دون أن يخبرك أحد.
"You shall know a word by the company it keeps."
"تعرف الكلمة من رفقتها" ("You shall know a word by the company it keeps.")
Meaning comes from neighbours, not from a dictionary. If you remember one sentence from this page, remember that one, and attach the ongchoi story to it so you can reconstruct the argument in an exam.
المعنى يأتي من الكلمات المجاورة، لا من القاموس. إن تذكرت جملة واحدة من هذه الصفحة فلتكن هذه، واربط بها قصة ongchoi حتى تستطيع إعادة بناء الحجة كلها في الامتحان.
Idea 1 and Idea 2
الفكرة 1 والفكرة 2
Slides 23 to 25 name the two halves and then glue them together. The exam can ask for either half by name, so keep the numbering.
الشرائح 23 إلى 25 تسمّي النصفين ثم تلصقهما معاً. والامتحان قد يسأل عن أي نصف بالاسم، فاحفظ الترقيم.
- Idea 1: defining meaning by linguistic distribution. Define the meaning of a word by its distribution in language use, meaning its neighbouring words or grammatical environments. This is Wittgenstein plus Harris plus ongchoi.
Idea 1: تعريف المعنى بالتوزيع اللغوي. عرّف معنى الكلمة بتوزيعها في استخدام اللغة، أي بـ الكلمات المجاورة أو grammatical environments. هذا هو Wittgenstein زائد Harris زائد ongchoi.
- Idea 2: meaning as a point in space (Osgood et al. 1957). From the previous page: valence, arousal, dominance. Three numbers per word, so the connotation of a word is a vector in 3-space. The idea generalises: meaning is a point in multidimensional space.
Idea 2: المعنى نقطة في فضاء (Osgood et al. 1957). من الصفحة السابقة: valence وarousal وdominance، ثلاثة أرقام لكل كلمة، فتكون connotation الكلمة متجهاً في فضاء ثلاثي الأبعاد. والفكرة تتعمّم: المعنى نقطة في فضاء متعدد الأبعاد.
Slide 25 simply puts them on one slide, because the combination is the whole model:
تضع الشريحة 25 الفكرتين في شريحة واحدة، لأن الجمع بينهما هو النموذج كله:
يُقرأ الصندوق كمعادلة: Idea 1 (المعنى = التوزيع) زائد Idea 2 (المعنى = نقطة في فضاء) يساوي النتيجة المظللة: الكلمة متجه يُتعلَّم من الكلمات المجاورة لها. هذا السطر الأخير هو ما تكتبه لو طُلب منك تعريف vector semantics في سطر واحد.
Defining meaning as a point in space based on distribution
تعريف المعنى كنقطة في فضاء بناءً على التوزيع (distribution)
Slide 26 states the three consequences. These are the sentences most likely to be quoted back at you.
تذكر الشريحة 26 ثلاث نتائج، وهي الجمل الأكثر احتمالاً أن تُقتبس عليك في الامتحان.
- Each word is a vector, not just "good" or "w45".
كل كلمة هي متجه (vector)، وليست مجرد رمز مثل "good" أو "w45".
- Similar words are nearby in semantic space.
الكلمات المتشابهة قريبة من بعضها في semantic space.
- We build this space automatically, by seeing which words are nearby in text.
نبني هذا الفضاء آلياً، بملاحظة أي الكلمات تظهر متجاورة في النص.
That third bullet is the engineering payoff. Nobody hand-labels anything. You point the method at raw text and the space builds itself.
النقطة الثالثة هي المكسب الهندسي: لا أحد يصنّف شيئاً يدوياً. توجّه الطريقة إلى نص خام، فيبني الفضاء نفسه بنفسه.
يرسم الشكل semantic space في بعدين تخطيطياً، وفيه ثلاثة عناقيد: leafy greens (ومعها ongchoi وspinach وchard وcollard greens)، وtech words، وbeverages. اقرأه هكذا: الكلمات ذات التوزيع المتشابه تسقط قريبة من بعضها، فيجد ongchoi نفسه داخل عنقود الأوراق الخضراء بلا أن يعرّفه أحد. والنقطة المعزولة cup هي الدرس المهم: مرتبطة بـ coffee لكنها بعيدة عنها، فالارتباط (relatedness) لا يعني التشابه (similarity).
The word "embedding"
كلمة "embedding"
Slide 27 defines the term you have been waiting for.
تعرّف الشريحة 27 المصطلح الذي كنت تنتظره.
We define the meaning of a word as a vector. It is called an "embedding" because it is embedded into a space.
نعرّف معنى الكلمة بأنه متجه (vector). ويسمى "embedding" لأنه مغروس في فضاء (embedded into a space).
- It is the standard way to represent meaning in NLP.
إنه الطريقة القياسية لتمثيل المعنى في NLP.
- Every modern NLP algorithm uses embeddings as the representation of word meaning.
كل خوارزمية NLP حديثة تستخدم embeddings لتمثيل معنى الكلمة.
- It gives a fine-grained model of meaning for similarity: not just "same or different" but a continuous degree.
يعطي نموذجاً دقيق التفاصيل للمعنى يصلح لقياس التشابه: ليس "متطابق أو مختلف" فقط، بل درجة متصلة.
If an exam asks "why the word embedding?", the answer is the etymology on this slide: the word is embedded into a vector space.
إذا سأل الامتحان "why the word embedding?" فالجواب هو الاشتقاق الموجود في هذه الشريحة: الكلمة مغروسة في فضاء متجهات.
Why vectors? The sentiment analysis argument
لماذا المتجهات؟ حجة تحليل المشاعر (sentiment analysis)
Slide 28. This is the slide that justifies the whole effort, and it is a likely short-answer question. The argument is a direct comparison.
الشريحة 28، وهي الشريحة التي تبرّر كل هذا الجهد، وسؤال مقالي قصير محتمل. والحجة فيها مقارنة مباشرة.
With words as features
مع الكلمات كسمات (features)
A feature is a word identity.
الميزة (feature) هي هوية كلمة.
Feature 5: 'the previous word was "terrible"'
This fires only on an exact string match. The identical word has to appear in both training and test. See "awful" at test time, having only trained on "terrible", and the feature is dead.
هذه الميزة لا تعمل إلا عند تطابق نصي تام، فيجب أن تظهر الكلمة نفسها في التدريب والاختبار. وإذا رأيت "awful" وقت الاختبار وكنت قد دربت على "terrible" فقط، فالميزة ميتة.
With embeddings as features
مع الـ embeddings كسمات
A feature is a word vector.
الميزة هي متجه كلمة.
'the previous word was vector [35, 22, 17, ...]'
At test time you might see a similar vector [34, 21, 14]. Close enough to fire. You generalise to similar but unseen words.
وقت الاختبار قد ترى متجهاً مشابهاً مثل [34, 21, 14]، قريباً بما يكفي ليعمل. فأنت تُعمّم على كلمات مشابهة لم ترها من قبل.
Vectors buy you generalisation. String features demand exact matches and shatter on unseen vocabulary. Vector features degrade gracefully, because a nearby vector is still a useful signal. That is the entire "why vectors?" slide in one sentence.
المتجهات تشتري لك التعميم. ميزات النصوص تطلب تطابقاً تاماً وتتفتت أمام أي كلمة جديدة، أما ميزات المتجهات فتتدهور بلطف، لأن متجهاً قريباً يبقى إشارة مفيدة. هذه هي شريحة "why vectors?" كلها في جملة واحدة.
The two kinds of embeddings this course covers
نوعا الـ embeddings اللذان يغطيهما المقرر
Slide 29 sets up the structure of everything that follows. Learn this split now, because the next several pages live inside it.
تضع الشريحة 29 هيكل كل ما سيأتي بعدها. تعلّم هذا التقسيم الآن، لأن الصفحات التالية كلها تسكن داخله.
| Sparseمتناثر (Sparse) | Denseكثيف (Dense) | |
|---|---|---|
| Named on the slideالمذكور في الشريحة | tf-idf | Word2vecWord2vec |
| Vector typeنوع المتجه | Sparse vectors (mostly zeros)متجهات متناثرة (sparse)، معظمها أصفار | Dense vectors (mostly non-zero)متجهات كثيفة (dense)، معظمها غير صفري |
| Lengthالطول | Long: one dimension per vocabulary item or per document, so thousands to millionsطويل: بُعد لكل كلمة في المفردات أو لكل مستند، أي من آلاف إلى ملايين | Short: typically a few hundred dimensionsقصير: عادةً بضع مئات من الأبعاد |
| How values are producedكيف تُنتَج القيم | Words are represented by (a simple function of) the counts of nearby wordsتُمثَّل الكلمات بـ (دالة بسيطة من) عدّات الكلمات المجاورة | Representation is created by training a classifier to predict whether a word is likely to appear nearbyيُبنى التمثيل عبر تدريب مصنِّف (classifier) يتنبأ بما إذا كانت كلمة ما مرجحة الظهور بالقرب |
| Counted or learned?معدود أم مُتعلَّم؟ | Counted, then reweightedمعدود، ثم يُعاد ترجيحه | Learned by optimisationمُتعلَّم بالتحسين (optimisation) |
| Role in the courseدوره في المقرر | "Information Retrieval workhorse", a common baseline model"Information Retrieval workhorse"، أي حصان العمل في استرجاع المعلومات، ونموذج أساس (baseline) شائع | The modern representation; later extended to contextual embeddingsالتمثيل الحديث، ويُوسَّع لاحقاً إلى contextual embeddings |
يقارن الجدول عمودين: Sparse ومثاله tf-idf، وDense ومثاله Word2vec. اقرأه صفاً صفاً: المتجهات المتناثرة طويلة (بُعد لكل كلمة في vocabulary أو لكل وثيقة، فآلاف إلى ملايين) ومعظم قيمها أصفار، وتأتي من عدّ الكلمات المجاورة. أما الكثيفة فقصيرة (بضع مئات من الأبعاد) وتأتي من تدريب classifier يتنبأ إن كانت كلمة محتملة الظهور بالجوار. والصف الأخير هو الدور في المقرر: tf-idf حصان Information Retrieval وbaseline شائع، والكثيف هو التمثيل الحديث الذي يُمتد لاحقاً إلى contextual embeddings.
The other member of the sparse family you will meet shortly is PPMI, positive pointwise mutual information. It is a different reweighting of the same raw counts. tf-idf and PPMI are both count-based and both sparse; word2vec is predict-based and dense.
العضو الآخر في عائلة sparse الذي ستقابله قريباً هو PPMI، أي positive pointwise mutual information، وهو إعادة ترجيح مختلفة لنفس العدّات الخام. فـ tf-idf وPPMI كلاهما قائم على العدّ وكلاهما sparse، أما word2vec فقائم على التنبؤ وdense.
Do not mix up the two axes. Sparse vs dense is about how many zeros the vector has, which follows from how long it is. Count-based vs prediction-based is about how the numbers were produced. tf-idf is sparse and count-based; word2vec is dense and prediction-based, so in this course they travel together, but they are two separate properties and a question can ask about either one.
لا تخلط بين المحورين. Sparse مقابل dense يتعلق بعدد الأصفار في المتجه، وهو ناتج عن طوله. وcount-based مقابل prediction-based يتعلق بكيفية إنتاج الأرقام. فـ tf-idf متناثر وقائم على العدّ، وword2vec كثيف وقائم على التنبؤ، فهما يسافران معاً في هذا المقرر، لكنهما خاصيتان منفصلتان ويمكن للسؤال أن يسأل عن أي واحدة منهما.
Also: "dense" does not mean "better at everything". The slide calls tf-idf the Information Retrieval workhorse and a common baseline, not a discarded relic.
وأيضاً: "dense" لا تعني "أفضل في كل شيء". الشريحة تسمي tf-idf حصان Information Retrieval وbaseline شائعاً، لا أثراً مهجوراً.
Where this goes next
إلى أين نتجه بعد ذلك
You now have the claim (a word is a vector built from its neighbours) but no actual numbers. The next page builds the first concrete vectors by literally counting: the term-document matrix and the word-word matrix.
أصبح عندك الادعاء (الكلمة متجه مبني من مجاوراتها) لكن بلا أرقام فعلية. والصفحة التالية تبني أول متجهات ملموسة بالعدّ الحرفي: term-document matrix وword-word matrix.
Check yourself
اختبر نفسك
Zellig Harris (1954) is quoted on the slides as claiming what?
ما الادعاء الذي تنسبه الشرائح إلى Zellig Harris (1954)؟
C. A is Wittgenstein (1953), which the lecture shows on the slide immediately before Harris. The pair is easy to swap under pressure, so anchor them: Wittgenstein gives the philosophical slogan, Harris gives the operational, measurable version in terms of environments. B is Osgood (1957) and D is the Principle of Contrast from the lexical semantics section.
الجواب C. والخيار A هو Wittgenstein (1953)، وتعرضه المحاضرة في الشريحة التي تسبق Harris مباشرة. من السهل تبديلهما تحت الضغط، فثبّتهما: Wittgenstein يعطي الشعار الفلسفي، وHarris يعطي الصيغة العملية القابلة للقياس بدلالة environments. وB هو Osgood (1957)، وD هو Principle of Contrast من قسم lexical semantics.
In the ongchoi example, what let you conclude that ongchoi is a leafy green?
في مثال ongchoi، ما الذي سمح لك باستنتاج أنه نبات ورقي أخضر؟
B. The slide names exactly those three context words as the evidence. The whole point of the example is that you reached a correct conclusion with no definition available, which is what makes it a clean demonstration of the distributional hypothesis. A is the thing you deliberately did not have. C is tempting but the argument on the slide is purely distributional, not morphological, and D is irrelevant: how often a word appears says nothing about what it means.
الجواب B. الشريحة تسمّي تلك الكلمات السياقية الثلاث بالتحديد كدليل. وكل مقصد المثال أنك وصلت إلى نتيجة صحيحة بدون أي تعريف متاح، وهذا ما يجعله برهاناً نظيفاً على distributional hypothesis. وA هو الشيء الذي تعمّدت ألا يكون بين يديك. وC مغرٍ، لكن الحجة في الشريحة توزيعية بحتة لا صرفية. وD غير ذي صلة: تكرار الكلمة لا يقول شيئاً عن معناها.
The lecture motivates embeddings with a sentiment analysis example. What is the advantage over using word identities as features?
تبرّر المحاضرة embeddings بمثال sentiment analysis. فما الميزة على استخدام هويات الكلمات كـ features؟
A. The slide contrasts the feature 'the previous word was "terrible"', which requires the exact same word in training and test, with 'the previous word was vector [35,22,17...]', where a test-time vector of [34,21,14] is close enough to be useful. B is not the argument on the slide, and a long sparse vector is in fact bulkier than a string. C is plainly false; both word2vec and the classifier still need data. D is actually the opposite of the truth: individual embedding dimensions are not interpretable, which is a known cost of dense vectors.
الجواب A. تقابل الشريحة بين ميزة 'the previous word was "terrible"' التي تطلب الكلمة نفسها في التدريب والاختبار، وميزة 'the previous word was vector [35,22,17...]' حيث يكون متجه اختبار مثل [34,21,14] قريباً بما يكفي ليكون مفيداً. وB ليست الحجة المذكورة، بل إن المتجه المتناثر الطويل أثقل من النص فعلاً. وC خاطئة بوضوح، فـ word2vec وclassifier كلاهما يحتاج بيانات. وD عكس الحقيقة: أبعاد embedding المفردة غير قابلة للتفسير، وهذه تكلفة معروفة للمتجهات الكثيفة.
According to slide 29, which pairing is correct?
حسب الشريحة 29، أي اقتران هو الصحيح؟
D. The slide splits embeddings into Sparse (tf-idf), where words are represented by a simple function of the counts of nearby words, and Dense (word2vec), where the representation is created by training a classifier to predict whether a word is likely to appear nearby. C is the exact inversion, which is the trap. A inverts only the sparsity. Remember the roles too: tf-idf is called the Information Retrieval workhorse and a common baseline, and the dense line later extends to contextual embeddings.
الجواب D. تقسّم الشريحة embeddings إلى Sparse (tf-idf) حيث تُمثَّل الكلمات بدالة بسيطة لعدّات الكلمات المجاورة، وDense (word2vec) حيث يُنشأ التمثيل بتدريب classifier يتنبأ إن كانت كلمة محتملة الظهور بالجوار. وC هو العكس التام، وهو الفخ. وA يعكس التناثر وحده. وتذكّر الأدوار أيضاً: tf-idf يُسمى حصان Information Retrieval وbaseline شائعاً، والخط الكثيف يمتد لاحقاً إلى contextual embeddings.
Why is a word vector called an "embedding"?
لماذا يسمى متجه الكلمة "embedding"؟
B. The slide's wording is literal: it is called an embedding "because it's embedded into a space". A sounds plausible for word2vec specifically, but tf-idf vectors are embeddings too and involve no network at all, so the term cannot depend on neural weights. The same slide adds the two claims worth quoting: embeddings are the standard way to represent meaning in NLP, and every modern NLP algorithm uses them.
الجواب B. صياغة الشريحة حرفية: يسمى embedding لأنه "embedded into a space". وA يبدو معقولاً لـ word2vec تحديداً، لكن متجهات tf-idf هي embeddings أيضاً ولا تتضمن أي شبكة، فالمصطلح لا يمكن أن يعتمد على أوزان عصبية. وتضيف الشريحة نفسها ادعاءين يستحقان الاقتباس: embeddings هي الطريقة القياسية لتمثيل المعنى في NLP، وكل خوارزمية NLP حديثة تستخدمها.