開始使用免費開始

第 1 部分:定義完整模型

在這裡,你要實作編碼器—解碼器模型最後幾個層。你會使用 DenseTimeDistributed 層,來取得模型的最終預測(也就是法文單字的機率分佈)。

我們已提供你目前為止實作好的編碼器與解碼器(不含最上層)。解碼器 GRU 層的輸出 de_out 也已提供。我們用前綴 en(例如 en_gru)標示與編碼器相關的物件,用 de 標示與解碼器相關的物件(例如 de_gru)。

本練習屬於課程

使用 Keras 進行機器翻譯

檢視課程

練習說明

  • 從 Keras 匯入 DenseTimeDistributed 層。
  • 定義一個使用 softmax 啟用函式、輸出維度為 fr_vocabDense 層。
  • 將該 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)
編輯並執行程式碼