開始使用免費開始

訓練模型與推論模型之間的連結

在這裡你要把訓練過的模型權重,轉移到推論模型上。於編碼器—解碼器模型中,有 3 個含有參數的層:

  • 編碼器的 GRU
  • 解碼器的 GRU
  • 解碼器的 Dense

其他層(例如 TimeDistributed)沒有任何參數,因此不需要複製權重。

在本練習中,你已獲得訓練好的編碼器 GRU 層(tr_en_gru)、訓練好的解碼器 GRUtr_de_gru),以及訓練好的 Dense 層(tr_de_dense)。你也能存取推論模型的所有層(包含編碼器),例如編碼器 GRU 層(en_gru)、解碼器 GRUde_gru)以及 Dense 層(de_dense)。

本練習屬於課程

使用 Keras 進行機器翻譯

檢視課程

練習說明

  • 載入訓練好的編碼器 GRU 層的權重。
  • 設定推論模型中編碼器 GRU 層的權重。
  • 載入訓練好的解碼器 GRU 層的權重,並在推論模型中設定這些權重。
  • 載入訓練好的解碼器 Dense 層的權重,並在推論模型中設定這些權重。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Load the weights to the encoder GRU from the trained model
en_gru_w = ____.get_weights()
# Set the weights of the encoder GRU of the inference model
en_gru.____(____)
# Load and set the weights to the decoder GRU
de_gru.____(tr_de_gru.____)
# Load and set the weights to the decoder Dense
____.set_weights(____.____)
編輯並執行程式碼