Search

Search papers and lessons

Dense Passage Retrieval for Open-Domain Question Answering

Vladimir Karpukhin, Barlas Oğuz, Sewon Min et al. · 2020

DPR trains two BERT encoders so that questions and matching passages have high dot products, and it beats keyword search at finding answer-bearing passages when trained on question-answer pairs alone.

On this page

  • Colour = topic mix
  • Discrete math
  • Neural network basics
  • Calculus
  • Linear algebra
  • Probability
  • Algorithms
  • ReadReady to readNot yet

TL;DR

Open-domain question answering first has to find a few passages that might contain the answer, out of millions. DPR (Dense Passage Retriever) does this with two BERT encoders, one for questions and one for passages, that map text to vectors so that a question and its answer-bearing passage have a large dot product. Trained only on question-passage pairs, it finds the answer in the top 20 passages far more often than the keyword method BM25 on most benchmarks.

Builds on: Bi-Encoder, BERT: Pre-training of Deep Bidirectional Transformers, Contrastive Learning · Leads to: Retrieval-Augmented Generation (the retrieve-then-read pattern that DPR made practical; the link is conceptual rather than a direct dependency)

Why it mattered

DPR became one of the standard first-stage retrievers for question answering and for systems that feed retrieved text to a language model. Before it, dense retrieval was widely believed to need large amounts of extra pretraining to beat keyword search. DPR showed that a simple supervised dual encoder, with a good batching trick, was enough, and its code and pretrained models made the method easy to reuse. Later retrieval work, including many systems that index web or enterprise documents, follows the same pattern: encode once offline, search with a nearest-neighbour index at query time.

The problem

Open-domain question answering works in two stages. A retriever scans a large collection of passages, here 21 million passages from English Wikipedia, and selects a short list, typically 20 to 100. A reader then reads that list and extracts the answer. If the retriever misses the passage with the answer, the reader cannot recover, so the quality of the first stage limits the whole system.

The standard retriever is keyword matching, such as TF-IDF or BM25. It is fast, because it uses an Inverted Index, but it matches words, not meanings. A question asking for the "bad guy in the Lord of the Rings" will miss a passage that says "villain" and never says "bad guy". Dense retrieval, which represents text as vectors, can match the meaning. The difficulty was that good dense vectors seemed to need a lot of labelled data or extra pretraining. ORQA, the strongest earlier dense system, used an expensive pretraining task. DPR asks whether plain question-passage pairs are enough.

How it works

Follow one question through the system, with a tiny vector example in place of real 768-dimensional embeddings.

flowchart LR
  subgraph Offline
    P[21M passages] --> EP[Passage encoder BERT]
    EP --> IDX[Vector index]
  end
  subgraph Online
    Q[Question] --> EQ[Question encoder BERT]
    EQ --> S[Search top k by dot product]
    IDX --> S
    S --> R[Reader]
  end
  1. Encode every passage once, offline. A BERT encoder reads each passage and outputs one vector for its [CLS] token, of size d=768d = 768. The 21 million passage vectors are stored in an index. This is slow but happens only once.
  2. Encode the question at query time. A second BERT encoder, with its own weights, reads the question and outputs one vector. For this trace, use a 2-dimensional toy: the question vector is q=(1,2)q = (1, 2).
  3. Score every passage by dot product. Suppose three passages have vectors p1=(2,1)p_1 = (2, 1), p2=(0,1)p_2 = (0, 1) and p3=(−1,0)p_3 = (-1, 0). The scores are q⋅p1=1×2+2×1=4q \cdot p_1 = 1 \times 2 + 2 \times 1 = 4, q⋅p2=1×0+2×1=2q \cdot p_2 = 1 \times 0 + 2 \times 1 = 2, and q⋅p3=1×(−1)+2×0=−1q \cdot p_3 = 1 \times (-1) + 2 \times 0 = -1.
  4. Keep the top k. The index returns the passages with the largest scores. With k=2k = 2, that is p1p_1 and p2p_2, which are passed to the reader.
  5. Train the encoders so the right passage scores highest. Training uses pairs of a question and its positive passage, plus negative passages that are wrong. The loss, explained in the math section, pushes the positive's score up relative to the negatives. This is the step that makes the toy geometry above true for real text.
  6. Negatives come cheaply from the batch. Within a mini-batch of questions, the passage that belongs to one question is a negative for every other question. This turns a batch of size BB into BB positives and B−1B - 1 negatives per question, at no extra encoding cost.

