A bi-encoder (short for "bidirectional encoder") is a type of neural network architecture designed to convert two different pieces of input (typically text) into separate, dense numerical representations (vectors or embeddings) that can then be easily compared for similarity.
The "bi" refers to the fact that there are two distinct encoder networks, even if they share the same underlying architecture and potentially some weights.
How a Bi-Encoder Works:
Two Independent Encoders: It consists of two separate encoder models. Let's call them Encoder A and Encoder B. These are often transformer-based models like BERT, RoBERTa, or specialized models like Sentence-BERT (SBERT).
Separate Input Processing:
Input 1 (e.g., a search query) goes into Encoder A, producing a query embedding (vector).
Input 2 (e.g., a document, a candidate answer, or another sentence) goes into Encoder B, producing a document/candidate embedding (vector).
Crucially, these two inputs are processed independently. The encoders do not "see" both inputs simultaneously during the encoding phase.
Similarity Comparison: Once both embeddings are generated, a similarity metric (most commonly cosine similarity) is used to calculate how alike the two vectors are. A higher similarity score indicates a stronger semantic relationship between the two original inputs.
Efficiency for Retrieval/Search: This is the primary advantage.
Pre-computation: Document embeddings can be pre-computed offline and stored in a vector database (or FAISS index).
Fast Query Time: When a new query comes in, only the query needs to be encoded. Then, its embedding is quickly compared against the millions of pre-computed document embeddings using efficient similarity search algorithms. This makes it incredibly fast for large-scale information retrieval.
Scalability: Can handle very large corpuses of documents because the search process becomes a vector similarity search, not a text-to-text comparison.
Flexibility: The generated embeddings can be used for various tasks beyond just direct comparison, such as clustering, semantic routing, or cold-start recommendations.
Semantic Understanding: Unlike keyword-based search, bi-encoders understand the meaning of the query and documents, allowing for more relevant results even if exact keywords aren't present.
How Bi-Encoders are Trained:
Bi-encoders are typically trained end-to-end on pairs of data:
Positive Pairs: A query and a highly relevant document/answer. The model learns to make their embeddings close in vector space.
Negative Pairs: A query and an irrelevant document/answer. The model learns to push their embeddings far apart.
Common training objectives include:
Contrastive Loss: Minimizing the distance between positive pairs and maximizing it for negative pairs.
Triplet Loss: Using an anchor, a positive example, and a negative example to enforce a margin between positive and negative distances.
Multiple Negative Ranking (MNR) Loss: A very popular method where for a given query, multiple negative documents are considered simultaneously, and the model learns to rank the positive document higher than all negatives.
Disadvantages/Limitations:
Less Contextual Interaction: Because the inputs are encoded independently, the bi-encoder cannot capture the very fine-grained, token-level interactions between the query and the document that a cross-encoder can (where both texts are concatenated and fed into a single transformer for joint processing). This can sometimes lead to slightly lower accuracy for ranking the top few results.
Training Complexity: Requires effective strategies for selecting negative samples to train robust models.
Common Use Cases:
Information Retrieval / Search Engines: Retrieving relevant documents for a user's query.
Question Answering Systems: Finding candidate answers or passages that semantically match a question.
Recommendation Systems: Finding similar items (e.g., similar movies, products).
Duplicate Detection: Identifying semantically identical or very similar pieces of text.
Semantic Clustering: Grouping similar sentences or documents together.
Example Model:
The most well-known example of a bi-encoder is Sentence-BERT (SBERT), which greatly popularized the use of bi-encoders for efficient semantic similarity tasks.