ส่วนที่ 2: ทำความเข้าใจโมเดล GRU
ในส่วนนี้จะได้เรียนรู้วิธีใช้โมเดล Keras ให้รับ batch ของ input ที่มีขนาดใดก็ได้ ความสามารถนี้มีประโยชน์หลายด้าน ตัวอย่างเช่น ช่วยให้กำหนดโมเดล Keras เพียงครั้งเดียว แล้วทดลองใช้ขนาด batch ที่แตกต่างกันในขั้นตอนการฝึกโมเดลได้โดยไม่ต้องแก้ไขโมเดลเลย
สำหรับแบบฝึกหัดนี้ ได้นำเข้า keras และ numpy (ในชื่อ np) ไว้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Translation ด้วย Keras
คำแนะนำการฝึกหัด
- กำหนด input layer ที่รับ batch ที่มีขนาดใดก็ได้ โดยมี sequence length เท่ากับ 3 และ input size เท่ากับ 4
- กำหนด GRU layer ที่มี 10 hidden unit เพื่อรับ input จากชั้นก่อนหน้าและสร้าง output
- กำหนด Model ชื่อ
modelโดยรับ input layer เป็น input และใช้ผลลัพธ์จาก GRU layer เป็น output โดยสามารถใช้ syntaxkeras.models.Model(<argument>=<value>)ในการกำหนดโมเดล - พยากรณ์ผลลัพธ์ของโมเดลสำหรับทั้ง
x1และx2
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Define an input layer
inp = keras.layers.____(____=(____))
# Define a GRU layer that takes in the input
gru_out = keras.layers.____(____)(____)
# Define a model that outputs the GRU output
____ = keras.models.____(inputs=____, outputs=____)
x1 = np.random.normal(size=(2,3,4))
x2 = np.random.normal(size=(5,3,4))
# Get the output of the model and print the result
y1 = ____.____(____)
y2 = ____.____(____)
print("shape (y1) = ", y1.shape, " shape (y2) = ", y2.shape)