التعلّم من التفضيلات / Learning from preferences

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

المستوى: متوسط · الدرس 6 من 14

أهداف الدرس

  1. أن تفهم RLHF وDPO.
  2. أن تعرف لماذا تجعل التفضيلات الإجابات أفضل.

بعد الضبط بالتعليمات نعرض إجابتين للسؤال نفسه ونقول أيّهما أفضل. يتعلّم النموذج من آلاف المقارنات أن يميل إلى الإجابات الأنفع والأصدق والأكثر أماناً.

طريقتان للتعلّم من التفضيلات
طريقتان للتعلّم من التفضيلات
Python
import numpy as np

def dpo_loss(lp_chosen, lp_rejected, ref_chosen, ref_rejected, beta=0.1):
    # How much more the model prefers the chosen answer than the reference model does
    margin = (lp_chosen - ref_chosen) - (lp_rejected - ref_rejected)
    return float(np.log1p(np.exp(-beta * margin)))   # = -log(sigmoid(beta * margin))

print(round(dpo_loss(-12.0, -15.0, -13.0, -14.0), 4))  # 0.5981
RLHFDPO
نموذج مكافأةنعملا
التعقيدأعلىأبسط
الشيوع اليومفي المختبرات الكبيرةواسع، ومفتوح المصدر

يمكن أن يأتي التفضيل من نموذج آخر يحكم وفق مبادئ مكتوبة بدل البشر وحدهم؛ وهذا يوسّع البيانات ويثبّت القيم.

تمرين

أيّ الإجابتين تفضّل لطالب سأل «كيف أحسّن معدّلي؟»: نصيحة عامة أم خطوات محدّدة بمواعيد؟ ولماذا؟

الإجابة

الخطوات المحدّدة: أنفع وقابلة للتنفيذ. هذه هي الإشارة التي يتعلّمها النموذج من التفضيلات.

Level: Intermediate · Lesson 6 of 14

Lesson goals

  1. Understand RLHF and DPO.
  2. Know why preferences make answers better.

After instruction tuning we show two answers to the same question and say which is better. From thousands of comparisons the model learns to lean towards answers that are more helpful, honest and safe.

Two ways to learn from preferences
Two ways to learn from preferences
Python
import numpy as np

def dpo_loss(lp_chosen, lp_rejected, ref_chosen, ref_rejected, beta=0.1):
    # How much more the model prefers the chosen answer than the reference model does
    margin = (lp_chosen - ref_chosen) - (lp_rejected - ref_rejected)
    return float(np.log1p(np.exp(-beta * margin)))   # = -log(sigmoid(beta * margin))

print(round(dpo_loss(-12.0, -15.0, -13.0, -14.0), 4))  # 0.5981
RLHFDPO
Reward modelYesNo
ComplexityHigherSimpler
Common todayIn large labsWidespread, open source

Preferences can come from another model judging by written principles, not only from people; that scales data and keeps values steady.

Exercise

Which do you prefer for «how do I raise my GPA?»: general advice or concrete steps with dates? Why?

Answer

Concrete steps: more useful and actionable. That is the signal a model learns from preferences.

التعليقات / 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