↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

One vector per text

To find the paragraphs that answer a question among thousands, you cannot run a large model over every (question, paragraph) pair at query time. A bi-encoder (or dual encoder) instead turns every text into one vector on its own, so the paragraphs can be encoded once, ahead of time, and a question only has to be encoded and compared. If the encoder is trained so that a question lands near the paragraphs that answer it, retrieval becomes a nearest-neighbour search.

The model on this page is bge-small-en-v1.5: 12 BERT blocks, 384-dimensional hidden states and 12 heads, 33 M parameters. The Text → one vector panel follows one question through it. The question is prefixed with the model's retrieval instruction, "Represent this sentence for searching relevant passages: "; paragraphs are encoded without it. Every token attends to every other in all 12 blocks, and the last block's vector at the [CLS] token, normalised to length 1, is the text's embedding. The panel shades each token by [CLS]'s attention to it in the last block, averaged over the heads.

Cosine and ranking

With unit vectors and the dot product is the cosine of the angle between them:

The Cosine = Σ query × paragraph panel draws the two vectors and their 384 products, which sum to the score. Ranking the corpus is then one matrix product of the question vector with all paragraph vectors, sorted. The corpus is the SQuAD v1.1 validation set's 2,067 unique paragraphs; the 300 questions are drawn from it at random, and each question's one relevant paragraph is the one it was written on.

The All 2,067 cosines histogram shows that a question's cosines with unrelated paragraphs are far from 0: all texts point partly the same way in this space. A cosine therefore means little on its own, and only the order is used.

Measuring retrieval

With one relevant paragraph at rank in a question's list, the standard metrics reduce to simple functions of , averaged over the 300 questions:

Recall@k asks whether the paragraph is in the first ; MRR and nDCG also reward placing it higher, nDCG with a gentler, logarithmic discount. bge-small puts the relevant paragraph first for 0.673 of the questions and in the top 10 for 0.947, with MRR@10 0.766 and nDCG@10 0.810; every relevant paragraph is in its question's top 100. The page's values match pytrec_eval, the standard evaluation tool.

Contrastive training with InfoNCE

An encoder learns this geometry from pairs of a question and the paragraph that answers it. In a batch of pairs, each question's positive is its own paragraph and the other paragraphs are its in-batch negatives. The InfoNCE loss is the cross-entropy of picking the positive among them by softmax over cosines divided by a temperature :

The Contrastive training · InfoNCE panel builds one such batch: 16 questions written on the first 16 paragraphs of one SQuAD training article, so the negatives are on the same topic and hard to tell apart. Its grid holds all cosines with the positives on the diagonal, and below it one question's softmax. With the cosines are multiplied by 50, so a gap of 0.05 in cosine is a factor in probability. The loss only asks the positive to beat the batch's other paragraphs, not to reach any particular cosine. Van den Oord et al. showed that is a lower bound on the mutual information between the two sides, one reason larger batches help.

The page then takes 5 real AdamW steps on that one batch (learning rate ). The batch's loss falls from 0.864 to 0.043, as shown in the InfoNCE loss of the batch chart; the model memorises it. On the 300 test questions nDCG@10 moves from 0.810 to 0.817 and Recall@1 from 0.673 to 0.690: one batch teaches almost nothing general.

Where it came from

Sentence-BERT (2019) fine-tuned BERT as a siamese network so that sentences could be compared by cosine instead of being fed to BERT in pairs. Dense Passage Retrieval (Karpukhin et al., 2020) trained a question encoder and a passage encoder with in-batch negatives for open-domain question answering and beat the classic lexical retriever BM25 on passage retrieval accuracy. The loss itself goes back to contrastive predictive coding (van den Oord et al., 2018). BGE, from the C-Pack paper, follows the now usual recipe: pre-training for retrieval, contrastive training on a very large set of text pairs, then fine-tuning on labelled pairs with instructions and hard negatives.

The other Retrieval tabs build on these vectors: Matryoshka shrinks them, ColBERT keeps a vector per token, Reranker reads question and paragraph together, HNSW and IVF-PQ search them without comparing every pair, and RAG hands what they find to a language model.

Papers

  1. Dense Passage Retrieval for Open-Domain Question Answering Vladimir Karpukhin et al. · EMNLP 2020 A question encoder and a passage encoder trained with in-batch negatives, the bi-encoder retrieval this tab shows.
  2. C-Pack: Packed Resources For General Chinese Embeddings Shitao Xiao, Zheng Liu, Peitian Zhang, Niklas Muennighoff, Defu Lian, Jian-Yun Nie · SIGIR 2024 The BGE embedding models, including the English bge-small-en-v1.5 recorded here, and their training recipe.
  3. Representation Learning with Contrastive Predictive Coding Aaron van den Oord, Yazhe Li, Oriol Vinyals · arXiv 2018 The InfoNCE loss and its bound on mutual information.
  4. Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks Nils Reimers, Iryna Gurevych · EMNLP 2019 BERT fine-tuned to give sentence vectors that are compared by cosine, the start of BERT-based embedding models.
  5. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova · NAACL 2019 The encoder architecture and the [CLS] token the embedding is read from.
  6. SQuAD: 100,000+ Questions for Machine Comprehension of Text Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, Percy Liang · EMNLP 2016 The questions and Wikipedia paragraphs every Retrieval tab searches.

See also