Check: with the scores above, p1p_1 beats p2p_2 and both beat p3p_3. Each step of the training objective is also a comparison among scores like these.

The math

Step 3 in symbols: the similarity

Let EQE_Q be the question encoder and EPE_P the passage encoder, each mapping text to a vector in Rd\mathbb{R}^d. The similarity is the dot product

sim(q,p)=EQ(q)⊤EP(p).\text{sim}(q, p) = E_Q(q)^{\top} E_P(p).

The paper chooses the dot product on purpose. Passage vectors can be computed in advance and stored, and a fast maximum inner product search (Maximum Inner Product Search) finds the top scores without checking every passage. A more expressive scorer, such as a network that compares question and passage word by word, would have to run on every pair at query time.

Step 5 in symbols: the training loss

Each training instance has one question qiq_i, one positive passage pi+p_i^{+}, and negative passages pi,1−,…,pi,n−p_{i,1}^{-}, \dots, p_{i,n}^{-}. The loss is the negative log-likelihood of the positive, normalised over the positive and the negatives. This is a Softmax over the candidates followed by a Cross-Entropy Loss:

L=−log⁡esim(qi,pi+)esim(qi,pi+)+∑j=1nesim(qi,pi,j−).L = -\log \frac{e^{\text{sim}(q_i, p_i^{+})}}{e^{\text{sim}(q_i, p_i^{+})} + \sum_{j=1}^{n} e^{\text{sim}(q_i, p_{i,j}^{-})}}.

Raising the positive score makes the fraction closer to 1 and the loss smaller. Raising a negative score makes the denominator bigger and the loss larger.

Step 6 in symbols: in-batch negatives

With BB questions in a batch, let Q\mathbf{Q} and P\mathbf{P} be the B×dB \times d matrices of question and positive-passage embeddings. The score matrix is

S=QP⊤,Sij=sim(qi,pj).\mathbf{S} = \mathbf{Q}\mathbf{P}^{\top}, \qquad S_{ij} = \text{sim}(q_i, p_j).

Entry SiiS_{ii} is a positive pair and SijS_{ij} for j≠ij \neq i is a negative. One matrix product scores B2B^2 pairs. The paper uses B=128B = 128, plus one BM25 negative per question.

Worked examples

Example
Example 1: a dot-product search by hand

Question vector q=(1,2)q = (1, 2), and three passages p1=(2,1)p_1 = (2, 1), p2=(0,1)p_2 = (0, 1), p3=(−1,0)p_3 = (-1, 0).

  1. Score p1p_1: 1×2+2×1=41 \times 2 + 2 \times 1 = 4.
  2. Score p2p_2: 1×0+2×1=21 \times 0 + 2 \times 1 = 2.
  3. Score p3p_3: 1×(−1)+2×0=−11 \times (-1) + 2 \times 0 = -1.
  4. Sort the scores: 4>2>−14 > 2 > -1, so the ranking is p1p_1, p2p_2, p3p_3.
  5. Interpretation: a top-2 search returns p1p_1 and p2p_2. The search needs only dot products, which is why an index can make it fast.
Example
Example 2: the training loss for one question

One question has a positive passage with score sim(q,p+)=2.0\text{sim}(q, p^{+}) = 2.0, and two negatives with scores 0.50.5 and 1.01.0.

  1. Exponentiate each score: e2.0≈7.389e^{2.0} \approx 7.389, e0.5≈1.649e^{0.5} \approx 1.649, e1.0≈2.718e^{1.0} \approx 2.718.
  2. Add the denominator: 7.389+1.649+2.718=11.7567.389 + 1.649 + 2.718 = 11.756.
  3. The probability that the positive is chosen is 7.389/11.756≈0.6297.389 / 11.756 \approx 0.629.
  4. The loss is −ln⁡(0.629)≈0.465-\ln(0.629) \approx 0.465.
  5. Interpretation: the model is not yet confident. If the positive's score rose to 4.0 with the same negatives, e4.0≈54.598e^{4.0} \approx 54.598, the probability would rise to 54.598/58.965≈0.92654.598 / 58.965 \approx 0.926 and the loss would fall to about 0.08.
Example
Try it: batch size and the number of training pairs

A batch has B=128B = 128 questions, each with one positive passage. How many training pairs does one batch produce, how many of those are negatives, and how many negatives does each question get from the batch alone?

