Generating translations
You will now be generating French translations using an inference model trained using Teacher Forcing.
This model (nmt_tf
) has been trained for 50 epochs on 100,000 sentences which achieved around 98% accuracy on a 35000+ validation set. It might take longer for this exercise to initialize as the trained model needs to be loaded. You are provided with the sents2seqs()
function. You have also been given two new functions:
word2onehot(tokenizer, word, vocab_size)
which accepts:
- tokenizer - A Keras
Tokenizer
object - word - A string representing a word from the vocabulary (e.g.
'apple'
) - vocab_size - Vocabulary size
probs2word(probs, tok)
which accepts:
- probs - An output from the model of the shape
[1,<French Vocab Size>]
- tok - A Keras
Tokenizer
object
You can peak at the source code for these functions by typing print(inspect.getsource(word2onehot))
and print(inspect.getsource(probs2word))
in the console.
This exercise is part of the course
Machine Translation with Keras
Exercise instructions
- Predict the initial decoder state (
de_s_t
) with the encoder. - Predict the output and the new state from the decoder using the previous prediction (output) and the previous state as inputs. Remember to recursively generate the new state.
- Get the word string from the probability output using the
probs2word()
function. - Convert the word string to a one-hot sequence using the
word2onehot()
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
en_sent = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
print('English: {}'.format(en_sent))
en_seq = sents2seqs('source', en_sent, onehot=True, reverse=True)
# Predict the initial decoder state with the encoder
de_s_t = ____.predict(____)
de_seq = word2onehot(fr_tok, 'sos', fr_vocab)
fr_sent = ''
for i in range(fr_len):
# Predict from the decoder and recursively assign the new state to de_s_t
de_prob, ____ = ____.predict([____,____])
# Get the word from the probability output using probs2word
de_w = probs2word(____, fr_tok)
# Convert the word to a onehot sequence using word2onehot
de_seq = word2onehot(fr_tok, ____, fr_vocab)
if de_w == 'eos': break
fr_sent += de_w + ' '
print("French (Ours): {}".format(fr_sent))
print("French (Google Translate): les etats-unis sont parfois froids en décembre, mais parfois gelés en juin")