AI engineering paper · 2019

Sentence-BERT

Reimers and Gurevych · EMNLP 2019

How Sentence-BERT made BERT produce one vector per sentence, why comparing sentences in pairs was too slow for search, and how training works now.

Each sentence goes through the same network alone and becomes one vector. Comparing two sentences is then one cosine similarity, a score for how closely two vectors point the same way. So 10,000 sentences need 10,000 runs of the network, not 49,995,000.

You’ll learn to

  • Explain why scoring every pair of sentences with BERT is too slow for search, and how two copies of one network avoid it.
  • Trace a sentence through pooling and cosine similarity by hand, with small numbers.
  • Say what the model was trained on, what each of the three training goals looked at, and which choices mattered most.
  • Describe what changed after the paper: training that treats the other items in a batch as wrong answers, labels that mark queries and passages, wider tests, and vectors cut down to a shorter length.

The idea in short

Search by meaning needs each piece of text turned into a vector, a fixed-length list of numbers, so that texts with similar meaning get vectors that sit close together. BERT, the leading language model of 2018, could judge whether two sentences mean the same, but only by reading them together as one input. Finding the closest pair among 10,000 sentences that way takes about 50 million runs of the model, around 65 hours.

Sentence-BERT uses two copies of BERT that share the same numbers. Each copy reads one sentence alone, and its outputs are averaged into one vector. Training pushes the vectors of related sentences together. After that, each sentence goes through the model once, and comparing two sentences costs one short sum. The same 10,000 sentences take about 5 seconds. Later work kept this shape and changed how it is trained.

The problem

Search by meaning starts by turning a piece of text into a : a fixed-length list of numbers made by a model, where texts with similar meaning get vectors that sit close together. Another word for it is an . To answer a query you embed it too, then look for the stored vectors nearest to it. That is the idea behind , and it also lets you group similar texts into clusters.

In 2019 Nils Reimers and Iryna Gurevych published “Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks”. This guide calls the method SBERT and the publication “the paper”. It starts from a problem with the best sentence-comparing model of the time.

, released in 2018, is a language model: a program that has read a huge amount of text and stores what it learned as millions of numbers called its . BERT cuts text into tokens, small pieces that are usually words or parts of words, and gives every token its own vector. Its base size has 12 stacked layers and 110 million weights. Its large size has 24 layers and 340 million. Training a model like this a little further on your own examples, which changes its weights, is . A fine-tuned BERT set records on , or STS: people score pairs of sentences from 0 (unrelated) to 5 (same meaning), and a system is graded on how closely its scores follow theirs. The paper’s tests use seven such collections of scored pairs.

The catch is how BERT does it. It takes the two sentences together as one input, joined by a separator mark, and in every layer each token can look at the tokens of both sentences. This looking-at-each-other is called . A model that reads a pair together is called a . It never produces a vector for one sentence on its own. It only produces a score for a pair.

That is fine for scoring one pair and ruinous for search. The paper does the sums:

  • To find the most similar pair among 10,000 sentences, every pair must be scored: 10,000 × 9,999 ÷ 2 = 49,995,000 model runs, one run per pair. On a high-end , a graphics chip used for machine learning, the paper puts that at about 65 hours.
  • To find the best match for a new question among the 40 million questions already on the Quora forum, you would score the new question against each of them. The paper puts one query at over 50 hours.

That is roughly 4.5 to 4.7 milliseconds per run (our arithmetic: 65 hours is 234,000 seconds, and 234,000 ÷ 49,995,000 = 4.7 ms; 50 hours is 180,000 seconds, and 180,000 ÷ 40,000,000 = 4.5 ms). Nothing about that is slow for one pair. The problem is the number of pairs, which grows with the square of the number of sentences.

The obvious shortcut is to run each sentence through BERT alone and squeeze its token vectors into one, either by averaging them or by taking the vector of the special first token, which BERT puts at the start of every input. Several projects already did this, and nobody had checked whether the result was any good. The paper checked. Compared with similarity scores from humans, these vectors did worse than averaged GloVe vectors, which give each word one fixed vector and use no deep network at all. The numbers come below, once the score has been explained.

So the question the paper answers is: can BERT be changed so that each sentence gets its own useful vector in one pass, cheap enough for search and clustering, without losing much accuracy?

The design choices

The answer is SBERT. It changes BERT in three ways, and the table adds the check that the changes worked.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks by Reimers and Gurevych, EMNLP 2019.

Read the original paper

More AI engineering papers

All papers