資料前處理
你現在需要為新的模型處理資料。這個模型有兩個輸入與一個輸出。兩個輸入分別是:獨熱編碼的一組英文單字,以及去除最後一個單字的獨熱編碼法文單字。
輸出則是去除第一個單字的獨熱編碼法文單字。也就是說,在解碼器中,每個輸入的法文字,都對應到一個輸出,亦即下一個單字。這裡你會學到如何實作這件事。
你已獲得 sents2seqs() 函式、en_text 與 fr_text。
本練習屬於課程
使用 Keras 進行機器翻譯
練習說明
- 使用
sents2seqs()函式(啟用 onehot 編碼並反轉)取得一批編碼器輸入(從i到i+bsize)。 - 使用
sents2seqs()函式(啟用 onehot 編碼)取得一批解碼器的輸入與輸出(從i到i+bsize)。 - 針對時間維度從
de_xy切片,將解碼器輸入(所有法文字,除了最後一個)分離出來。 - 將解碼器輸出(所有法文字,除了第一個)從
de_xy分離出來。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
bsize = 250
for i in range(0, len(en_text), bsize):
# Get the encoder inputs using the sents2seqs() function
en_x = ____('source', ____[____:____], onehot=True, reverse=____)
# Get the decoder inputs/outputs using the sents2seqs() function
de_xy = sents2seqs('target', ____[____:____], onehot=True)
# Separate the decoder inputs from de_xy
de_x = de_xy[:,____,:]
# Separate the decoder outputs from de_xy
de_y = de_xy[:,____,:]
print("Data from ", i, " to ", i+bsize)
print("\tnp.argmax() => en_x[0]: ", np.argmax(en_x[0], axis=-1))
print("\tnp.argmax() => de_x[0]: ", np.argmax(de_x[0], axis=-1))
print("\tnp.argmax() => de_y[0]: ", np.argmax(de_y[0], axis=-1))