開始使用免費開始

第 2 部分:文字反轉模型-編碼器

現在你要完成文字反轉模型編碼器的其餘部分。編碼器會使用你先前實作的 words2onehot() 函式所產生的 one-hot 向量作為輸入。

在這裡你會實作 encoder() 函式。encoder() 函式接收一組 one-hot 向量,並將它們轉換為字詞 ID 的清單。

在本練習中,已提供 words2onehot() 函式與 word2index 字典(包含單字 Welikedogs)。

本練習屬於課程

使用 Keras 進行機器翻譯

檢視課程

練習說明

  • 使用 np.argmax() 函式將 onehot 轉換為字詞 ID 的陣列,並回傳這些 ID。
  • 建立一個包含 Welikedogs 的單字清單。
  • 使用 words2onehot() 函式把該單字清單轉換為 one-hot 向量。請記得,words2onehot() 的引數是一個單字清單與一個 Python 字典。
  • 使用 encoder() 函式對這些 one-hot 向量取得情境向量(context vector)。

動手互動練習

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

def encoder(onehot):
  # Get word IDs from onehot vectors and return the IDs
  word_ids = np.____(____, axis=____)
  return ____

# Define "We like dogs" as words
words = ____
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Get the context vector by using the encoder function
context = encoder(____)
print(context)
編輯並執行程式碼