# 双编码器

双编码器（bi-encoder 或 dual-encoder），又称双塔模型（two-tower model），是一种神经检索架构：它将查询和文档分别独立编码为共享嵌入空间中的稠密向量，再通过余弦相似度或内积等相似度函数比较两个向量来为相关性打分。<sup>[1](https://www.envisioning.com/vocab/dual-encoder)</sup><sup> • </sup><sup>[2](https://www.sci.utah.edu/~beiwang/publications/IR_Survey_BeiWang_2025.pdf)</sup> 其核心特征在于，查询与文档在编码阶段从不通过注意力机制相互交互；所有交互都发生在编码之后，表现为对定长向量的算术运算。<sup>[1](https://www.envisioning.com/vocab/dual-encoder)</sup>

| 关键事实 | 详情 |
|---|---|
| 架构 | 两个编码器塔将查询和文档独立映射到共享向量空间；两塔可共享权重，也可分别参数化<sup>[1](https://www.envisioning.com/vocab/dual-encoder)</sup><sup> • </sup><sup>[3](https://inferensys.com/glossary/semantic-search-and-entity-recognition/dense-passage-retrieval/bi-encoder)</sup> |
| 打分 | 相关性是作用于两个向量的相似度函数，最常见的是余弦相似度或内积<sup>[1](https://www.envisioning.com/vocab/dual-encoder)</sup><sup> • </sup><sup>[2](https://www.sci.utah.edu/~beiwang/publications/IR_Survey_BeiWang_2025.pdf)</sup> |
| 关键效率特性 | 文档嵌入离线计算并建立索引，因此查询时的开销仅为一次查询编码加一次近似最近邻（ANN）查找<sup>[4](https://inferensys.com/glossary/answer-engine-architecture/semantic-indexing-pipelines/bi-encoder)</sup> |
| 里程碑成果 | 稠密段落检索（DPR，2020）在开放域问答数据集上，top-20 段落检索准确率绝对提升 9–19 个百分点，超越 Lucene-BM25<sup>[5](https://arxiv.org/abs/2004.04906)</sup> |
| 速度示例 | Sentence-BERT 将在一万句句子中寻找最相似句对的开销，从 BERT 的约 65 小时降至约 5 秒<sup>[6](https://arxiv.org/abs/1908.10084)</sup> |
| 已知弱点 | 单表示双编码器无法建模细粒度的查询-文档交互，且稠密检索器在零样本评估中往往不如 BM25<sup>[7](https://doi.org/10.1145/3637870)</sup><sup> • </sup><sup>[8](https://arxiv.org/abs/2104.08663)</sup> |

## 工作原理

该架构包含两遍编码和一次比较。在索引阶段，集合中的每篇文档被编码一次，得到定长向量，存入向量索引。当查询到来时，系统只编码查询，并对预建索引执行近似[最近邻搜索](https://www.edgepedia.org/zh/nearest-neighbor-search)，从而避免重新编码整个语料的开销。<sup>[4](https://inferensys.com/glossary/answer-engine-architecture/semantic-indexing-pipelines/bi-encoder)</sup> 由于文档向量不依赖于查询，它可以被缓存，并复用于此后到来的每一个查询。<sup>[9](https://zeroentropy.dev/concepts/bi-encoder/)</sup>

两个编码器不必是同一个模型。在多数实现中，同一套权重执行两遍编码，因此“两个编码器”描述的是排列方式，而非模型数量；在 DPR 风格的设置中，问题编码器和段落编码器分开是标准做法，这样可以针对每个塔做领域特定的优化。<sup>[10](https://whatisrag.net/blogs/bi-encoder/)</sup><sup> • </sup><sup>[3](https://inferensys.com/glossary/semantic-search-and-entity-recognition/dense-passage-retrieval/bi-encoder)</sup> 有些设计刻意让两者不同，用较小的编码器处理查询，用较大的编码器处理文档。<sup>[10](https://whatisrag.net/blogs/bi-encoder/)</sup>

训练通常采用对比学习：模型被训练为将相关的（查询，文档）对在向量空间中拉近，将不相关的对推远，常用批内负采样。<sup>[1](https://www.envisioning.com/vocab/dual-encoder)</sup><sup> • </sup><sup>[4](https://inferensys.com/glossary/answer-engine-architecture/semantic-indexing-pipelines/bi-encoder)</sup>

## 独立性为何能带来规模

独立性正是全部要点所在，因为它使文档侧可以预计算。<sup>[10](https://whatisrag.net/blogs/bi-encoder/)</sup> 一旦向量建立索引，检索就变成一个最近邻问题，借助成熟的 ANN 基础设施可扩展到数十亿向量。<sup>[1](https://www.envisioning.com/vocab/dual-encoder)</sup> 交叉编码器（cross-encoder）通过完整的交叉注意力同时读取查询和文档，对每个（查询，文档）对都需要一次新的前向传播——对 N 个候选大约需要 O(N) 次变换器模型评估，而索引式双编码器只需 O(1)。<sup>[9](https://zeroentropy.dev/concepts/bi-encoder/)</sup><sup> • </sup><sup>[11](https://www.dei.unipd.it/~faggioli/temp/clef2025/paper_276.pdf)</sup>

实测案例显示了这一优势的规模。谷歌的双编码器实体检索系统仅用维基百科中的锚文本链接训练，通过近似最近邻搜索检索候选实体，可在 2 毫秒内从 590 万个候选实体中完成实体链接。<sup>[12](https://arxiv.org/abs/1911.03814)</sup> 同样的双塔模式也被用于文本搜索之外的场景：谷歌将其应用于 [Google Play](https://www.edgepedia.org/zh/google-play) 的大规模应用推荐，采用混合负采样（Mixed Negative Sampling）训练方案，在离线实验和在线 A/B 测试中均优于批负采样和一元负采样。<sup>[13](https://research.google/pubs/mixed-negative-sampling-for-learning-two-tower-neural-networks-in-recommendations/)</sup>

## 独立性的代价

可预计算向量的代价体现在表示能力上。文档的单个向量必须服务于所有可能的查询，因此它是对文档主题的概括，而非对某个具体问题如何作答的陈述。<sup>[10](https://whatisrag.net/blogs/bi-encoder/)</sup> 《ACM 信息系统汇刊》上的稠密检索综述直接指出了这一局限：单表示双编码器无法很好地建模查询与文本之间细粒度的语义交互。<sup>[7](https://doi.org/10.1145/3637870)</sup>

BEIR 基准在零样本设置下跨 18 个多样化数据集评估了 10 种检索系统，使这一弱点变得具体：BM25 仍是稳健的基线，重排序和后期交互模型以高计算成本取得了最佳的零样本表现，而稠密和稀疏检索模型计算效率更高，但往往不如其他方法。<sup>[8](https://arxiv.org/abs/2104.08663)</sup> 在域内数据上击败 BM25 的双编码器稠密检索器，被发现在分布外评估中表现不佳。<sup>[7](https://doi.org/10.1145/3637870)</sup>

## 实践中的双编码器与交叉编码器

交叉编码器将拼接后的查询-文档输入通过完整的交叉注意力层处理，捕捉双编码器遗漏的细粒度交互信号。<sup>[4](https://inferensys.com/glossary/answer-engine-architecture/semantic-indexing-pipelines/bi-encoder)</sup> 生产环境的标准答案是两阶段检索：由双编码器（或 BM25 等词汇系统）快速检索出候选集，通常约 100 条，再由交叉编码器对这些候选重排序。<sup>[14](https://www.sbert.net/examples/sentence_transformer/applications/retrieve_rerank/README.html)</sup><sup> • </sup><sup>[11](https://www.dei.unipd.it/~faggioli/temp/clef2025/paper_276.pdf)</sup> 在 MTEB BEIR 基准上，BGE-Reranker-v2-m3 等交叉编码器可将 nDCG@10 相比双编码器基线提升 5–10 个点。<sup>[15](https://inferensys.com/differences/graphrag-vs-vector-rag-architectures/re-ranking-and-scoring-models/cross-encoder-vs-bi-encoder-for-reranking)</sup>

由于交叉编码器准确但昂贵，其知识常通过蒸馏迁移到双编码器：由交叉编码器教师为（查询，文档）对生成软标签或分数，用于训练更快的双编码器学生。<sup>[16](https://inferensys.com/glossary/retrieval-augmented-generation-architectures/cross-encoder-reranking/bi-encoder-vs-cross-encoder)</sup> 一个早期案例训练双编码器问答模型，使其在 8000 万个合成问答对上匹配更准确的交叉编码器的预测。<sup>[17](https://arxiv.org/abs/2106.08190)</sup> ColBERT 等后期交互模型居于两者之间，保留逐词元向量和 MaxSim 分数，以接近双编码器的延迟恢复接近交叉编码器的准确率。<sup>[18](https://llmbook.icsgen-ai.org/part-7-retrieval-information-extraction-with-llms/module-36-retrieval-tools/section-36.4.html)</sup>

## 历史与谱系

双编码器的双塔形态类似于 DSSM 等基于表示的 BERT 前模型。<sup>[7](https://doi.org/10.1145/3637870)</sup> 三个系统确立了现代范式。Sentence-BERT（Reimers 与 Gurevych，2019）使用孪生网络和三联体网络结构导出可用余弦相似度比较的句子嵌入，使 BERT 级别的语义可用于相似度搜索。<sup>[6](https://arxiv.org/abs/1908.10084)</sup> 稠密段落检索（Karpukhin 等，2020，Facebook AI Research）表明，检索可以仅用稠密表示实现，通过简单的双编码器框架从少量问题和段落中学习，并帮助端到端问答系统在多个开放域基准上取得新的最优结果。<sup>[5](https://arxiv.org/abs/2004.04906)</sup> 前一年的实体检索工作已经表明，双塔模型可以完全取代别名表候选生成。<sup>[12](https://arxiv.org/abs/1911.03814)</sup>

基于 BERT 的双编码器——查询和段落分别用独立或共享的 BERT 模型编码——成为神经检索的基础范式，此后的嵌入模型如 Linq-Embed-Mistral（2024）和 [Qwen3-Embedding](https://www.edgepedia.org/zh/qwen3-embedding)（2025）通过使用更大的语言模型作为底层编码器延续了这一范式。<sup>[19](https://openreview.net/pdf?id=4GRsedu43K)</sup> 零样本技术也建立在其上：[HyDE](https://www.edgepedia.org/zh/hypothetical-document-embeddings) 编码由指令遵循语言模型生成的假设文档，利用双编码器的嵌入空间将生成文本锚定到真实语料文档。<sup>[20](https://arxiv.org/abs/2212.10496)</sup>

## References

1. [Dual-Encoder, Envisioning Vocab](https://www.envisioning.com/vocab/dual-encoder)
2. [Learned Dense Retrieval survey (Bei Wang, University of Utah)](https://www.sci.utah.edu/~beiwang/publications/IR_Survey_BeiWang_2025.pdf)
3. [What is a Bi-Encoder? Definition & Architecture, Inference Systems](https://inferensys.com/glossary/semantic-search-and-entity-recognition/dense-passage-retrieval/bi-encoder)
4. [What is a Bi-Encoder? Architecture & Retrieval, Inference Systems](https://inferensys.com/glossary/answer-engine-architecture/semantic-indexing-pipelines/bi-encoder)
5. [Karpukhin et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. arXiv:2004.04906](https://arxiv.org/abs/2004.04906)
6. [Reimers & Gurevych (2019). Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks. arXiv:1908.10084](https://arxiv.org/abs/1908.10084)
7. [Dense Text Retrieval Based on Pretrained Language Models: A Survey, ACM Transactions on Information Systems](https://doi.org/10.1145/3637870)
8. [Thakur et al. (2021). BEIR: A Heterogeneous Benchmark for Zero-shot Evaluation of Information Retrieval Models. arXiv:2104.08663](https://arxiv.org/abs/2104.08663)
9. [Bi-encoder: dual-tower architecture for fast dense retrieval, ZeroEntropy](https://zeroentropy.dev/concepts/bi-encoder/)
10. [Bi-Encoder, The RAG Glossary](https://whatisrag.net/blogs/bi-encoder/)
11. [AGH IR at LongEval: Improving Scientific Information Retrieval with Dense Representations and Cross-Encoder Re-ranking, CLEF 2025](https://www.dei.unipd.it/~faggioli/temp/clef2025/paper_276.pdf)
12. [Scalable Zero-shot Entity Linking with Dense Entity Retrieval, arXiv](https://arxiv.org/abs/1911.03814)
13. [Mixed Negative Sampling for Learning Two-tower Neural Networks in Recommendations, Google Research](https://research.google/pubs/mixed-negative-sampling-for-learning-two-tower-neural-networks-in-recommendations/)
14. [Retrieve & Re-Rank, Sentence Transformers documentation](https://www.sbert.net/examples/sentence_transformer/applications/retrieve_rerank/README.html)
15. [Cross-Encoder vs Bi-Encoder for Reranking Guide, Inference Systems](https://inferensys.com/differences/graphrag-vs-vector-rag-architectures/re-ranking-and-scoring-models/cross-encoder-vs-bi-encoder-for-reranking)
16. [Bi-Encoder vs. Cross-Encoder: Neural Retrieval Architectures, Inference Systems](https://inferensys.com/glossary/retrieval-augmented-generation-architectures/cross-encoder-reranking/bi-encoder-vs-cross-encoder)
17. [Question Answering Infused Pre-training of General-Purpose Contextualized Representations. arXiv:2106.08190](https://arxiv.org/abs/2106.08190)
18. [Section 36.4: Models, Building Language AI](https://llmbook.icsgen-ai.org/part-7-retrieval-information-extraction-with-llms/module-36-retrieval-tools/section-36.4.html)
19. [OpenReview paper describing the BERT-based bi-encoder as the foundational paradigm for neural retrieval](https://openreview.net/pdf?id=4GRsedu43K)
20. [Gao et al. (2022). Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE). arXiv:2212.10496](https://arxiv.org/abs/2212.10496)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Prompting, reasoning, and agents

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/bi-encoder

English original: https://www.edgepedia.org/bi-encoder
