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
- Encode every passage once, offline. A BERT encoder reads each passage and outputs one vector for its [CLS] token, of size . The 21 million passage vectors are stored in an index. This is slow but happens only once.
- 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 .
- Score every passage by dot product. Suppose three passages have vectors , and . The scores are , , and .
- Keep the top k. The index returns the passages with the largest scores. With , that is and , which are passed to the reader.
- 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.
- 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 into positives and negatives per question, at no extra encoding cost.
Check: with the scores above, beats and both beat . Each step of the training objective is also a comparison among scores like these.
The math
Step 3 in symbols: the similarity
Let be the question encoder and the passage encoder, each mapping text to a vector in . The similarity is the dot product
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 , one positive passage , and negative passages . 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:
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 questions in a batch, let and be the matrices of question and positive-passage embeddings. The score matrix is
Entry is a positive pair and for is a negative. One matrix product scores pairs. The paper uses , plus one BM25 negative per question.
Worked examples
Question vector , and three passages , , .
- Score : .
- Score : .
- Score : .
- Sort the scores: , so the ranking is , , .
- Interpretation: a top-2 search returns and . The search needs only dot products, which is why an index can make it fast.
One question has a positive passage with score , and two negatives with scores and .
- Exponentiate each score: , , .
- Add the denominator: .
- The probability that the positive is chosen is .
- The loss is .
- Interpretation: the model is not yet confident. If the positive's score rose to 4.0 with the same negatives, , the probability would rise to and the loss would fall to about 0.08.
A batch has 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 entries. Of these, 128 are positive (the diagonal), so are negatives. Each question gets 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.
- Introduction. Needs Open-Domain Question Answering for the task and TF-IDF or BM25 for the baseline it beats.
- Background. Needs Inverted Index for keyword search and Maximum Inner Product Search for the dense side.
- 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.
- Training. Needs Softmax, Cross-Entropy Loss and Contrastive Learning. The in-batch trick is a contrastive objective.
- Setup and retrieval results. Needs Approximate Nearest Neighbor Search for the indexing, and Stochastic Gradient Descent for training with Adam.
- 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 . Passages are , , and . Rank them by dot product.Show answer
. . . The ranking is (8), then (5), then (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 gives 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
- Karpukhin, V. et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. https://arxiv.org/abs/2004.04906
- Devlin, J. et al. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. https://arxiv.org/abs/1810.04805