エンコーダ・デコーダのアーキテクチャ

Kerasで学ぶMachine Translation

Thushan Ganegedara

Data Scientist and Author

エンコーダ・デコーダモデル

機械翻訳モデル

Kerasで学ぶMachine Translation

エンコーダ

エンコーダ

Kerasで学ぶMachine Translation

エンコーダとデコーダ

エンコーダとデコーダ

Kerasで学ぶMachine Translation

たとえ:エンコーダ・デコーダのアーキテクチャ

エンコーダのたとえ

デコーダのたとえ

Kerasで学ぶMachine Translation

文の反転:エンコーダ・デコーダモデル

エンコーダ・デコーダで文を反転

Kerasで学ぶMachine Translation

エンコーダの作成

def words2onehot(word_list, word2index):
  word_ids = [word2index[w] for w in word_list]
  onehot = to_categorical(word_ids, 3)
  return onehot
def encoder(onehot):
  word_ids = np.argmax(onehot, axis=1)
  return word_ids
Kerasで学ぶMachine Translation

エンコーダの作成

onehot = words2onehot(["I", "like", "cats"], word2index)
context = encoder(onehot)
print(context)
[0, 1, 2]
Kerasで学ぶMachine Translation

デコーダの作成

  • デコーダ:単語ID → 反転 → ワンホット
def decoder(context_vector):
  word_ids_rev = context_vector[::-1]
  onehot_rev = to_categorical(word_ids_rev, 3)
  return onehot_rev
  • 補助関数:ワンホットを単語に変換
def onehot2words(onehot, index2word):
  ids = np.argmax(onehot, axis=1)
  return [index2word[id] for id in ids]
Kerasで学ぶMachine Translation

デコーダの作成

onehot_rev = decoder(context)
reversed_words = onehot2words(onehot_rev, index2word)

print(reversed_words)
['cats', 'like', 'I']
Kerasで学ぶMachine Translation

Passons à la pratique !

Kerasで学ぶMachine Translation

Preparing Video For Download...