Sub topic 13
Training Objectives for Rerankers
A cross-encoder produces one number for a (query, passage) pair. The training objective decides what that number means. This chapter covers the three families of objectives (pointwise, pairwise, listwise), what each one teaches the model, and how to pick one.
This chapter assumes you know how a cross-encoder reads query and passage together, how hard negatives are mined, and what raw scores look like at inference time. You should also be comfortable with PyTorch tensors and the idea of a loss function.
The shared setup
All three families train the same architecture: a transformer reads [CLS] query [SEP] passage [SEP], and a linear head maps the [CLS] representation to a single logit. The difference lies in how those logits are compared with labels.
Training data is usually organized as groups. Each group has one query, one or more positives, and several negatives. The negatives often come from the first-stage retriever, so they are the hard negatives from the previous chapter.
- A pointwise loss looks at each pair on its own.
- A pairwise loss looks at a positive and a negative from the same query.
- A listwise loss looks at the whole group at once.
Pointwise objectives
A pointwise objective treats reranking as classification or regression on single pairs.
With binary labels, the standard choice is binary cross-entropy on the logit:
- Label 1 for relevant pairs, 0 for non-relevant pairs.
- Loss is
-[y log σ(s) + (1-y) log(1-σ(s))], averaged over pairs.
This is the simplest objective, and it is the default for many public checkpoints, including the MS MARCO MiniLM models used earlier in this tutorial. It works with any labeled pair data, including data with no group structure.
Pointwise training has two weaknesses.
- It ignores ordering. The loss never asks whether a relevant passage outscored an irrelevant one for the same query. It only asks each pair to land near its own label. A query with easy negatives and a query with hard negatives are pushed toward the same absolute targets.
- It is sensitive to class balance. If you sample 1 positive for every 30 negatives, the model can lower the loss by predicting "not relevant" almost everywhere. Control the ratio or weight the positive class.
The upside is that scores tend to look probability-like, because the sigmoid of the logit is trained to match a 0/1 label. As the scoring chapter warned, that does not make them calibrated across domains. It only means the numbers have a stable scale within one trained model.
Graded labels (for example 0 to 3) can use regression with mean squared error, or the labels can be rescaled to [0, 1] and fed to binary cross-entropy.
Pairwise objectives
A pairwise objective cares only that the positive scores higher than the negative for the same query. Absolute values do not matter.
Two common forms:
- RankNet loss:
log(1 + exp(-(s_pos - s_neg))). This is the logistic loss on the score difference. - Margin (hinge) loss:
max(0, m - (s_pos - s_neg)). It stops pushing once the positive beats the negative by marginm.
Pairwise training matches the job at inference time, since reranking is sorting. It is also less sensitive to label imbalance because every term contains one positive and one negative.
Its main weakness is that all pairs count equally. Fixing a swap between ranks 49 and 50 contributes as much to the loss as fixing a swap between ranks 1 and 2, but only the second matters for most applications. Pairwise losses also scale as positives × negatives per query, which adds cost when groups are large.
A related idea is margin MSE distillation. A strong teacher scores the (positive, negative) pair, and the student is trained to reproduce the teacher's score difference. It needs no human labels beyond the teacher's output, and it carries soft information about how much better the positive is. Its quality is capped by the teacher.
Listwise objectives
A listwise objective scores a whole group and compares it with the desired ordering.
The most widely used form is softmax cross-entropy over the group. With the positive at index 0 and N negatives:
loss = -log( exp(s_0) / Σ_j exp(s_j) )
This treats the group as a multi-class problem where the model must pick the positive. Every negative in the group pushes against the positive at the same time, with harder negatives (higher scores) contributing larger gradients. This is the same shape of loss used for in-batch negatives in bi-encoder training, applied to joint scores.
Other listwise variants:
- ListNet compares a softmax distribution over predicted scores with a softmax distribution over graded labels.
- ListMLE maximizes the likelihood of the ideal permutation.
- LambdaRank and LambdaLoss scale pairwise gradients by how much swapping two documents would change nDCG. They tie the training signal directly to the metric you report.
Listwise training fits the task best, but it costs the most. Each query needs a whole group in a batch, so a batch of 16 queries with groups of 8 means 128 forward passes through a transformer that reads both texts at once. Group size also becomes a training hyperparameter. Groups that are too small give a weak signal, and groups that are too large exhaust memory.
A runnable comparison
The code below computes all three families from the same scores. It uses PyTorch only, so you can run it without downloading a model.
import torch
import torch.nn.functional as F
# One query, one positive (index 0), three negatives.
scores = torch.tensor([[1.2, 0.8, -0.5, 0.3]])
labels = torch.tensor([[1.0, 0.0, 0.0, 0.0]])
# Pointwise: binary cross-entropy on each logit.
pointwise = F.binary_cross_entropy_with_logits(scores, labels)
# Pairwise: positive logit against each negative logit.
pos = scores[:, :1]
neg = scores[:, 1:]
ranknet = F.softplus(-(pos - neg)).mean()
hinge = F.relu(1.0 - (pos - neg)).mean()
# Listwise: softmax cross-entropy, target is index 0 in each group.
target = torch.zeros(scores.size(0), dtype=torch.long)
listwise = F.cross_entropy(scores, target)
print(f"pointwise BCE : {pointwise.item():.3f}")
print(f"pairwise RankNet: {ranknet.item():.3f}")
print(f"pairwise hinge: {hinge.item():.3f}")
print(f"listwise softmax: {listwise.item():.3f}")
def group_listwise_loss(flat_scores, group_size):
"""flat_scores holds B * group_size logits, positive first in each group."""
grouped = flat_scores.view(-1, group_size)
target = torch.zeros(grouped.size(0), dtype=torch.long)
return F.cross_entropy(grouped, target)
print(group_listwise_loss(scores.view(-1), group_size=4).item())The helper group_listwise_loss shows the practical detail: the model scores a flat batch of pairs, and you reshape the logits into (queries, group_size) before applying the loss. This only works if the data loader keeps each query's candidates together and puts the positive in a known position.
Comparing the objectives
| Objective | Needs groups | Learns | Cost per query | Main risk |
|---|---|---|---|---|
| Pointwise (BCE) | No | Absolute relevance per pair | Low | Ignores ordering, class imbalance |
| Pairwise (RankNet, hinge) | Yes | Positive above negative | Medium | All swaps weighted equally |
| Listwise (softmax CE) | Yes | Positive above the whole group | Higher | Memory, group size choice |
| Metric-aware (LambdaLoss) | Yes | Ordering weighted by nDCG gain | Higher | More tuning, harder to debug |
Recent versions of sentence-transformers include cross-encoder loss classes for several of these families (binary cross-entropy, RankNet, ListNet, ListMLE, LambdaLoss, and margin MSE). Check the documentation for your installed version, because the class names and the expected dataset format have changed between releases.
Interaction with hard negatives
The objective and the negative sampling strategy work together.
- Softmax listwise loss with hard negatives gives strong gradients, because the hardest negative dominates the denominator. This sharpens the top of the ranking.
- False negatives hurt more under listwise and pairwise losses. If an unlabeled but relevant passage is treated as a negative, the model is explicitly pushed to score it below the labeled positive. Pointwise loss makes the same mistake but with a weaker signal. Filter negatives that a stronger model scores close to the positive, or drop the top few retrieved passages when mining.
- Easy negatives only (random passages) make every objective trivial. The model learns topic matching, which the bi-encoder already does.
Train on negatives that look like what your first stage returns at serving time. If your production candidates come from hybrid retrieval, mine negatives from the hybrid pipeline too.
Choosing an objective
Use these criteria in order.
- What data do you have? Isolated labeled pairs allow only pointwise training. Grouped judgments or mined negatives open up pairwise and listwise training.
- What do you need from the scores? If a downstream step applies a fixed cutoff, a pointwise-trained model gives a more stable scale. If you only sort and take the top k, ordering objectives are enough.
- What can you afford? Listwise groups multiply training compute by the group size.
- What do you report? If nDCG@10 is the target metric, a metric-aware loss is worth trying. Always compare against the pointwise baseline on held-out queries.
Exercise
You fine-tune a cross-encoder with softmax cross-entropy over groups of 1 positive and 15 hard negatives. Validation nDCG@10 drops after training, and you find that many of the "negatives" are near-duplicates of the positive passage. Which part of the setup is most likely causing this, and what would you change?
Show answer
False negatives in the group. Under a listwise softmax loss, each near-duplicate is pushed below the labeled positive, which teaches the model to penalize passages that are actually relevant. Filter out negatives that score very close to the positive under a strong reference model (or deduplicate the corpus), then retrain. Using a pointwise loss would only soften the problem, not remove it.
Exercise
A team has only isolated (query, passage, 0/1) labels with no grouping and wants to train a first reranker quickly. Which objective family fits, and what should they watch for?
Show answer
Pointwise binary cross-entropy, because it needs no group structure. They should check the ratio of positives to negatives, since heavy imbalance lets the model reduce the loss by predicting "not relevant" for everything. They should also evaluate ordering with MRR or nDCG, because pointwise loss does not optimize ordering directly.