Sub topic 13
Training objectives for rerankers
This chapter assumes you know how a cross-encoder scores a (query, passage) pair, what hard negatives are, and basic PyTorch tensors. The training objective, also called the loss, decides what the model learns from those pairs. Three families are common: pointwise, pairwise, and listwise.
What the loss must teach
A reranker only needs to put relevant passages above irrelevant ones for the same query. The objectives differ in how much of that ordering the model sees at once.
Pointwise
Pointwise training treats each (query, passage) pair as an independent label. The model outputs one logit, and you apply binary cross-entropy against a 0/1 label. A graded label scale such as 0 to 3 also works with a regression loss.
- It is simple, and it works with any labeled data.
- It does not directly optimize ordering. A model can fit every label well and still rank two passages in the wrong order.
- Scores tend to sit near the label scale, which makes thresholds easier to set. They are still not calibrated probabilities, as the scoring chapter explained.
Pairwise
Pairwise training takes a positive and a negative passage for the same query. It pushes the positive's score above the negative's. The RankNet-style logistic loss is log(1 + exp(-(s_pos - s_neg))). A margin loss, max(0, margin - (s_pos - s_neg)), is the alternative.
This matches the ranking goal more closely. It also makes hard negatives matter. A random negative is separated trivially, so the loss goes to zero and teaches nothing. A near-miss negative gives a useful gradient.
Listwise
Listwise training scores one positive with several negatives in a single group. It applies softmax cross-entropy over the group, so the positive must beat all negatives together. This is often called a localized contrastive loss. Other listwise losses, such as LambdaRank and ListNet, weight errors by their effect on a metric like nDCG.
Listwise training sees the most context per step. Its cost is memory, because each query needs a full group of forward passes. Group size is a real hyperparameter, commonly 8 to 32 passages.
All three on one example
The script below computes each loss on the same four scores. Passage 0 is relevant.
import torch
import torch.nn.functional as F
scores = torch.tensor([[2.1, 0.3, -0.5, 1.0]]) # one query, four passages
labels = torch.tensor([[1.0, 0.0, 0.0, 0.0]]) # passage 0 is relevant
pointwise = F.binary_cross_entropy_with_logits(scores, labels)
pos = scores[:, 0:1]
neg = scores[:, 1:]
pairwise = F.softplus(-(pos - neg)).mean() # log(1 + exp(-(s_pos - s_neg)))
listwise = F.cross_entropy(scores, torch.tensor([0]))
print(f"pointwise: {pointwise.item():.3f}")
print(f"pairwise: {pairwise.item():.3f}")
print(f"listwise: {listwise.item():.3f}")The negative with score 1.0 contributes the most to the pairwise and listwise losses. It is the hard negative. The pointwise loss does not single it out.
Choosing an objective
| Objective | Input per step | Strength | Weakness |
|---|---|---|---|
| Pointwise | One pair | Simple, works with graded labels | Ignores relative order |
| Pairwise | Positive and negative | Matches the ranking goal | Needs good negatives |
| Listwise | One positive, many negatives | Most context, strong results | Higher memory and compute |
A practical default is listwise softmax cross-entropy with hard negatives mined from your first-stage retriever. Fall back to pointwise when you only have noisy, graded, or thresholded labels.
Exercise
You train with pairwise loss, but each query's negatives are randomly sampled passages from the corpus. Training loss falls quickly, yet reranking barely improves on the first-stage ranking. Why, and what do you change?
Show answer
Random negatives are easy to separate, so the loss reaches near zero without the model learning fine distinctions. Mine hard negatives from the first-stage retriever, which are the passages it ranks highly but that are not relevant. Filter likely false negatives. Consider moving to a listwise loss so each positive competes against a full group of them.