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.
This exercise is part of the course
Machine Translation with Keras
Exercise instructions
- 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_map
and assign toseq_len
. - Get the word ID in the
i-th
sentence att-th
position. - Append the
String
word (i.e. not the word ID) corresponding towid
to the listwords
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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))