開始使用免費開始

第 1 部分:尋寶遊戲

你剛好抽中一趟全額招待的熱帶島嶼之旅。四處探險時,你發現了一張古老的藏寶圖,指向一筆巨大寶藏,上面還寫了幾段由 1 與 0 組成的祕密訊息。因為你剛修完這門課,你立刻看出那是一串 onehot 編碼的向量。你也很幸運地找到了字詞與索引的對照表,知道每個 ID 對應到哪個單字。

現在你需要解密這段祕密訊息,弄清楚地圖在說什麼。你拿到一個 treasure_map,它是一個由「句子數」×「單字數」×「onehot 向量長度」所組成的矩陣。你也拿到了一個 index2word Python 字典,用來把 ID 對應到單字。

本練習屬於課程

使用 Keras 進行機器翻譯

檢視課程

練習說明

  • 取得 treasure_map 中 onehot 編碼向量所對應的字詞 ID(onehot 向量的維度在「最後一個」維度)。
  • treasure_map 取出序列長度(也就是時間步數),指定給 seq_len
  • 取得第 i 個句子在第 t 個位置的字詞 ID。
  • wid 對應的字串單字(不是字詞 ID)附加到清單 words

動手互動練習

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

# Get the word IDs from the treasure map
word_ids = ____.____(____, axis=____)
# Get the sequence length from the treasure map
seq_len = treasure_map.shape[____]

for i in range(treasure_map.shape[0]):
	words = []
	for t in range(seq_len):
      	# Get the word ID for the i-th sentence and t-th position
	    wid = word_ids[i, ____]
	    if wid != 0:
          	# Append the word corresponding to wid
	        words.append(____[____])
	print("Instruction ", i+1, ": ", ' '.join(words))
編輯並執行程式碼