Hugging Face, End to EndLesson 54 of 75

Module 6 · Embeddings, and search that understands

The second pass that fixes the first

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 7 of 9 in this chapter

This chapter’s goalBuild and measure a semantic search over your own documents: choosing an embedding model against your language and text length, chunking deliberately, normalising vectors for the similarity you use, adding a cross-encoder reranker, and reporting recall@k on questions you wrote yourself.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:08Not available
Full audiobookNot availableNot available
Where the time goes in one retrieval requestEmbed the query5 msSearch a million vectors10 msRerank the top fifty on aCPU100 msAbout a hundred and twenty milliseconds before any generation begins, and the rerank is four fifths ofit. So that is the step to tune, by retrieving fewer candidates or moving the cross-encoder to a GPU.It is also where most of the accuracy comes from, which is why it is worth the time.
About a hundred and twenty milliseconds before any generation begins, and the rerank is four fifths of it. So that is the step to tune, by retrieving fewer candidates or moving the cross-encoder to a GPU. It is also where most of the accuracy comes from, which is why it is worth the time.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Two architectures, and why both exist

A bi-encoder (everything so far) encodes the query and the document separately and compares vectors. The document’s vector is computed once, in advance, so searching a million documents is a million dot products. Fast, and the comparison is crude, because the model never saw the query and the document together.

A cross-encoder takes the query and the document as one input and outputs a single relevance score:

Python
from sentence_transformers import CrossEncoder

ce = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
print(ce.predict([("where is my parcel", "Your order shipped on Tuesday."),
                  ("where is my parcel", "How to change your password.")]))
# [ 8.21 -6.94 ]

Every query token can attend to every document token, so the model can tell that a document answers a question rather than merely being about the same topic. That is exactly the weakness the first lesson named.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘The second pass that fixes the first’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/reranking-with-cross-encoders (Accessed: 27 November 2026).