Sub topic 12
Training Data and Hard Negatives
This chapter assumes you know how a cross-encoder scores a query and passage together, how a bi-encoder retriever builds a candidate pool, and the basic vocabulary of query, passage, and relevance. You do not need to train a model to follow it.
What a cross-encoder learns from
A cross-encoder is trained on labeled pairs: a query, a passage, and a judgment of how relevant the passage is. The model learns to tell relevant passages from irrelevant ones. What counts as "irrelevant" in the training data decides what the model can separate later.
- A positive is a passage that answers the query.
- A negative is a passage that does not.
- A hard negative is a negative that looks relevant. It shares vocabulary or topic with the query but does not answer it.
A random negative is any passage picked from the corpus without looking at the query. Random negatives are usually easy. For "How do I rotate an API key?", a passage about invoice dates is obviously wrong, so the model learns almost nothing from it.
Why hard negatives matter
At inference time, the reranker only sees candidates that the first stage already thought were close. The passage "To change your password, open Account Settings" is a typical example. It is on the same subject area as the query, but it does not answer it. If training only used random negatives, the model would never practice separating such near misses.
Training negatives should therefore look like the first stage's mistakes. This is why the concept graph links first-stage retrieval to training data. The retriever that feeds your reranker is also the best source of its training negatives.
Mining hard negatives
The usual recipe has four steps:
- Run each training query through your first-stage retriever and keep the top N results, for example 30 to 100.
- Remove passages known to be relevant.
- Skip the very top ranks if your labels are sparse (see the false negative problem below).
- Sample a few of the remaining passages per query as negatives.
The code below shows the mining step. It assumes you already have ranked passage IDs from a retriever.
import random
def mine_hard_negatives(ranked_ids, positives, n_negatives=4, skip_top=0, seed=0):
candidates = [d for d in ranked_ids[skip_top:] if d not in positives]
rng = random.Random(seed)
rng.shuffle(candidates)
return candidates[:n_negatives]
ranked = ["d4", "d1", "d9", "d2", "d7", "d5", "d8"]
positives = {"d1"}
print(mine_hard_negatives(ranked, positives, n_negatives=3, skip_top=1))The false negative problem
Most datasets label only a few passages per query. Other passages may also answer the query but were never judged. If your retriever returns such a passage and you label it negative, you teach the model to push down a correct answer. These are false negatives.
The harder your mining, the more false negatives you get. Common ways to reduce them:
- Skip the top few ranks when mining, as in
skip_topabove. - Drop negatives that a strong existing reranker scores close to the positive.
- Review a small sample by hand before training.
Where labeled data comes from
| Source | Strength | Weakness |
|---|---|---|
| Public datasets such as MS MARCO | Large and ready to use | Web search domain may not match yours |
| Click or feedback logs | Real user intent | Biased toward items already ranked high |
| Human annotation | Highest quality | Slow and costly |
| LLM-generated queries or labels | Cheap and scalable | Needs spot checks for noise |
If your domain differs from the public data, adding even a modest set of in-domain pairs with mined negatives often helps more than more generic data.
Exercise
You build a reranker training set by taking the top 5 retriever results for each query and labeling every passage that is not the known positive as a negative. Training accuracy looks good, but the reranker sometimes pushes correct answers down on real queries. What is the likely cause, and what is one fix?
Show answer
Many of the top-ranked "negatives" are unlabeled relevant passages (false negatives), so the model learns to demote correct answers. Fixes include skipping the top few ranks when mining, filtering out negatives that a strong reranker scores close to the positive, or hand-checking a sample of negatives.