الرئيسية / مقرر Deep Learning / الفصل 9 الجزء 2

الفصل 9 (الجزء 2): Transformers

The Transformer block and positions

على طول: البلوك اللي بيتبني منه كل نموذج لغة كبير النهارده.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

الـ residual حوالين الـ attention

أمثلة Python

أمثلة قصيرة وبسيطة

الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.

المثال ١: Layer norm

Sample output (press Run to produce your own):
[[-1.34 -0.45  0.45  1.34]
 [-1.34 -0.45  0.45  1.34]]

المثال ٢: Positional encoding

Sample output (press Run to produce your own):
[[ 0.    0.    1.    1.  ]
 [ 0.84  0.01  0.54  1.  ]
 [ 0.91  0.02 -0.42  1.  ]
 [ 0.14  0.03 -0.99  1.  ]]

المثال ٣: بلوك كامل

Sample output (press Run to produce your own):
output shape (5, 8)

افتح الفصل المقابل في كتاب d2l.ai

اختبار قصير

اختبر فهمك

1. Layer norm normalizes over:

2. Why add positional encoding?

3. A decoder uses a mask so that:

4. GPT-style models are:

5. A Transformer block has attention followed by:

تمارين برمجة

جرّب بإيدك

التمرين 1

طبّق layer norm على token واحد [2, 4, 6, 8] واطبع الناتج.

Apply layer norm to one token [2, 4, 6, 8] and print the result.

(output appears here)

التمرين 2

اعمل residual: x = [1, 2]، F(x) = [0.1, -0.2]. اطبع x + F(x).

Residual step: x = [1, 2] and F(x) = [0.1, -0.2]. Print x + F(x).

(output appears here)

مشروع صغير في آخر المحاضرة

أثر الـ positional encoding

الداتا: Optical Recognition of Handwritten Digits: 1797 images of 8x8 pixels. Source: UCI Machine Learning Repository.

License / source: Ships with scikit-learn. Dataset page

الخطوات

  1. استخدم نفس الـ attention على صفوف digits.
  2. مرة من غير أي معلومة عن الموضع، ومرة بإضافة vector موضع لكل صف.
  3. درّب LogisticRegression على المخرجات في الحالتين.
  4. قارن. المتوقع: من غير مواضع حوالي 0.77 ومعاها حوالي 0.93. (ملاحظة: vectors المواضع هنا عشوائية ثابتة مش متعلّمة).
Expected output (press Run to produce your own):
attention, no positions  : 0.769
attention + positions  : 0.931

مشروع نهاية الفصل

ملف المشروع العملي للفصل 9

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

Credits: the topic order follows the book "Dive into Deep Learning" (d2l.ai) by Zhang, Lipton, Li and Smola, whose text is licensed CC BY-SA 4.0. The explanations, diagrams and code in this lecture are our own and are not copied from the book. Reference: d2l.ai, The Transformer Architecture.