0
0
NLPml~10 mins

GloVe embeddings in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A3:
B0:
C2:
D1:
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.
2fill in blank
medium

Complete 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'
A'apple'
B'APPLE'
C'apples'
D'Apple'
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.
3fill in blank
hard

Fix 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'
Avec2
Bvec1 - vec2
Cvec1
Dvec1 + vec2
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.
4fill in blank
hard

Fill 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'
A>
B50
C<
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which filters vectors shorter than 50.
Using 100 which is not the intended threshold.
5fill in blank
hard

Fill 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'
A>
B1.0
C100
D<
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.