第 2 部分:定義完整模型
你知道嗎?僅僅是英翻法這個任務,訓練一個 Google Neural Machine Translator 的變體,就花了大約 6 天與 96 個 GPU。
在這個練習中,你會定義一個類似、但簡化許多的、以編碼器—解碼器為基礎的神經機器翻譯模型。你將使用先前定義的輸入與輸出,建立一個 Keras Model 物件,並以指定的損失函式與最佳化器來編譯模型。
這裡已提供你先前定義的 en_inputs(編碼器輸入層)、en_out 與 en_state(編碼器 GRU 輸出)、de_out(解碼器 GRU 輸出),以及 de_pred(解碼器預測)。
本練習屬於課程
使用 Keras 進行機器翻譯
練習說明
- 定義一個 Keras
Model,將en_inputs作為輸入,並將解碼器的預測(de_pred)作為輸出。 - 呼叫
<model>.compile編譯模型,使用'adam'最佳化器、交叉熵損失,以及以準確率(acc)作為評估指標。 - 列印模型摘要。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from tensorflow.keras.models import Model
# Define a model with encoder input and decoder output
nmt = ____(____=____, outputs=____)
# Compile the model with an optimizer and a loss
nmt.____(optimizer=____, ____='categorical_crossentropy', metrics=[____])
# View the summary of the model
nmt.____()