﻿# Contrastive dual encoders learn from positive and negative pairs

Contrastive dual encoders learn to score matching query-document pairs above negatives. Understand in-batch candidates, false negatives and representation geometry.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/contrastive-dual-encoder-negatives/
Language: en

A dual encoder maps queries and documents into representations that can be compared efficiently. Contrastive training supplies matching pairs and competing candidates, encouraging the matching score to stand out. The negative set is therefore part of the objective, not an incidental collection of unused examples around the positive pair.

## Independent encoders produce comparable representations

A query representation and a document representation are computed separately, then compared using a declared similarity such as a dot product. The encoding weights may be shared or separate depending on the architecture. Document representations can be computed ahead of a query, which supports efficient retrieval. This independence differs from jointly encoding every query-document pair. It also means the chosen representation must preserve information needed by the similarity function, because the final score cannot introduce arbitrary new token-level interaction that was never represented.

## The loss compares a positive with alternatives

For a query with a labeled matching document, a contrastive objective can normalize scores over that document and selected negatives. Raising the matching score relative to alternatives reduces the corresponding loss. Consider a query about lunar eclipses with a matching astronomy paragraph and unrelated travel paragraphs. The objective teaches a relative ordering within this candidate set. It does not directly calibrate the score into a universal probability of relevance for any future corpus, because normalization depends on which candidates participated in the comparison.

## In-batch negatives reuse other labeled documents

Documents paired with other queries in the batch can serve as competing candidates. This expands comparisons without independently gathering every negative. The assumption can fail when another query’s document is also relevant to the current query. Such a false negative receives an unfavorable training role despite legitimate relevance. Hard negatives introduce related but nonmatching candidates, which can sharpen distinctions while requiring careful labeling. Batch composition and negative mining therefore affect the learning signal, not just hardware occupancy or the convenience of data loading.

## Evaluate retrieval beyond the training candidate set

Inspect whether held-out queries retrieve useful documents from the intended corpus, using declared relevance judgments and ranking metrics. Separate encoder quality from index approximation and corpus freshness. A tiny score matrix with labeled positives can verify the loss construction before a large run, including the exact candidate denominator. Document the negative policy and similarity normalization so the experiment can be reproduced. This makes the geometry and supervision explicit while avoiding the claim that every nonmatching pair is semantically wrong or that a high training similarity alone proves a useful retrieval system.

## Primary research and implementation references

- [ColBERT: Efficient and Effective Passage Search via Contextualized Late Interaction](https://arxiv.org/abs/2004.12832).

- [Dense Passage Retrieval for Open-Domain Question Answering](https://arxiv.org/abs/2004.04906).

- [Sentence-BERT: Sentence Embeddings Using Siamese BERT-Networks](https://arxiv.org/abs/1908.10084).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
