Aan de slagGa gratis aan de slag

Part 1: Text reversing model - Encoder

Creating a simple text reversing model is a great method to understand the mechanics of encoder decoder models and how they connect. You will now implement the encoder part of a text reversing model.

The implementation of the encoder has been split over two exercises. In this exercise, you will be defining the words2onehot() helper function. The words2onehot() function should take in a list of words and a dictionary word2index and convert the list of words to an array of one-hot vectors. The word2index dictionary is available in the workspace.

Deze oefening maakt deel uit van de cursus

Machine Translation with Keras

Cursus bekijken

Oefeninstructies

  • Convert words to IDs using the word2index dictionary in the words2onehot() function
  • Convert word IDs to onehot vectors having length 3 (using the num_classes argument) and return the resulting array.
  • Call the words2onehot() function with the words I, like and cats and assign the result to onehot.
  • Print the words and their corresponding onehot vectors using print() and zip() functions. The zip() function allows you to iterate multiple lists at the same time.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

import numpy as np

def words2onehot(word_list, word2index):
  # Convert words to word IDs
  word_ids = [____[w] for w in ____]
  # Convert word IDs to onehot vectors and return the onehot array
  onehot = ____(____, num_classes=3)
  return ____

words = ["I", "like", "cats"]
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Print the result as (, ) tuples
print([(w,ohe.tolist()) for ____,____ in zip(words, ____)])
Code bewerken en uitvoeren