Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Sentence transformers in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Sentence Transformer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of sentence transformers?

Sentence transformers are used to convert sentences into vectors. What is the main reason for doing this?

ATo represent sentences as fixed-size numerical vectors for similarity comparison
BTo translate sentences from one language to another
CTo generate new sentences from a given input
DTo count the number of words in a sentence
Attempts:
2 left
💡 Hint

Think about how computers understand text for tasks like search or clustering.

Predict Output
intermediate
2:00remaining
Output of embedding similarity calculation

Given two sentences embedded using a sentence transformer, what is the cosine similarity output?

Prompt Engineering / GenAI
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity

model = SentenceTransformer('all-MiniLM-L6-v2')
sentences = ['I love machine learning.', 'Machine learning is great.']
embeddings = model.encode(sentences)
similarity = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]
print(round(similarity, 2))
A0.15
B0.85
C1.00
D0.50
Attempts:
2 left
💡 Hint

These sentences have similar meaning, so expect a high similarity but less than 1.

Model Choice
advanced
2:00remaining
Choosing the best sentence transformer model for large-scale semantic search

You want to build a semantic search engine that balances speed and accuracy on millions of sentences. Which sentence transformer model is best?

A'all-MiniLM-L6-v2' for fast and reasonably accurate embeddings
B'bert-base-uncased' for the most accurate embeddings but slower speed
C'distilbert-base-uncased' for the smallest model but less accuracy
D'gpt-3' for generating embeddings with zero-shot learning
Attempts:
2 left
💡 Hint

Consider the trade-off between speed and accuracy for large datasets.

Hyperparameter
advanced
2:00remaining
Effect of changing pooling strategy in sentence transformers

Sentence transformers use pooling to create sentence embeddings from token embeddings. What happens if you change from mean pooling to max pooling?

AMax pooling always improves embedding quality regardless of task
BMax pooling averages all token embeddings, smoothing the sentence representation
CMax pooling reduces embedding size by half compared to mean pooling
DMax pooling captures the most prominent features, possibly making embeddings more sensitive to key words
Attempts:
2 left
💡 Hint

Think about how max pooling selects values compared to averaging.

🔧 Debug
expert
2:30remaining
Why does this sentence transformer embedding code raise an error?

Consider this code snippet:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode('This is a test sentence')
print(embeddings.shape)

What error will this code raise and why?

AAttributeError because embeddings is a 1D array and has no attribute 'shape'
BValueError because the model name is invalid
CNo error; the code runs and prints the shape
DTypeError because encode expects a list of sentences, not a single string
Attempts:
2 left
💡 Hint

Check the documentation for the encode method input types and output.

Practice

(1/5)
1. What is the main purpose of sentence transformers in AI?
easy
A. To count the number of words in a sentence
B. To translate sentences from one language to another
C. To convert sentences into numbers that computers can understand
D. To generate new sentences from scratch

Solution

  1. Step 1: Understand the role of sentence transformers

    Sentence transformers convert sentences into numerical vectors so computers can process them.
  2. Step 2: Compare options with this understanding

    Only To convert sentences into numbers that computers can understand describes this conversion; others describe different tasks.
  3. Final Answer:

    To convert sentences into numbers that computers can understand -> Option C
  4. Quick Check:

    Sentence transformers = convert sentences to numbers [OK]
Hint: Remember: transformers turn text into numbers [OK]
Common Mistakes:
  • Confusing sentence transformers with translation models
  • Thinking they generate new sentences
  • Assuming they only count words
2. Which of the following is the correct way to import a sentence transformer model in Python?
easy
A. from sentence_transformers import sentence_transformer
B. import SentenceTransformer from sentence_transformers
C. import sentence_transformers.SentenceTransformer
D. from sentence_transformers import SentenceTransformer

Solution

  1. Step 1: Recall the correct Python import syntax for sentence transformers

    The correct syntax is 'from sentence_transformers import SentenceTransformer' with exact capitalization.
  2. Step 2: Check each option for syntax correctness

    from sentence_transformers import SentenceTransformer matches the correct syntax; others have wrong order, case, or module names.
  3. Final Answer:

    from sentence_transformers import SentenceTransformer -> Option D
  4. Quick Check:

    Correct import syntax = from sentence_transformers import SentenceTransformer [OK]
Hint: Use 'from module import Class' format for imports [OK]
Common Mistakes:
  • Swapping import order
  • Using wrong capitalization
  • Confusing module and class names
3. What will be the output type of the following code snippet?
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
sentence = 'Hello world'
embedding = model.encode(sentence)
print(type(embedding))
medium
A. <class 'list'>
B. <class 'numpy.ndarray'>
C. <class 'str'>
D. <class 'int'>

Solution

  1. Step 1: Understand the output of model.encode()

    The encode method returns a numerical vector as a numpy array representing the sentence embedding.
  2. Step 2: Identify the type printed

    Printing type(embedding) shows <class 'numpy.ndarray'> because embeddings are numpy arrays.
  3. Final Answer:

    <class 'numpy.ndarray'> -> Option B
  4. Quick Check:

    model.encode() output type = numpy.ndarray [OK]
Hint: model.encode returns numpy arrays for embeddings [OK]
Common Mistakes:
  • Assuming output is a list
  • Thinking output is a string
  • Expecting an integer type
4. Identify the error in this code snippet using sentence transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
sentences = ['Hello world', 'Hi there']
embeddings = model.encode(sentences)
print(embeddings.shape)
medium
A. There is no error; the code runs correctly
B. model.encode() cannot take a list of sentences
C. embeddings does not have a shape attribute
D. The model name 'all-MiniLM-L6-v2' is incorrect

Solution

  1. Step 1: Check model name validity

    'all-MiniLM-L6-v2' is a valid pre-trained model name for sentence transformers.
  2. Step 2: Verify model.encode() input and output

    model.encode() accepts a list of sentences and returns a numpy array with shape attribute.
  3. Step 3: Confirm no errors in code

    All syntax and usage are correct; printing embeddings.shape works as expected.
  4. Final Answer:

    There is no error; the code runs correctly -> Option A
  5. Quick Check:

    Valid model and input = code runs fine [OK]
Hint: model.encode accepts lists and returns arrays with shape [OK]
Common Mistakes:
  • Thinking model.encode only accepts single sentences
  • Assuming embeddings lack shape attribute
  • Believing model name is invalid
5. You want to find the most similar sentence to 'I love machine learning' from a list using sentence transformers. Which approach is best?
hard
A. Encode all sentences, then use cosine similarity to find the closest embedding
B. Compare sentences by counting common words directly
C. Use a translation model to translate sentences before comparison
D. Manually check each sentence for similarity without encoding

Solution

  1. Step 1: Understand the goal of similarity search

    Finding the most similar sentence requires comparing sentence meanings numerically.
  2. Step 2: Identify the best method for semantic similarity

    Encoding sentences into embeddings and using cosine similarity is the standard and effective approach.
  3. Step 3: Evaluate other options

    Counting words or manual checks ignore meaning; translation is unrelated here.
  4. Final Answer:

    Encode all sentences, then use cosine similarity to find the closest embedding -> Option A
  5. Quick Check:

    Semantic similarity = encode + cosine similarity [OK]
Hint: Use embeddings + cosine similarity for best sentence matching [OK]
Common Mistakes:
  • Relying on word count instead of meaning
  • Using translation unnecessarily
  • Skipping encoding step