فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة Python
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: SGD و Momentum على وادي ضيق
Sample output (press Run to produce your own): SGD distance to minimum: 3.029 momentum distance to minimum: 0.018
المثال ٢: Adam من الصفر
Sample output (press Run to produce your own): Adam distance to minimum: 0.0
المثال ٣: مقارنة الـ optimizers على digits
Sample output (press Run to produce your own): sgd test accuracy: 0.597 sgd+momentum test accuracy: 0.889 adam test accuracy: 0.919
اختبار قصير
اختبر فهمك
1. Why does plain SGD zigzag in a narrow valley?
2. Momentum keeps:
3. Adam gives each weight:
4. A common first choice of optimizer is:
5. Mini-batch noise is:
تمارين برمجة
جرّب بإيدك
التمرين 1
طبّق 20 خطوة SGD على الدالة f(w) = w^2 من w = 5 بـ lr = 0.1 واطبع w.
Run 20 steps of SGD on f(w) = w^2 starting at w = 5 with lr = 0.1. Print w.
(output appears here)
w = 5.0
lr = 0.1
for step in range(20):
w -= lr * 2 * w
print(round(w, 4))
Output
0.0576
التمرين 2
نفس الدالة بـ momentum (beta = 0.9, lr = 0.02) لـ 50 خطوة، واطبع w.
Same function with momentum (beta = 0.9, lr = 0.02) for 50 steps. Print w.
(output appears here)
w, v = 5.0, 0.0
for step in range(50):
v = 0.9 * v + 2 * w
w -= 0.02 * v
print(round(w, 4))
Output
-0.3407
مشروع صغير في آخر المحاضرة
قارن SGD و Momentum و Adam على الأرقام من الصفر
الداتا: Optical Recognition of Handwritten Digits: 1797 images of 8x8 pixels. Source: UCI Machine Learning Repository.
License / source: Ships with scikit-learn. Dataset page
الخطوات
- حمّل digits، اقسم ٨٠/٢٠، واقسم الـ pixels على 16.
- اكتب دالة بتحسب gradient الـ softmax regression.
- درّب ٦٠ خطوة بـ SGD (lr 0.5) وبـ momentum (lr 0.1, beta 0.9).
- اطبع الـ loss النهائي ودقة الـ test لكل واحد.
Expected output (press Run to produce your own): SGD loss 0.561 test accuracy: 0.919 momentum loss 0.338 test accuracy: 0.931
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, Optimization Algorithms.