1. Generating translations with the NMT
In this lesson you will be generating translations from the model trained in the previous exercises.
2. Motivation
You have a model that has been trained and validated but how do you make sure it can function as an useful daily translator and translate previously unseen English sentences to French?
The best way would be to hold out a test set in addition to the train and validation data and measure the accuracy on that set.
Here, you will be testing the model only on one test sentence and visually inspecting it.
3. Transforming the input
Let's take this sentence as an example.
The first thing you will need to do is apply the exact same transformation to the source sentence you applied during training. That is you will convert English sentences to reversed onehot encoded inputs.
You can then print the word IDs and do a quick sanity check. The Keras tokenizer applies word IDs based on their frequency in the text. Therefore more frequent words like "it", "the", "is" will have smaller IDs. If you look at the positions those small word IDs appear at, they align with the positions of the words in the reversed English sentence.
4. Generating the translation
With the English sentence converted to an onehot encoded sequence, you can use the model dot predict function to obtain the French translation.
However, as you saw earlier, the output of the model will be a probability distribution over French vocabulary for each decoder position.
Therefore, to get the French word IDs you will need to use np dot argmax function over the last axis of fr_pred, which will give the sequence of IDs.
5. Converting the prediction to a sentence
To convert the sequence IDs to actual words, you can use list comprehension. As you can see, it looks like a complex line of code. However all it does is, take all the ids in fr_seq which are not zeros and convert each ID to a word using the tokenizer's index_word attribute. Don't worry if you did not fully understand. We will discuss this in detail.
Let's first take a look at the translations produced by our model.
This is what the model produced as the translation. This is a model that has been trained on 100,000 sentences and validated using more than 35000 sentences. This model achieved around 90% accuracy on the validation set.
If you compare this to what Google translate produces, it looks very similar.
6. List comprehension in more detail
Let's now look at list comprehension in more detail. List comprehension is an alternative to using for loops and actually are more efficient than using for loops. This is what you are using to obtain the sentences.
What it does can be broken into several lines of code as follows. It is going through all the sequences in the French word IDs and if the ID is not zero, the ID is converted to a word using the index_word attribute of the Tokenizer and added to a list.
7. Let's practice!
So far you learned to train a machine translation model and validate the model. Now you know how to use the trained model and generate translations. Let's practice!