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

الفصل 2 (الجزء 1): Linear Neural Networks

Linear regression from scratch, vectorized

على طول: النموذج الخطي كشبكة بـ neuron واحد، وإزاي نكتبه vectorized.

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

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

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

رسوم متحركة

الـ gradient descent على الـ loss
النموذج الخطي = neuron من غير activation

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

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

اختبار قصير

اختبر فهمك

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)

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

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

توقّع تقدّم مرض السكر بـ نموذج خطي من الصفر

الداتا: Diabetes dataset: 442 patients, 10 features, target = disease progression. Source: Efron et al., 2004.

License / source: Ships with scikit-learn. Dataset page

الخطوات

  1. قسّم الداتا ٨٠/٢٠ (random_state=0) وعمل standardization بمتوسط وانحراف الـ training.
  2. درّب w (١٠ أوزان) و b بـ mini-batch SGD (batch = 32، lr = 0.01، ٥٠ epoch).
  3. احسب R2 على الـ test.
  4. قارن بـ 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.