المحوّل والانتباه / Transformers and attention

الترتيب / Order
3 من 14 / 3 of 14
آخر تحديث / Updated
8 أكتوبر 2026 / 8 October 2026

المستوى: مبتدئ · الدرس 3 من 14

أهداف الدرس

  1. أن تفهم فكرة الانتباه.
  2. أن تعرف ما نافذة السياق.

المحوّل (Transformer) هو البنية خلف النماذج اللغوية الحديثة. سرّه الانتباه: كل كلمة تنظر إلى بقية الكلمات وتقرّر أيّها أهمّ لفهمها. تتكرّر هذه الخطوة في طبقات كثيرة، فيبني النموذج فهماً أعمق طبقة بعد طبقة.

الانتباه يربط الضمير بما يعود إليه
الانتباه يربط الضمير بما يعود إليه
Python
import numpy as np

def attention(Q, K, V):
    d = Q.shape[-1]
    scores = Q @ K.T / np.sqrt(d)                   # how much each word looks at each other word
    scores -= scores.max(axis=-1, keepdims=True)
    weights = np.exp(scores)
    weights /= weights.sum(axis=-1, keepdims=True)  # softmax: each row sums to 1
    return weights @ V, weights

rng = np.random.default_rng(0)
Q = K = V = rng.normal(size=(5, 8))   # 5 words, 8 numbers each
out, w = attention(Q, K, V)
print(w.sum(axis=1).round(2))         # [1. 1. 1. 1. 1.]
الفكرةببساطة
رؤوس انتباه متعدّدةعدّة «عيون» تنظر إلى علاقات مختلفة معاً
الموضعمعلومة عن ترتيب الكلمات
نافذة السياقأقصى ما يقرؤه النموذج دفعة واحدة — اليوم مئات آلاف الرموز في نماذج كثيرة

تمرين

لماذا لا يكفي أن ينظر النموذج إلى الكلمة السابقة وحدها؟

الإجابة

لأن المعنى قد يتعلّق بكلمة بعيدة: الضمير في آخر الجملة يعود إلى اسم في أولها.

Level: Beginner · Lesson 3 of 14

Lesson goals

  1. Understand the idea of attention.
  2. Know what a context window is.

The Transformer is the architecture behind modern language models. Its secret is attention: each word looks at the others and decides which matter most for its meaning. The step repeats over many layers, so understanding deepens layer by layer.

Attention links a pronoun to what it refers to
Attention links a pronoun to what it refers to
Python
import numpy as np

def attention(Q, K, V):
    d = Q.shape[-1]
    scores = Q @ K.T / np.sqrt(d)                   # how much each word looks at each other word
    scores -= scores.max(axis=-1, keepdims=True)
    weights = np.exp(scores)
    weights /= weights.sum(axis=-1, keepdims=True)  # softmax: each row sums to 1
    return weights @ V, weights

rng = np.random.default_rng(0)
Q = K = V = rng.normal(size=(5, 8))   # 5 words, 8 numbers each
out, w = attention(Q, K, V)
print(w.sum(axis=1).round(2))         # [1. 1. 1. 1. 1.]
IdeaSimply
Multi-head attentionSeveral «eyes» looking at different relations at once
PositionInformation about word order
Context windowThe most the model reads at once — hundreds of thousands of tokens in many models today

Exercise

Why is looking only at the previous word not enough?

Answer

Meaning can depend on a distant word: a pronoun at the end refers to a noun at the start.

التعليقات / Comments

لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.

تعليق الطالب يظهر فوراً، وتعليق الزائر بعد موافقة المدرّس. / A student's comment shows at once; a visitor's once the teacher approves it.

في «الذكاء الاصطناعي: من الصفر إلى الاحتراف» / In “AI: from zero to hero”

  1. 1ما الذكاء الاصطناعي التوليدي؟ / What is generative AI?فيها صور / has picturesالتعليقات مفتوحة / comments open
  2. 2الرموز والتضمينات / Tokens and embeddingsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  3. 3المحوّل والانتباه / Transformers and attentionفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  4. 4التدريب المسبق / Pre-trainingفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  5. 5الضبط بالتعليمات / Instruction tuning (SFT)فيها كود / has codeالتعليقات مفتوحة / comments open
  6. 6التعلّم من التفضيلات / Learning from preferencesفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  7. 7نماذج الاستدلال / Reasoning modelsفيها كود / has codeالتعليقات مفتوحة / comments open
  8. 8الضبط الدقيق الفعّال / Efficient fine-tuningفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  9. 9هندسة الأوامر والسياق / Prompt and context engineeringفيها كود / has codeالتعليقات مفتوحة / comments open
  10. 10الاسترجاع المعزّز (RAG) من الصفر / RAG from scratchفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  11. 11RAG المتقدّم / Advanced RAGفيها كود / has codeالتعليقات مفتوحة / comments open
  12. 12الوكلاء والأدوات / Agents and toolsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  13. 13التقييم والأمان / Evaluation and safetyفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  14. 14مشروع ختامي: مساعد عربي / Capstone: an Arabic assistantفيها كود / has codeفيها صور / has picturesفيها نموذج / has a formالتعليقات مفتوحة / comments open

↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق↑ ↓ to move · Enter to open · Esc to close