Sentence-BERT (SBERT) is BERT fine-tuned so that a whole sentence becomes a single vector, allowing two sentences to be compared with a simple cosine similarity. Nils Reimers and Iryna Gurevych introduced it in 2019 (Reimers & Gurevych, 2019), and their library, sentence-transformers, became one of the most widely used tools for producing text embeddings (Alammar & Grootendorst, 2024, pp. 62, 293).
The problem: BERT compares well, but at a price
BERT works as a cross-encoder: to measure how similar two sentences are, you feed them in together, separated by a [SEP] token. It is accurate but does not scale: finding the most similar pair among 10,000 sentences takes about 50 million passes through the network, some 65 hours on a V100 GPU (Reimers & Gurevych, 2019, p. 3982; Alammar & Grootendorst, 2024, p. 294). The obvious alternative, passing each sentence alone and averaging its vectors or taking the [CLS] vector, yields poor sentence embeddings: on seven semantic similarity tasks both do worse than simply averaging GloVe vectors, and [CLS] is the worst of all (Reimers & Gurevych, 2019, pp. 3982, 3985).
How it works
SBERT adds a pooling layer (by default, the mean of the output vectors) and fine-tunes BERT inside a siamese network: two copies with the same weights process each sentence separately and produce vectors u and v (Reimers & Gurevych, 2019, p. 3984; Alammar & Grootendorst, 2024, pp. 294–295). During fine-tuning, a classifier receives (u, v, |u − v|) and must decide whether the pair is entailment, contradiction or neutral, using the natural language inference data of SNLI (570,000 pairs) and MultiNLI (430,000) (Reimers & Gurevych, 2019, pp. 3984–3985). One epoch is enough, under 20 minutes of compute (2019, pp. 3984–3985). The decisive component turned out to be the difference |u − v|, which pushes similar pairs together and dissimilar ones apart (2019, pp. 3988–3989). Afterwards the classifier is discarded: in use, only the vectors and the cosine remain. It is a case of contrastive learning: the model learns what makes two texts alike by seeing similar and dissimilar pairs (Alammar & Grootendorst, 2024, pp. 292–295).
What changed
The result is a bi-encoder: each text is encoded once and stored. Finding the most similar pair among 10,000 sentences drops from 65 hours to about 5 seconds, plus 0.01 seconds of cosines; with suitable indexes, searching millions of questions takes milliseconds (Reimers & Gurevych, 2019, p. 3983). On semantic similarity, SBERT also beat the best sentence embedding models of the day, InferSent and the Universal Sentence Encoder (2019, pp. 3983, 3985).
This architecture is the basis of dense semantic search: the query and the documents are projected into the same space and the nearest neighbors are returned (Alammar & Grootendorst, 2024, pp. 226–228), stored in a vector database. It is the typical retriever of Retrieval-augmented generation (RAG).
Limits
The bi-encoder is fast but less accurate than the cross-encoder, which reads both texts at once; that is why search systems often chain them: SBERT retrieves candidates and a cross-encoder reranks them (Alammar & Grootendorst, 2024, pp. 226, 244, 295). It inherits BERT's window, so long documents must be split (2024, p. 235). And the similarity it learns depends on its data: fine-tuned on NLI, it did worse than the Universal Sentence Encoder on SICK-R and on TREC question classification, domains closer to the latter's training data (Reimers & Gurevych, 2019, pp. 3985, 3988).