開始使用免費開始

NMT 範例

這個練習延續你在課程一開始搶先看的 NMT(神經機器翻譯)。你會繼續把葡萄牙語的短句翻成英文。

一些範例句子已放在變數 sentences 中,並已印在主控台上。

另外,變數 model 中提供了一個已訓練完成的模型,你會使用兩個自訂函式來簡化部分步驟:

  • encode_sequences():把文字轉成數值索引序列並進行補齊(padding)。
  • translate_many():使用預先訓練的模型,將多個葡萄牙語句子翻譯成英文。稍後你會自己實作這個函式。

想了解這些函式的更多細節,請使用 help()。套件 pandas 已以 pd 載入。

本練習屬於課程

使用 Keras 建立語言模型的循環神經網路(RNN)

檢視課程

練習說明

  • 使用 encode_sequences() 對文字做前處理,並把結果存到變數 X
  • 使用 translate_many(),傳入 X 參數,翻譯 sentences
  • 建立一個 pd.DataFrame(),把原始與翻譯後的串列作為欄位。
  • 印出資料框。

動手互動練習

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

# Transform text into sequence of indexes and pad
X = ____(sentences)

# Print the sequences of indexes
print(X)

# Translate the sentences
translated = translate_many(model, ____)

# Create pandas DataFrame with original and translated
df = pd.DataFrame({'Original': ____, 'Translated': ____})

# Print the DataFrame
print(____)
編輯並執行程式碼