Question: Q 3 . Neural Machine Translation: Code [ 2 0 ] In this question, you will learn to implement the Neural Machine Translation task using
Q Neural Machine Translation: Code
In this question, you will learn to implement the Neural Machine Translation task using a sequencetosequence model with attention.
Load WMT data from Huggingface. Use deen German to English data configuration. Note that this will need a lot of space approximately MB for the data and a few minutes for generating the traintest split.
The training dataset contains a dictionary with languages de and en as keys and the respective sentences as values. Create a DataFrame with columns 'German' and 'English' and the respective sentences. Build two separate vocabularies for German and English words and maintain a dictionary for German words with the word as key corresponding to the index and vice versa as in Q step Add and tags at the start and the end of each English sentence and add the tokens to the vocabulary as well.
With the dictionary created above, convert the German sentence to the array of indexes of each word in the sentence. eg "I learn NLP might be converted to where in the dictionary, i is mapped to index 'learn' is mapped to and n l p is mapped to Find the maximum length of the sentences in the German corpus. Pad each array with such that the length of each index vector calculated above will be of the same length. For example, if the maximum length of a sequence in the dataset is I learn NLP will be converted to
Convert each word in the English vocabulary to a onehot encoded vector of length equal to the number of words in the vocabulary including the start and the end tag Perform steps and step on the test set and validation set as well.
Build an Encoder with an Embedding layer, LSTM layers and an attention layer. For Decoder, use LSTMs and fully connected layers. Use Softmax activation for decoder output.
Train the model using Cross entropy loss, an optimizer of your choice use of adam is recommended and epochs. You are free to vary the number of epochs to boost performance.
To generate the translation of the given German sentence from the trained model, convert the sentence to the index vector, feed it to the model and convert the output to the English word with the highest Softmax probability. Terminate the process once you encounter token.
With the help of nltktranslate.bleuscore.sentencebleu find the average BLEU score on the test set, which measures the similarity of the machinetranslated text to a set of reference translations.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
