Get startedGet started for free

Training the model

Did you know that in 2017, Google translate served more than 500 million users daily?

Here, you will train your first Teacher Forced model. Teacher Forcing is commonly used in sequence to sequence models like your neural machine translator to achieve better performance.

You will be provided with the sents2seqs() function, English sentences en_text and French sentences fr_text.

This exercise is part of the course

Machine Translation with Keras

View Course

Exercise instructions

  • Get the decoder input which contains the onehot encoded sequences of French words (except for the last word in every sequence).
  • Get the decoder output which contains the onehot encoded sequences of French words (except for the first word in every sequence).
  • Train the model on a single batch of data.
  • Obtain the evaluation metrics (loss and accuracy) for training data en_x, de_x and de_y.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

n_epochs, bsize = 3, 250

for ei in range(n_epochs):
  for i in range(0,data_size,bsize):
    en_x = sents2seqs('source', en_text[i:i+bsize], onehot=True, reverse=True)
    de_xy = sents2seqs('target', fr_text[i:i+bsize], onehot=True)
    # Separate the decoder inputs from de_xy
    de_x = ____[:,____,:]
    # Separate the decoder outputs from de_xy
    de_y = ____[____]
    # Train the model on a single batch of data    
    nmt_tf.____([____,____], ____)    
    # Obtain the eval metrics for the training data
    res = _____.evaluate([____,____], ____, batch_size=bsize, verbose=0)
    print("{} => Train Loss:{}, Train Acc: {}".format(ei+1,res[0], res[1]*100.0))  
Edit and Run Code