第 1 部分:定義完整模型
在這裡,你要實作編碼器—解碼器模型最後幾個層。你會使用 Dense 與 TimeDistributed 層,來取得模型的最終預測(也就是法文單字的機率分佈)。
我們已提供你目前為止實作好的編碼器與解碼器(不含最上層)。解碼器 GRU 層的輸出 de_out 也已提供。我們用前綴 en(例如 en_gru)標示與編碼器相關的物件,用 de 標示與解碼器相關的物件(例如 de_gru)。
本練習屬於課程
使用 Keras 進行機器翻譯
練習說明
- 從 Keras 匯入
Dense與TimeDistributed層。 - 定義一個使用
softmax啟用函式、輸出維度為fr_vocab的Dense層。 - 將該
Dense層包在TimeDistributed層中。 - 將
de_out傳入de_dense_time層,以取得模型的最終預測。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import Dense and TimeDistributed layers
from tensorflow.keras.____ import ____, ____
# Define a softmax dense layer that has fr_vocab outputs
de_dense = ____(____, ____)
# Wrap the dense layer in a TimeDistributed layer
de_dense_time = ____(de_dense)
# Get the final prediction of the model
de_pred = ____(de_out)
print("Prediction shape: ", de_pred.shape)