فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة Python
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: خطوة RNN واحدة
Sample output (press Run to produce your own): [ 0.572 0.197 -0.149]
المثال ٢: قراءة تسلسل كامل
Sample output (press Run to produce your own): 0.491 0.541 0.609 0.64 0.555
المثال ٣: الـ gradient بيتلاشى
Sample output (press Run to produce your own): scale 0.5 step 1 size 1.1256 scale 0.5 step 5 size 0.0235 scale 0.5 step 10 size 0.0007 scale 0.5 step 20 size 0.0 scale 1.5 step 1 size 3.7473 scale 1.5 step 5 size 9.1206 scale 1.5 step 10 size 50.1197 scale 1.5 step 20 size 4367.8465
اختبار قصير
اختبر فهمك
1. What does the hidden state hold?
2. Are the weights the same at every time step?
3. BPTT stands for:
4. Why does a plain RNN forget old inputs?
5. What helps with exploding gradients?
تمارين برمجة
جرّب بإيدك
التمرين 1
طبّق خطوة RNN: x = 1، h = 0.5، Wx = 0.8، Wh = 0.4 (أرقام عادية) واطبع tanh(x*Wx + h*Wh).
Apply one RNN step with scalars: x = 1, h = 0.5, Wx = 0.8, Wh = 0.4. Print tanh(x*Wx + h*Wh).
(output appears here)
import numpy as np x, h, Wx, Wh = 1.0, 0.5, 0.8, 0.4 print(round(np.tanh(x * Wx + h * Wh), 3))
Output
0.762
التمرين 2
كرّر الخطوة دي على الـ input [1, 0, 0] وابدأ بـ h = 0 واطبع الـ h بعد كل خطوة.
Repeat that step over the inputs [1, 0, 0], starting from h = 0. Print h after each step.
(output appears here)
import numpy as np
Wx, Wh = 0.8, 0.4
h = 0.0
for x in [1.0, 0.0, 0.0]:
h = np.tanh(x * Wx + h * Wh)
print(round(h, 3))
Output
0.664 0.26 0.103
مشروع صغير في آخر المحاضرة
قراءة الأرقام صف صف بـ RNN
الداتا: Optical Recognition of Handwritten Digits: 1797 images of 8x8 pixels. Source: UCI Machine Learning Repository.
License / source: Ships with scikit-learn. Dataset page
الخطوات
- حمّل digits واقسم ٨٠/٢٠. كل صورة ٨×٨ بنعتبرها ٨ خطوات، كل خطوة صف من ٨ أرقام.
- اعمل RNN بأوزان عشوائية ثابتة (hidden = 64) واقرا الصفوف بالترتيب.
- درّب LogisticRegression على آخر hidden state بس.
- قارن بمتوسط الصفوف (من غير ترتيب). المتوقع: RNN حوالي 0.875 ومتوسط الصفوف حوالي 0.48، يعني الترتيب فارق.
Expected output (press Run to produce your own): RNN last state : 0.875 mean of rows (no order): 0.481
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, Recurrent Neural Networks.