Архітектура encoder–decoder

Machine Translation з Keras

Thushan Ganegedara

Data Scientist and Author

Модель encoder–decoder

Модель машинного перекладу

Machine Translation з Keras

Encoder

Encoder

Machine Translation з Keras

Encoder і Decoder

Encoder decoder

Machine Translation з Keras

Аналогія: архітектура encoder–decoder

Аналогія encoder

Аналогія decoder

Machine Translation з Keras

Реверс речень — модель encoder–decoder

Текстове реверсування encoder–decoder

Machine Translation з Keras

Пишемо encoder

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
Machine Translation з Keras

Пишемо encoder

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

Пишемо decoder

  • Decoder: Word IDs → реверс 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]
Machine Translation з Keras

Пишемо decoder

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

print(reversed_words)
['cats', 'like', 'I']
Machine Translation з Keras

Давайте потренуємось!

Machine Translation з Keras

Preparing Video For Download...