Understanding one-hot vectors
Here you will learn to generate one-hot encoded vectors from words. One-hot encoding is a common transformation applied to words to represent them numerically.
You will be using the Keras to_categorical()
function to create one-hot vectors. The to_categorical()
function expects a sequence of integers as the input. Therefore, a word2index
dictionary is provided which can be used to convert a word to an integer.
To successfully complete this exercise you will also have to use the built-in Python zip()
function. The zip()
function allows you to iterate multiple things at once. For example if you have two lists xx
and yy
of same length, by calling for x,y in zip(xx,yy)
you can access each x
and y
elements of the lists iteratively.
This exercise is part of the course
Machine Translation with Keras
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
from tensorflow.keras.utils import to_categorical
# Create a list of words and convert them to indices
words = [____, ____, ____]
word_ids = [word2index[____] for w in ____]
print(____)