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
Oefeninstructies
- Convert words to IDs using the
word2indexdictionary in thewords2onehot()function - Convert word IDs to onehot vectors having length
3(using thenum_classesargument) and return the resulting array. - Call the
words2onehot()function with the wordsI,likeandcatsand assign the result toonehot. - Print the words and their corresponding onehot vectors using
print()andzip()functions. Thezip()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, ____)])