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

الفصل 5 (الجزء 1): Optimization

SGD, momentum and Adam

على طول: إزاي نوصل للقاع أسرع من الـ gradient descent العادي.

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

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

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

رسوم متحركة

SGD مقابل Momentum

أمثلة 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

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

اختبار قصير

اختبر فهمك

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)

التمرين 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)

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

قارن 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

الخطوات

  1. حمّل digits، اقسم ٨٠/٢٠، واقسم الـ pixels على 16.
  2. اكتب دالة بتحسب gradient الـ softmax regression.
  3. درّب ٦٠ خطوة بـ SGD (lr 0.5) وبـ momentum (lr 0.1, beta 0.9).
  4. اطبع الـ 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.