فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة Python
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: فجوة الـ train والـ test
Sample output (press Run to produce your own): train: 0.99 test: 0.73
المثال ٢: حجم الشبكة والـ validation
Sample output (press Run to produce your own): hidden 1 train 0.54 val 0.39 hidden 4 train 0.84 val 0.81 hidden 16 train 0.84 val 0.83 hidden 128 train 0.94 val 0.95
المثال ٣: الـ weight decay
Sample output (press Run to produce your own): alpha 0 train 0.99 test 0.73 alpha 0.01 train 0.99 test 0.77 alpha 0.1 train 0.97 test 0.8 alpha 1 train 0.84 test 0.83
اختبار قصير
اختبر فهمك
1. Low training error but much higher validation error signals:
2. The test set should be used:
3. Which one helps against overfitting?
4. Weight decay pushes the weights:
5. Underfitting means:
تمارين برمجة
جرّب بإيدك
التمرين 1
درّب MLP بـ hidden (1,) على make_moons (400, noise 0.2) واطبع دقة الـ train والـ test (underfitting).
Train an MLP with hidden (1,) on make_moons (400, noise 0.2). Print train and test accuracy (underfitting).
(output appears here)
from sklearn.datasets import make_moons from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier X, y = make_moons(400, noise=0.2, random_state=0) Xtr, Xte, ytr, yte = train_test_split(X, y, random_state=0) m = MLPClassifier((1,), max_iter=3000, random_state=0).fit(Xtr, ytr) print(round(m.score(Xtr, ytr), 2), round(m.score(Xte, yte), 2))
Output
0.51 0.47
التمرين 2
طبّق weight decay يدوي: w = w * (1 - lr * alpha) على w = [1, -2, 3] بـ lr = 0.1 و alpha = 0.5 لـ 10 خطوات واطبع w.
Apply manual weight decay w = w * (1 - lr * alpha) to w = [1, -2, 3], lr = 0.1, alpha = 0.5, for 10 steps. Print w.
(output appears here)
import numpy as np
w = np.array([1.0, -2.0, 3.0])
lr, alpha = 0.1, 0.5
for step in range(10):
w = w * (1 - lr * alpha)
print(np.round(w, 3))
Output
[ 0.599 -1.197 1.796]
مشروع صغير في آخر المحاضرة
اضبط حجم الشبكة والـ weight decay على الـ validation
الداتا: Breast Cancer Wisconsin (Diagnostic): 569 samples, 30 features, benign or malignant. Source: UCI Machine Learning Repository.
License / source: Ships with scikit-learn. Dataset page
الخطوات
- قسّم الداتا train / validation / test بنسب ٦٠/٢٠/٢٠ (random_state=0) واعمل standardization من الـ train.
- جرّب hidden (4,) و(64,) و(64, 64) مع alpha = 0 و1.
- اختار أحسن إعداد على الـ validation.
- اطبع دقة الـ test لإعداد واحد بس: اللي اخترته.
Expected output (press Run to produce your own): hidden (4,) alpha 0 val 0.956 hidden (4,) alpha 1 val 0.956 hidden (64,) alpha 0 val 0.956 hidden (64,) alpha 1 val 0.956 hidden (64, 64) alpha 0 val 0.956 hidden (64, 64) alpha 1 val 0.956 test accuracy of the chosen setting: 0.956
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, Generalization in Deep Learning and Weight Decay.