Sub topic 2
Reading Cross-Encoder Scores
Assume you know Python, embeddings, vector search, and basic retrieval terms. A cross-encoder receives a query and passage together, then produces a relevance score for that pair.
Reading cross-encoder scores
A score is usually a model logit or regression value. It ranks passages, but it is not automatically a probability. A score of 2.0 does not mean “80% relevant,” and scores from different models are not directly comparable.
Use scores to order candidates from the same model and run. Higher is usually better, but verify the model documentation. If you need probabilities, calibrate the model on labeled data.
from sentence_transformers import CrossEncoder
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
query = "How do I rotate an API key?"
passages = [
"Rotate API keys from the Security page.",
"Our API returns JSON by default.",
]
scores = model.predict([(query, passage) for passage in passages])
for passage, score in sorted(
zip(passages, scores), key=lambda item: item[1], reverse=True
):
print(f"{score:.2f} {passage}")The reranker can only reorder passages it receives. It cannot recover a relevant passage missing from the first-stage candidate pool.
Measuring whether reranking helps
Compare the original first-stage order with the reranked order on labeled queries. Keep the candidate pool and final cutoff consistent.
- Recall@k measures whether relevant documents appear in the top
k. It is especially important for checking the first-stage pool. - MRR is mean reciprocal rank. For each query, it rewards the position of the first relevant result.
- nDCG evaluates the ordering of documents with graded relevance labels.
import math
def reciprocal_rank(ranked_ids, relevant):
for rank, doc_id in enumerate(ranked_ids, start=1):
if doc_id in relevant:
return 1 / rank
return 0.0
before = ["d3", "d7", "d1"]
after = ["d1", "d3", "d7"]
print(reciprocal_rank(before, {"d1"})) # 0.333...
print(reciprocal_rank(after, {"d1"})) # 1.0How many candidates to rerank
Reranking more candidates can improve recall, but cross-encoder work grows roughly linearly with candidate count. Start with a practical depth such as 20–100, then measure quality and latency.
Candidate depth must be large enough to contain relevant passages. If first-stage recall@50 is poor, increasing reranking depth cannot fix the missing documents. Batch pairs to improve throughput, while watching memory and response-time limits.
Exercise
A relevant passage is absent from the top 50 first-stage results. Will reranking 100 candidates retrieve it?
Show answer
No. Reranking can reorder only its input candidates. Increase first-stage recall or retrieve a deeper pool.