Answer: The score matrix has 128×128=16384128 \times 128 = 16384 entries. Of these, 128 are positive (the diagonal), so 16384−128=1625616384 - 128 = 16256 are negatives. Each question gets 128−1=127128 - 1 = 127 in-batch negatives, plus the one BM25 negative the paper adds, for 128 negatives in total. All of this comes from one matrix product over 128 question and 128 passage vectors.

Results

The retrieval metric is top-k accuracy: the share of questions for which at least one of the top k passages contains the answer. The paper reports it on several open-domain benchmarks, including Natural Questions and TREC, for top-20 and top-100 retrieval. Its headline result is that a trained dense retriever beats BM25 on most of these, and the paper's own tables give the exact per-dataset figures, which should be read there rather than from a summary.

The exception is SQuAD. There BM25 stays ahead. The paper's explanation is that annotators wrote SQuAD questions after reading the passage, so the wording overlaps heavily with it, which favours keyword search. SQuAD also draws on a small set of Wikipedia articles, which limits what the encoder learns.

Ablations show that in-batch negatives with one BM25 negative per question work well. The paper reports that the choice of similarity function (dot product, L2 or cosine) matters little. It also reports end-to-end question answering with a BERT reader, where DPR improves on the earlier ORQA system.

Speed and cost. Searching a dense index is fast, but building it is the expensive step. Encoding all 21 million passages takes many GPU-hours, while an inverted index for BM25 is built much faster. Once built, the index must be rebuilt whenever the passage encoder changes.

Limits. The retriever is trained on labelled question-answer pairs, so it needs data for each domain. Keyword matching remains stronger where questions reuse the passage's words.

Reading guide

The paper is read in this order. Each section names the lessons it needs.

  1. Introduction. Needs Open-Domain Question Answering for the task and TF-IDF or BM25 for the baseline it beats.
  2. Background. Needs Inverted Index for keyword search and Maximum Inner Product Search for the dense side.
  3. Method overview. Needs Bi-Encoder and BERT: Pre-training of Deep Bidirectional Transformers. The encoders use the [CLS] vector of BERT, which the BERT lesson explains.
  4. Training. Needs Softmax, Cross-Entropy Loss and Contrastive Learning. The in-batch trick is a contrastive objective.
  5. Setup and retrieval results. Needs Approximate Nearest Neighbor Search for the indexing, and Stochastic Gradient Descent for training with Adam.
  6. End-to-end question answering. Needs Masked Language Modeling for the BERT reader's pretraining background, and Retrieval-Augmented Generation for the broader pattern.

Check yourself

QuestionWhy is the dot product, rather than a more expressive scorer, used for the similarity in DPR?Show answer

A dot product between a question vector and a passage vector can be computed after the passages are encoded, so all passage vectors can be stored in an index. A query then needs only a fast nearest-neighbour search over the stored vectors. A scorer that reads the question and passage together would have to run on every pair at query time, which is far too slow across 21 million passages.

QuestionQuestion vector q=(2,1)q = (2, 1). Passages are p1=(1,3)p_1 = (1, 3), p2=(4,0)p_2 = (4, 0), and p3=(−1,2)p_3 = (-1, 2). Rank them by dot product.Show answer

q⋅p1=2×1+1×3=5q \cdot p_1 = 2 \times 1 + 1 \times 3 = 5. q⋅p2=2×4+1×0=8q \cdot p_2 = 2 \times 4 + 1 \times 0 = 8. q⋅p3=2×(−1)+1×2=0q \cdot p_3 = 2 \times (-1) + 1 \times 2 = 0. The ranking is p2p_2 (8), then p1p_1 (5), then p3p_3 (0).

QuestionWhy does DPR lose to BM25 on SQuAD, while it wins on Natural Questions?Show answer

On SQuAD, annotators wrote questions after seeing the passage, so the questions share many exact words with it. Keyword matching exploits this directly. SQuAD also uses only about 500 Wikipedia articles, so the training data is biased toward those articles and does not teach the encoder broad semantic matching. Natural Questions uses real search queries, where the words often differ from the passage, and there dense matching of meaning helps.

QuestionWhy does the in-batch negative trick reduce the cost of training, and what is its downside?Show answer

The passages that belong to other questions in the batch are already encoded, so their scores come from one matrix product at almost no extra cost. A batch of BB gives B−1B - 1 negatives per question for free. The downside is that these negatives are random, and random passages are often easy to reject. The paper adds one harder BM25 negative per question to help with that.

Sources

Read the full paper

Vladimir Karpukhin, Barlas Oğuz, Sewon Min et al., 2020. arXiv:2004.04906