Generating translations with the NMT

Machine Translation with Keras

Thushan Ganegedara

Data Scientist and Author

Motivation

  • You have a trained model
  • Need to be able to assist humans on translation tasks
  • Test the model on unseen data

  • How?

    • Hold-out test set to evalute the model
    • You will test the model by asking it to predict translations for one sentence.
Machine Translation with Keras

Transforming the input

  • English sentence
en_st = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
  • Transform the encoder sentence
en_seq = sents2seqs('source', en_st, onehot=True, reverse=True)
print(np.argmax(en_seq, axis=-1)
English: ['the united states is sometimes chilly during december , 
           but it is sometimes freezing in june .']
Reversed sentence: ['june in freezing sometimes is it ...'] 
Reversed sequence: [[34   3  54       10        2  4  7 45  5 69 10  2 23 22  6]]
Machine Translation with Keras

Generating the translation

  • Generating a prediction

    fr_pred = model.predict(en_seq)
    
  • fr_pred.shape

    • [sentences, seq len, vocab size]
  • Getting the predicted classes
fr_seq = np.argmax(fr_pred, axis=-1)[0]
[[ 3  7 35 34  2 ...  5  4  4  0  0]] # <= fr_seq
  • fr_seq.shape
    • [num sentences, sequence len]

Machine Translation with Keras

Converting the prediction to a sentence

  • Converting the produced word IDs to a sentence using list comprehension
fr_sentence = ' '.join([fr_id2word[i] for i in fr_seq if i != 0])
English: the united states is sometimes chilly during december , but it is sometimes freezing in june .

French: les états unis est parfois froid en décembre mais il est parfois le gel en
French (Google Translate): les etats-unis sont parfois froids en décembre, mais parfois gelés en juin
Machine Translation with Keras

List comprehension in more detail

  • List comprehension
word_list = [fr_tok.index_word[i] for i in fr_seq if i != 0]
  • For loop
word_list = []
for i in fr_seq:
  if i != 0:
    word_list.append(fr_tok.index_word[i])
Machine Translation with Keras

Let's practice!

Machine Translation with Keras

Preparing Video For Download...