Part 2: Text reversing model - Encoder

You will now implement the rest of the encoder of the text reversing model. The encoder feeds on the one-hot vectors produced by the words2onehot() function you implemented previously.

Here you will be implementing the encoder() function. The encoder() function takes in a set of one-hot vectors and converts them to a list of word ids.

For this exercise, the words2onehot() function and the word2index dictionary (having the words We, like and dogs) have been provided.

This exercise is part of the course

Machine Translation with Keras

View Course

Exercise instructions

  • Convert onehot to an array of word IDs using np.argmax() function and return the word IDs.
  • Define a list of words with words We, like, dogs.
  • Convert the list of words to onehot vectors using the words2onehot() function. Remember that words2onehot() takes a list of words and a Python dictionary as its arguments.
  • Get the context vector of the onehot vectors using the encoder() function.

Hands-on interactive exercise

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

def encoder(onehot):
  # Get word IDs from onehot vectors and return the IDs
  word_ids = np.____(____, axis=____)
  return ____

# Define "We like dogs" as words
words = ____
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Get the context vector by using the encoder function
context = encoder(____)
print(context)