Semantic Similarity and Sentence Embeddings
Sentence-BERT, cosine similarity for semantic search, embedding clustering.
Semantic Similarity and Sentence Embeddings is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Sentences, Not Just Words
Word embeddings represent words, but we often need to compare whole sentences or documents. Averaging word vectors loses nuance; we want a single vector that captures full meaning.
What Are Sentence Embeddings
Sentence embeddings map an entire sentence to one dense vector so that sentences with similar meaning have nearby vectors, enabling semantic search and clustering.
The sentence-transformers Library
The sentence-transformers library makes this easy. It wraps fine-tuned transformer models that produce high-quality sentence vectors directly.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")Why all-MiniLM-L6-v2
all-MiniLM-L6-v2 is a popular default: small, fast, and accurate, producing 384-dimensional vectors. It balances speed and quality well for most applications.
Encoding Sentences
model.encode turns a list of sentences into a matrix of embeddings, one row per sentence.
sentences = [
"The cat sits on the mat.",
"A feline rests on the rug.",
"I am learning machine learning.",
]
embeddings = model.encode(sentences)
print(embeddings.shape) # (3, 384)Measuring Similarity
Cosine similarity measures the angle between two vectors, ignoring magnitude. Values near 1 mean very similar meaning, near 0 mean unrelated.
from sentence_transformers import util
sim = util.cos_sim(embeddings[0], embeddings[1])
print(sim.item()) # high, both about a cat restingUsing sklearn cosine_similarity
You can also use scikit-learn directly on the embedding matrix to get a full pairwise similarity matrix.
from sklearn.metrics.pairwise import cosine_similarity
matrix = cosine_similarity(embeddings)
print(matrix) # (3, 3) pairwise similaritiesSemantic Search Idea
Semantic search finds documents by meaning, not keywords. Encode a query and all documents, then rank documents by cosine similarity to the query vector.
A Semantic Search Example
Encode the corpus once, then for each query compute similarities and return the top matches.
from sentence_transformers import util
corpus = ["How to reset my password", "Refund policy details", "Track my order"]
corpus_emb = model.encode(corpus)
query_emb = model.encode("I forgot my login")
hits = util.semantic_search(query_emb, corpus_emb, top_k=2)
print(hits)Real-World Applications
Sentence embeddings power FAQ matching, duplicate detection, clustering, recommendation, and the retrieval step in RAG systems that feed relevant context to large language models.
Tips for Good Results
Pick a model trained for your task (semantic search vs paraphrase). Normalize embeddings if your similarity metric needs it, and for large corpora use a vector database for fast nearest-neighbor search.
Quick Check
Test your sentence embedding knowledge.
Recap
Recap: Sentence embeddings encode whole sentences into vectors. Use SentenceTransformer("all-MiniLM-L6-v2") and model.encode(sentences), then compare with cosine similarity. This powers semantic search: encode query and corpus, rank by similarity. Key for FAQ matching, clustering, and RAG retrieval.
Frequently asked questions
Is the “Semantic Similarity and Sentence Embeddings” lesson free?
Yes — the full text of “Semantic Similarity and Sentence Embeddings” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Semantic Similarity and Sentence Embeddings”?
Sentence-BERT, cosine similarity for semantic search, embedding clustering. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Semantic Similarity and Sentence Embeddings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Word2Vec: Skip-gram and CBOW
- GloVe and FastText Embeddings
- Text Classification with BERT
- Semantic Similarity and Sentence Embeddings