เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

กำหนด decoder

ในแบบฝึกหัดนี้ คุณจะสร้าง decoder และกำหนดโมเดลแบบครบวงจรที่รับ input จาก encoder ไปจนถึง output ของ GRU ใน decoder โดย decoder ใช้โครงสร้างโมเดลเดียวกับ encoder แต่มีความแตกต่างในส่วนของ input และ state ที่ป้อนเข้าไป ตัวอย่างเช่น decoder รับ context vector ที่ผลิตโดย encoder เป็น input พร้อมกับ initial state ด้วย ทั้งนี้ เราจะใช้คำนำหน้า en (เช่น en_gru) เพื่อระบุสิ่งที่เกี่ยวข้องกับ encoder และ de สำหรับสิ่งที่เกี่ยวข้องกับ decoder (เช่น de_gru)

ในการสร้าง decoder จะใช้เลเยอร์ RepeatVector และ GRU

สำหรับแบบฝึกหัดนี้ โมเดล encoder และเลเยอร์ต่าง ๆ ที่สร้างไว้แล้วจะถูกเตรียมให้พร้อมใช้งาน เช่น input ของ encoder จะอยู่ในรูปแบบ en_inputs และ context vector จะอยู่ใน en_state นอกจากนี้ ออบเจกต์ GRU และ Model ได้ถูก import ไว้เรียบร้อยแล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Machine Translation ด้วย Keras

ดูคอร์ส

คำแนะนำการฝึกหัด

  • กำหนดเลเยอร์ RepeatVector ที่รับ en_state เป็น input และทำซ้ำ fr_len ครั้ง
  • กำหนดเลเยอร์ GRU ชื่อ decoder_gru ซึ่งมี hidden unit เท่ากับ hsize และคืนค่า output ทั้งหมด
  • รับ output จากเลเยอร์ decoder_gru โดยป้อน de_inputs เป็น input และ en_state เป็น initial state ของ decoder
  • กำหนดโมเดลที่รับ en_inputs เป็น input และ gru_outputs เป็น output

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

from tensorflow.keras.layers import RepeatVector

hsize = 48
fr_len = 20
# Define a RepeatVector layer
de_inputs = ____(____)(____)
# Define a GRU model that returns all outputs
decoder_gru = ____(____, ____=____)
# Get the outputs of the decoder
gru_outputs = ____(____, initial_state=____)
# Define a model with the correct inputs and outputs
enc_dec = ____(inputs=____, outputs=____)
enc_dec.summary()
แก้ไขและรันโค้ด