Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load the sentence transformer model.
NLP
from sentence_transformers import SentenceTransformer model = SentenceTransformer('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a model name meant for image tasks like 'resnet50'.
Using a general language model like 'bert-base-uncased' without sentence transformer wrapper.
✗ Incorrect
The 'all-MiniLM-L6-v2' is a popular lightweight model for semantic similarity tasks using sentence transformers.
2fill in blank
mediumComplete the code to compute embeddings for a list of sentences.
NLP
sentences = ['I love machine learning', 'AI is fascinating'] embeddings = model.[1](sentences)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'predict' which is not available in sentence transformers.
Using 'fit' which is for training, not embedding.
✗ Incorrect
The 'encode' method computes embeddings for input sentences in sentence transformers.
3fill in blank
hardFix the error in computing cosine similarity between two embeddings.
NLP
from sklearn.metrics.pairwise import cosine_similarity sim = cosine_similarity([emb1], [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing emb2[0] which is a scalar, causing shape errors.
Passing emb2.tolist() which may flatten incorrectly.
✗ Incorrect
cosine_similarity expects 2D arrays, so embeddings should be wrapped in lists or arrays. Using 'emb2' as a list works correctly.
4fill in blank
hardFill both blanks to create a dictionary of sentence embeddings for sentences longer than 3 words.
NLP
sentences = ['I love AI', 'Machine learning is fun', 'Hello world'] embeddings = {sentence: model.[1]([sentence])[0] for sentence in sentences if len(sentence.[2]()) > 3}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'encode' for the second blank which is a string method.
Using 'split' for the first blank which is a model method.
✗ Incorrect
We use 'encode' to get embeddings and 'split' to count words by splitting the sentence string.
5fill in blank
hardFill all three blanks to compute cosine similarity scores for pairs of sentences.
NLP
from sklearn.metrics.pairwise import cosine_similarity sentences = ['AI is cool', 'I love AI'] embeddings = model.[1](sentences) similarity_score = cosine_similarity(embeddings[[2]].reshape(1, -1), embeddings[[3]].reshape(1, -1))[0][0]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'predict' instead of 'encode' for embeddings.
Mixing up indices 0 and 1.
✗ Incorrect
We encode sentences to get embeddings, then compare embedding 0 with embedding 1 using cosine similarity.