Sentence Embeddings & Sentence-BERT

Turning a whole sentence into numbers so you can search by meaning.

🟡 intermediate5 min readembeddingsretrieval
A sentence embedding turns a whole sentence into a single list of numbers, so similar sentences end up close together. Plain BERT can only compare two sentences by reading them together, so finding the most similar pair among 10,000 sentences takes around 65 hours. Sentence-BERT is trained so each sentence becomes a vector on its own, just once. Comparing vectors is then quick arithmetic, and the same search takes about five seconds. This is what powers semantic search and RAG retrieval.

The Sentence Comparison Problem

Suppose you want to search a database of N=10,000N = 10,000 customer support questions to find the most similar match for a new user query.

  Naive BERT Pairwise Cross-Encoder Approach:
  Pass: [CLS] Query [SEP] Candidate_1 [SEP] ──► Forward Pass 1
  Pass: [CLS] Query [SEP] Candidate_2 [SEP] ──► Forward Pass 2
  ...
  Total Forward Passes Required: 10,000 passes! (Takes ~5 seconds per query!)

If you want to find the top similar sentence pairs across all 10,000 documents:

Required Pairwise Passes=10,000×9,9992≈50,000,000 Forward Passes!(Takes ∼65 Hours!)\text{Required Pairwise Passes} = \frac{10,000 \times 9,999}{2} \approx 50,000,000 \text{ Forward Passes!} \quad (\text{Takes } \sim \mathbf{65 \text{ Hours!}})

Standard BERT cross-encoders cannot scale for real-time search!

The Sentence-BERT Solution (Siamese Architecture)

Sentence-BERT (SBERT - Reimers & Gurevych, 2019) introduced a Siamese / Dual-Encoder Architecture:

  OFFLINE PRE-COMPUTATION (Run ONCE):
  Sentence A ──► [ BERT Encoder ] ──► [ Mean Pooling ] ──► Embedding Vector u [1 x 768]
  Sentence B ──► [ BERT Encoder ] ──► [ Mean Pooling ] ──► Embedding Vector v [1 x 768]

  ONLINE REAL-TIME SEARCH (Sub-10ms):
  Cosine Similarity = ( u · v ) / ( ||u|| ||v|| )  ──► Sub-millisecond Matrix Math!

Pre-computing Vectors Offline

SBERT computes a single dense vector embedding u∈R768\mathbf{u} \in \mathbb{R}^{768} for every document offline and stores them in a Vector Database (Qdrant, Pinecone, FAISS).

When a user submits a query:

  1. Compute query vector q\mathbf{q} in a single forward pass (10ms10\text{ms}).
  2. Execute vector search CosineSim(q,ui)\text{CosineSim}(\mathbf{q}, \mathbf{u}_i) across 10,000 pre-computed vectors in under 1 millisecond!

Training SBERT: Multiple Negatives Ranking Loss (MNRL)

SBERT is trained on Natural Language Inference (NLI) datasets containing (Premise, Positive, Negative) sentence triplets:

  Premise:   "A man is playing a guitar."
  Positive:  "A person is performing music." (Entailment)
  Negative:  "A man is sleeping on a couch." (Contradiction)

Multiple Negatives Ranking Loss (InfoNCE):

L=−log⁡exp⁡(cos⁡(ui,vi+)/τ)exp⁡(cos⁡(ui,vi+)/τ)+∑jexp⁡(cos⁡(ui,vj−)/τ)\mathcal{L} = -\log \frac{\exp(\cos(\mathbf{u}_i, \mathbf{v}_i^+) / \tau)}{\exp(\cos(\mathbf{u}_i, \mathbf{v}_i^+) / \tau) + \sum_j \exp(\cos(\mathbf{u}_i, \mathbf{v}_j^-) / \tau)}

This loss pulls semantically equivalent sentence vectors together while pushing contradictory or unrelated sentence vectors far apart.

Say this out loud

Standard BERT requires pairing sentences together, taking 65 hours to compare 10,000 sentence combinations. Sentence-BERT uses a Siamese network architecture with mean pooling to project sentences into single dense vectors offline. Sentence similarity reduces to fast cosine dot products in vector databases, enabling sub-10ms semantic search in RAG pipelines.

Followups to expect

Check yourself

Question 1 of 3

Why is standard BERT inefficient for finding the most similar sentence pairs in a 10,000 sentence collection?

More in NLP & Transformers

See all →
The Attention Mechanism5 minTransformer Architecture5 minTokenization & BPE5 min