AI Lesson 67 – Sentence Embeddings (SBERT) | Dataplexa

Sentence Embeddings (SBERT)

In the previous lesson, we learned how word embeddings represent individual words using meaning and context. However, many real-world NLP tasks involve comparing complete sentences, not just single words. Sentence embeddings solve this problem by converting entire sentences into numerical vectors.

This lesson explains what sentence embeddings are, why they are needed, and how models like Sentence-BERT (SBERT) are used in modern NLP systems.

Real-World Connection

When you search a question on Google or ask a chatbot something in your own words, the system needs to understand the meaning of the entire sentence. Even if two sentences use different words, they may mean the same thing. Sentence embeddings allow systems to measure this similarity accurately.

For example, “How can I learn AI?” and “What is the best way to study artificial intelligence?” should be treated as similar queries.

What Are Sentence Embeddings?

Sentence embeddings are dense numerical vectors that represent the meaning of an entire sentence. Unlike Bag of Words or TF-IDF, they capture semantic meaning, word order, and context.

  • Each sentence becomes a fixed-length vector
  • Similar sentences have similar vectors
  • Works well for semantic similarity tasks

Why Word Embeddings Are Not Enough

Word embeddings represent words independently. Simply averaging word vectors often loses meaning and structure. Sentence embeddings solve this by learning sentence-level representations directly.

  • Preserves sentence meaning
  • Handles different word orders
  • Understands context better

What Is Sentence-BERT (SBERT)?

Sentence-BERT is a modification of the BERT architecture designed specifically to generate meaningful sentence embeddings. It uses Siamese and triplet network structures to compare sentence pairs efficiently.

SBERT is widely used because it is fast, accurate, and produces high-quality embeddings for real-world applications.

Sentence Embedding Example Using SBERT


from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

sentences = [
    "AI is transforming the world",
    "Artificial intelligence is changing technology",
    "I love playing football"
]

embeddings = model.encode(sentences)

print(embeddings.shape)
  
(3, 384)

Understanding the Output

Each sentence is converted into a 384-dimensional vector. These vectors can be compared using similarity measures such as cosine similarity to determine how close the meanings are.

Measuring Sentence Similarity


from sentence_transformers import util

similarity = util.cos_sim(embeddings[0], embeddings[1])
print(similarity)
  
tensor([[0.87]])

What the Similarity Score Means

A cosine similarity score close to 1 indicates high semantic similarity. The first two sentences are very similar in meaning, while the third sentence would produce a much lower similarity score.

Where Sentence Embeddings Are Used

  • Semantic search engines
  • Question answering systems
  • Chatbots and virtual assistants
  • Duplicate content detection
  • Document clustering

Practice Questions

Practice 1: What technique represents entire sentences as vectors?



Practice 2: Which model is commonly used for sentence embeddings?



Practice 3: Which metric is used to compare sentence embeddings?



Quick Quiz

Quiz 1: Sentence embeddings mainly capture what?





Quiz 2: Which method is commonly used to compare embeddings?





Quiz 3: Sentence embeddings are heavily used in which application?





Coming up next: NLP Classification Models — using embeddings to build real classifiers.