Sub topic 5
Reading cross-encoder scores
This chapter assumes you have built the retrieve-then-rerank pipeline from the previous chapter. You should be comfortable with NumPy arrays and with what a logit is (an unbounded number a model produces before any squashing function).
What the number is
A cross-encoder such as cross-encoder/ms-marco-MiniLM-L-6-v2 reads a (query, passage) pair and outputs one number. That number is a relevance score, trained so that relevant pairs score higher than irrelevant ones. Only the ordering is guaranteed to be meaningful.
Many rerankers output a raw logit, which can be negative and has no fixed upper bound. Others pass the logit through a sigmoid and return a value between 0 and 1. The output depends on the model and the library version, so inspect a few outputs before you build on them.
import numpy as np
from sentence_transformers import CrossEncoder
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2", max_length=512)
def score_pairs(query, passages):
return np.asarray(model.predict([(query, p) for p in passages]), dtype=float)
passages = [
"API keys can be rotated from the Security page.",
"Our API supports JSON and XML response formats.",
"To change your password, open Account Settings.",
]
for query in ["How do I rotate an API key?", "What is the capital of France?"]:
scores = score_pairs(query, passages)
print(query)
print(" range:", scores.min().round(2), "to", scores.max().round(2))
print(" best: ", passages[int(scores.argmax())])The second query has no relevant passage, yet the code still prints a "best" passage. The model always returns scores, and the top score for an unanswerable query can be low or merely mediocre.
Rules for using scores
- Compare scores only within one query. A score of 3.0 for one query does not mean the same as 3.0 for another. Different queries have different score distributions.
- Treat the scale as uncalibrated. A sigmoid output of 0.9 is not a 90% chance of relevance. The model was trained to rank, not to estimate probabilities.
- Prefer rank-based decisions. Keep the top k passages. This works without any assumption about the scale.
- Calibrate before thresholding. If you must drop low-scoring passages, pick a cutoff from labeled data for your own corpus.
| Decision | Safe with raw scores? | Why |
|---|---|---|
| Sort candidates for one query | Yes | Only order matters |
| Keep top 5 | Yes | Rank-based |
| Fixed cutoff such as "score > 0.5" | Only after tuning | Scale differs by model and domain |
| Compare scores across queries | No | Distributions shift per query |
| Report as "confidence" to users | No | Not a probability |
Choosing a cutoff from data
Run the reranker on queries where you know the relevant passages. Then compare the score distributions of relevant and non-relevant pairs, and choose the threshold that gives the precision you need.
import numpy as np
def best_threshold(scores, labels, min_precision=0.9):
scores = np.asarray(scores, dtype=float)
labels = np.asarray(labels, dtype=bool)
for t in np.sort(np.unique(scores)):
kept = scores >= t
if kept.sum() and labels[kept].mean() >= min_precision:
return float(t)
return None
scores = [7.1, 5.4, 2.0, 0.3, -1.2, -3.8]
labels = [1, 1, 1, 0, 0, 0]
print(best_threshold(scores, labels))This prints 2.0. Treat that threshold as valid only for this model and this kind of data. Recompute it if you change the model, the passage length, or the domain.
Common mistakes
- Assuming scores lie in 0 to 1 and then seeing negative values. Those values are logits.
- Averaging or adding a reranker score to a first-stage similarity score. The two scales differ, so rank fusion (covered later) is safer.
- Reading a small score gap between ranks 1 and 2 as a clear win. Near-ties are common, and their order is unreliable.
Exercise
A reranker returns a top score of 4.2 for query A and 1.1 for query B. Can you conclude that the top passage for A is more relevant than the top passage for B? Name one safe way to filter low-quality results.
Show answer
No. Scores are not calibrated across queries, so the two numbers are not comparable. A safe way to filter is to keep the top k passages by rank, or to tune a cutoff on labeled queries for this specific model and corpus.