ส่วนที่ 2: โมเดลกลับข้อความ - Encoder
ต่อไปจะมาสร้างส่วนที่เหลือของ encoder ในโมเดลกลับข้อความ encoder จะรับ one-hot vectors ที่ผลิตโดยฟังก์ชัน words2onehot() ที่ได้สร้างไว้ก่อนหน้านี้
ในแบบฝึกหัดนี้ จะเป็นการสร้างฟังก์ชัน encoder() ซึ่งรับ one-hot vectors และแปลงให้เป็นรายการของ word ID
ฟังก์ชัน words2onehot() และ dictionary word2index (ที่มีคำว่า We, like และ dogs) ได้เตรียมไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Translation ด้วย Keras
คำแนะนำการฝึกหัด
- แปลง
onehotให้เป็น array ของ word ID โดยใช้ฟังก์ชันnp.argmax()แล้ว return ค่า word ID ออกมา - กำหนดรายการคำที่ประกอบด้วย
We,like,dogs - แปลงรายการคำให้เป็น onehot vectors โดยใช้ฟังก์ชัน
words2onehot()โดยwords2onehot()รับรายการคำและ Python dictionary เป็นอาร์กิวเมนต์ - รับ context vector ของ onehot vectors โดยใช้ฟังก์ชัน
encoder()
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
def encoder(onehot):
# Get word IDs from onehot vectors and return the IDs
word_ids = np.____(____, axis=____)
return ____
# Define "We like dogs" as words
words = ____
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Get the context vector by using the encoder function
context = encoder(____)
print(context)