Part 1: Treasure hunt
You recently won a all-paid trip to a lush tropical island. While you were wandering around, you found an ancient treasure map pointing to a great treasure, which had a few secret messages written using 1s and 0s. Having just taken this course, you instantly recognize that it is a sequence of onehot encoded vectors. You have also been lucky to find the word to index mapping to know which word refers to which ID.
Now you need to decrypt the secret message and find out what this map is saying. You have been provided with a treasure_map which is a number of sentences by number of words by onehot vector length matrix. You have also been provided with the index2word Python dictionary that maps an ID to a word.
Deze oefening maakt deel uit van de cursus
Machine Translation with Keras
Oefeninstructies
- Get the word IDs for the onehot encoded vectors in the
treasure_map(onehot vector dimension is the last dimension). - Get the sequence length (i.e. number of time steps) from
treasure_mapand assign toseq_len. - Get the word ID in the
i-thsentence att-thposition. - Append the
Stringword (i.e. not the word ID) corresponding towidto the listwords.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Get the word IDs from the treasure map
word_ids = ____.____(____, axis=____)
# Get the sequence length from the treasure map
seq_len = treasure_map.shape[____]
for i in range(treasure_map.shape[0]):
words = []
for t in range(seq_len):
# Get the word ID for the i-th sentence and t-th position
wid = word_ids[i, ____]
if wid != 0:
# Append the word corresponding to wid
words.append(____[____])
print("Instruction ", i+1, ": ", ' '.join(words))