فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة Python
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: التوقّع والـ MSE بالمصفوفات
Sample output (press Run to produce your own): y_hat: [ 3. 7. 11.] MSE : 18.666666666666668
المثال ٢: التدريب الـ vectorized
Sample output (press Run to produce your own): w = [ 1.99 -1.01 0.51] b = 4.0
المثال ٣: Mini-batch SGD
Sample output (press Run to produce your own): epoch 0 w = [ 1.87 -0.94 0.46] epoch 1 w = [ 1.99 -0.99 0.51] epoch 2 w = [ 1.99 -0.99 0.5 ] epoch 3 w = [ 1.99 -1. 0.51] epoch 4 w = [ 2. -0.98 0.5 ]
اختبار قصير
اختبر فهمك
1. In y_hat = X @ w + b, what does each row of X hold?
2. Why is vectorized code preferred?
3. Mini-batch SGD uses:
4. R2 close to 0 means:
5. Why standardize features before gradient descent?
تمارين برمجة
جرّب بإيدك
التمرين 1
احسب MSE بين التوقّعات y_hat = [2, 4, 6] والهدف y = [3, 4, 5].
Compute the MSE between y_hat = [2, 4, 6] and y = [3, 4, 5].
(output appears here)
import numpy as np y_hat = np.array([2.0, 4.0, 6.0]) y = np.array([3.0, 4.0, 5.0]) print(np.mean((y_hat - y) ** 2))
Output
0.6666666666666666
التمرين 2
درّب نموذج خطي vectorized على الداتا دي (y = 3*x + 1) بـ 300 خطوة و lr = 0.1 واطبع w و b.
Train a vectorized linear model on this data (y = 3*x + 1): 300 steps, lr = 0.1. Print w and b.
(output appears here)
import numpy as np
x = np.linspace(-1, 1, 50)
y = 3 * x + 1
w, b = 0.0, 0.0
for step in range(300):
err = w * x + b - y
w -= 0.1 * 2 * np.mean(err * x)
b -= 0.1 * 2 * np.mean(err)
print(round(w, 2), round(b, 2))
Output
3.0 1.0
مشروع صغير في آخر المحاضرة
توقّع تقدّم مرض السكر بـ نموذج خطي من الصفر
الداتا: Diabetes dataset: 442 patients, 10 features, target = disease progression. Source: Efron et al., 2004.
License / source: Ships with scikit-learn. Dataset page
الخطوات
- قسّم الداتا ٨٠/٢٠ (random_state=0) وعمل standardization بمتوسط وانحراف الـ training.
- درّب w (١٠ أوزان) و b بـ mini-batch SGD (batch = 32، lr = 0.01، ٥٠ epoch).
- احسب R2 على الـ test.
- قارن بـ LinearRegression من scikit-learn.
Expected output (press Run to produce your own): ours R2: 0.34 sklearn R2: 0.33
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, Linear Neural Networks for Regression.