编码器-解码器架构

使用 Keras 的机器翻译

Thushan Ganegedara

Data Scientist and Author

编码器-解码器模型

机器翻译模型

使用 Keras 的机器翻译

编码器

编码器

使用 Keras 的机器翻译

编码器与解码器

编码器 解码器

使用 Keras 的机器翻译

类比:编码器-解码器架构

编码器类比

解码器类比

使用 Keras 的机器翻译

句子反转——编码器-解码器模型

编码器解码器 文本反转

使用 Keras 的机器翻译

编写编码器

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 的机器翻译

编写编码器

onehot = words2onehot(["I", "like", "cats"], word2index)
context = encoder(onehot)
print(context)
[0, 1, 2]
使用 Keras 的机器翻译

编写解码器

  • 解码器:词ID → 反转ID → one-hot 向量
def decoder(context_vector):
  word_ids_rev = context_vector[::-1]
  onehot_rev = to_categorical(word_ids_rev, 3)
  return onehot_rev
  • 辅助函数:将 one-hot 向量转成人类可读的词
def onehot2words(onehot, index2word):
  ids = np.argmax(onehot, axis=1)
  return [index2word[id] for id in ids]
使用 Keras 的机器翻译

编写解码器

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

print(reversed_words)
['cats', 'like', 'I']
使用 Keras 的机器翻译

Vamos praticar!

使用 Keras 的机器翻译

Preparing Video For Download...