Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load GloVe embeddings from a file.
NLP
glove_vectors = {}
with open('glove.txt', 'r', encoding='utf8') as f:
for line in f:
values = line.split()
word = values[0]
vector = list(map(float, values[[1]]))
glove_vectors[word] = vector Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0: which includes the word as a float and causes an error.
Using 2: or 3: which skips some vector values.
✗ Incorrect
The first value in each line is the word, so the vector starts from index 1 onward.
2fill in blank
mediumComplete the code to get the embedding vector for the word 'apple'.
NLP
word = 'apple' embedding = glove_vectors.get([1], None)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or capitalized versions that do not exist in the dictionary.
Using plural form 'apples' which may not be in the embeddings.
✗ Incorrect
GloVe embeddings are case sensitive and usually lowercase, so 'apple' is correct.
3fill in blank
hardFix the error in the code to compute cosine similarity between two GloVe vectors.
NLP
import numpy as np def cosine_similarity(vec1, vec2): dot_product = np.dot(vec1, vec2) norm1 = np.linalg.norm(vec1) norm2 = np.linalg.norm([1]) return dot_product / (norm1 * norm2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the first vector twice causing incorrect similarity values.
Using vector addition or subtraction instead of the second vector.
✗ Incorrect
The norm of the second vector should be computed using vec2 to calculate cosine similarity correctly.
4fill in blank
hardFill both blanks to create a dictionary of words with vectors longer than 50 dimensions.
NLP
filtered_glove = {word: vec for word, vec in glove_vectors.items() if len(vec) [1] [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which filters vectors shorter than 50.
Using 100 which is not the intended threshold.
✗ Incorrect
We want vectors with length greater than 50, so use '>' and 50.
5fill in blank
hardFill all three blanks to create a list of words whose embedding vectors have norm greater than 1.0.
NLP
import numpy as np words_with_norm = [word for word, vec in glove_vectors.items() if np.linalg.norm(vec) [1] [2]] result = words_with_norm[:[3]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which selects vectors with norm less than 1.0.
Using wrong slice number causing too few or too many words.
✗ Incorrect
We check if norm is greater than 1.0 and then take the first 100 words.