Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load the GloVe embeddings file.
TensorFlow
embedding_index = {}
with open('glove.6B.100d.txt', encoding='utf8') as f:
for line in f:
values = line.split()
word = values[[1]]
coefs = np.asarray(values[1:], dtype='float32')
embedding_index[word] = coefsAttempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 for the word.
Using negative index which refers to the last element.
✗ Incorrect
The first value in each line is the word itself, so we use index 0 to get the word.
2fill in blank
mediumComplete the code to create an embedding matrix for the tokenizer's word index.
TensorFlow
embedding_dim = 100 embedding_matrix = np.zeros((len(word_index) + 1, embedding_dim)) for word, i in word_index.items(): embedding_vector = embedding_index.get(word) if embedding_vector is not None: embedding_matrix[i] = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the word string instead of the vector.
Assigning the index instead of the vector.
✗ Incorrect
We assign the embedding vector for the word to the corresponding row in the matrix.
3fill in blank
hardFix the error in the code to create a Keras Embedding layer using the pre-trained embedding matrix.
TensorFlow
embedding_layer = tf.keras.layers.Embedding(
input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[[1]],
trainable=False
)Attempts:
3 left
💡 Hint
Common Mistakes
Passing the embedding_index dictionary instead of the matrix.
Passing a single vector instead of a list containing the matrix.
✗ Incorrect
The weights argument expects a list containing the embedding matrix.
4fill in blank
hardFill both blanks to compile the model with an optimizer and loss function suitable for binary classification.
TensorFlow
model.compile(optimizer=[1], loss=[2], metrics=['accuracy'])
Attempts:
3 left
💡 Hint
Common Mistakes
Using categorical_crossentropy for binary classification.
Using SGD optimizer without tuning.
✗ Incorrect
Adam optimizer and binary_crossentropy loss are standard choices for binary classification tasks.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps words to their embedding vectors only if the word length is greater than 3.
TensorFlow
filtered_embeddings = {word: embedding_index[word] for word in embedding_index if [1] [2] [3]Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word).
Using less than operator instead of greater than.
✗ Incorrect
We check if the length of the word is greater than 3 to filter embeddings.
