فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة 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)
اختبار قصير
اختبر فهمك
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)
import numpy as np x = np.array([2.0, 4.0, 6.0, 8.0]) print(np.round((x - x.mean()) / np.sqrt(x.var() + 1e-5), 2))
Output
[-1.34 -0.45 0.45 1.34]
التمرين 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)
import numpy as np x = np.array([1.0, 2.0]) F = np.array([0.1, -0.2]) print(x + F)
Output
[1.1 1.8]
مشروع صغير في آخر المحاضرة
أثر الـ 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
الخطوات
- استخدم نفس الـ attention على صفوف digits.
- مرة من غير أي معلومة عن الموضع، ومرة بإضافة vector موضع لكل صف.
- درّب LogisticRegression على المخرجات في الحالتين.
- قارن. المتوقع: من غير مواضع حوالي 0.77 ومعاها حوالي 0.93. (ملاحظة: vectors المواضع هنا عشوائية ثابتة مش متعلّمة).
Expected output (press Run to produce your own): attention, no positions : 0.769 attention + positions : 0.931
مشروع نهاية الفصل
ملف المشروع العملي للفصل 9
ملف 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.