機器翻譯入門

使用 Keras 進行機器翻譯

Thushan Ganegedara

Data Scientist and Author

機器翻譯

多國語言的 Hello

使用 Keras 進行機器翻譯

機器翻譯

帶有 Google 圖示的 Hello 翻譯

使用 Keras 進行機器翻譯

課程大綱

  • 第 1 章-機器翻譯入門
  • 第 2 章-實作機器翻譯模型(encoder-decoder 架構)
  • 第 3 章-訓練模型並產生翻譯
  • 第 4 章-改進翻譯模型
使用 Keras 進行機器翻譯

資料集(英法句子語料)

  • 英文語料
new jersey is sometimes quiet during autumn , and it is snowy in april .
the united states is usually chilly during july , and it is usually freezing ...
california is usually quiet during march , and it is usually hot in june .
  • 法文語料
new jersey est parfois calme pendant l' automne , et il est neigeux en avril .
les états-unis est généralement froid en juillet , et il gèle habituellement ...
california est généralement calme en mars , et il est généralement chaud en juin .
1 https://github.com/udacity/deep-learning/tree/master/language-translation/data
使用 Keras 進行機器翻譯

機器翻譯-總覽

英譯法

使用 Keras 進行機器翻譯

機器翻譯-總覽

來源與目標語言術語

使用 Keras 進行機器翻譯

機器翻譯-總覽

機器翻譯模型

使用 Keras 進行機器翻譯

機器翻譯-總覽

機器翻譯模型

使用 Keras 進行機器翻譯

One-hot 編碼向量

  • 由 0/1 組成的向量
  • 向量長度由詞彙表大小決定
  • 詞彙表:資料集中不重複的詞彙集合

One-hot 向量

使用 Keras 進行機器翻譯

One-hot 編碼向量

包含詞與其對應索引的對應表

word2index = {"I":0, "like": 1, "cats": 2}

將詞轉為 ID 或索引

words = ["I", "like", "cats"]
word_ids = [word2index[w] for w in words]
print(word_ids)
[0, 1, 2]
使用 Keras 進行機器翻譯

One-hot 編碼向量

未指定輸出向量長度的 one-hot 編碼

onehot_1 = to_categorical(word_ids)
print([(w,ohe.tolist()) for w,ohe in zip(words, onehot_1)])
[('I', [1.0, 0.0, 0.0]), ('like', [0.0, 1.0, 0.0]), ('cats', [0.0, 0.0, 1.0])]

有指定輸出向量長度的 one-hot 編碼

onehot_2 = to_categorical(word_ids, num_classes=5)
print([(w,ohe.tolist()) for w,ohe in zip(words, onehot_2)])
[('I', [1.0, 0.0, 0.0, 0.0, 0.0]), ('like', [0.0, 1.0, 0.0, 0.0, 0.0]), 
('cats', [0.0, 0.0, 1.0, 0.0, 0.0])]
使用 Keras 進行機器翻譯

一起來練習吧!

使用 Keras 進行機器翻譯

Preparing Video For Download...