الرموز والتضمينات / Tokens and embeddings

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

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

أهداف الدرس

  1. أن تفهم كيف يتحوّل النص إلى أرقام.
  2. أن تعرف لماذا تكلّف العربية رموزاً أكثر أحياناً.

لا يقرأ النموذج حروفاً ولا كلمات، بل رموزاً: قطعاً يقسّم إليها النصَّ مقطِّعٌ تعلّم الأجزاء الأكثر تكراراً. ثم يتحوّل كل رمز إلى تضمين: قائمة أرقام تمثّل معناه، فتتقارب المعاني المتشابهة.

من النص إلى متجهات
من النص إلى متجهات

نقيس تقارب معنيين بـ«تشابه جيب التمام»: قريب من 1 للمعنيين المتشابهين، وأقل للمختلفين.

Python
import numpy as np

def cosine(a, b):
    return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))

# Toy 4-number "meanings"
teacher  = np.array([0.9, 0.1, 0.8, 0.2])
lecturer = np.array([0.85, 0.15, 0.75, 0.3])
banana   = np.array([0.1, 0.9, 0.05, 0.7])

print(round(cosine(teacher, lecturer), 2))  # 0.99 — similar meaning
print(round(cosine(teacher, banana), 2))    # 0.26 — unrelated

النموذج الذي تدرّب على العربية قليلاً يقطّع الكلمة العربية إلى رموز كثيرة، فتصير الإجابة أبطأ وأغلى وأضعف. اختر نموذجاً يحسن العربية.

تمرين

لماذا نستعمل التضمينات في البحث بدل مطابقة الكلمات؟

الإجابة

لأنها تجد المعنى نفسه بكلمات مختلفة: «موعد التسجيل» تجد «متى يبدأ التسجيل».

Level: Beginner · Lesson 2 of 14

Lesson goals

  1. Understand how text becomes numbers.
  2. Know why Arabic sometimes costs more tokens.

A model reads neither letters nor words but tokens: pieces a tokenizer cuts text into, learned from the most frequent fragments. Each token then becomes an embedding: a list of numbers standing for its meaning, so similar meanings sit close together.

From text to vectors
From text to vectors

We measure how close two meanings are with «cosine similarity»: near 1 for similar meanings, lower for different ones.

Python
import numpy as np

def cosine(a, b):
    return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))

# Toy 4-number "meanings"
teacher  = np.array([0.9, 0.1, 0.8, 0.2])
lecturer = np.array([0.85, 0.15, 0.75, 0.3])
banana   = np.array([0.1, 0.9, 0.05, 0.7])

print(round(cosine(teacher, lecturer), 2))  # 0.99 — similar meaning
print(round(cosine(teacher, banana), 2))    # 0.26 — unrelated

A model trained on little Arabic cuts an Arabic word into many tokens, so answers get slower, costlier and weaker. Choose a model that is good at Arabic.

Exercise

Why use embeddings for search instead of matching words?

Answer

They find the same meaning in different words: «registration date» finds «when does registration 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