Lab: pooled retrieval against token-level MaxSim
The language chapter's second lab (LNG-B). Averaging a document's token vectors dilutes the few that match a query; late interaction keeps every token. Compare both with recall at 1.
What runs in CodeSplash
- LNG054Dense Passage Retrieval (DPR) Runs in CodeSplash
- LNG055ColBERT late-interaction retrieval Runs in CodeSplash
- Semantic search (BM25, sentence embeddings, hybrid) Not verified yet
You will be able to
- Score documents by the cosine of mean-pooled token vectors
- Score documents by MaxSim, the late-interaction rule
- Compare retrievers with recall at k on queries you didn't tune on
- Weigh MaxSim's accuracy against its storage and compute cost
The question
ReadDense retrievalDense retrievalSearch that embeds each text and query and ranks texts by cosine similarity, so a match can share meaning without sharing words. usually squeezes a document into one vector. When a short query matches two tokens of a ten-token document, averaging those ten vectors dilutes the match. MaxSimColBERT MaxSimScores a text by finding, for each query token vector, its best match among the text's token vectors, then summing. keeps a vector per token and adds, for each query token, its best match in the document. Which ranks the right document first, and at what cost? The lab answers on a toy set where each query was written for one document.
Explore it
ExploreDExplorable · Retrieval: BM25, dense vectors and late interaction
When does matching words beat matching meanings, and how do we score a ranking?
Query
Each query has graded relevance labels for the ten documents.
Method
BM25 and TF-IDF match words; the toy dense vectors and MaxSim match themes.
Metrics look at the top k documents.
How fast repeated words stop adding score.
How much long documents are penalized.
- 1●○Car insurance prices rise again this year2.12
- 2●●How much does vehicle maintenance cost per year2.01
- 3○○Bayesian priors and posterior distributions explained0.00
- 4○○Weather forecast: heavy rain and strong wind expected tomorrow0.00
- 5○○A beginner's guide to the p-value and multiple testing correction0.00
- 6○○Baking bread at home: flour, water, yeast and salt0.00
- 7○○Storm warning issued as rainfall totals climb0.00
- 8●●Fixing an automobile engine: a mechanic explains common repairs0.00
- 9○○Bonferroni and Holm corrections control the familywise error rate0.00
- 10○○Sourdough starter tips for better loaves0.00
- nDCG@3
- Recall@3
- nDCG@k
- 0.601
- Recall@k
- 0.667
- Precision@k
- 0.667
- Reciprocal rank
- 1.000
- Puzzle: For “car repair cost”, find a method that puts both very relevant documents in the top 3.
- Puzzle: For the query “holm”, find a method with reciprocal rank 1.
BM25 for “car repair cost”: nDCG@3 0.601, recall@3 0.67, reciprocal rank 1.00. The top document is “Car insurance prices rise again this year”. The dense vectors here are a hand-made toy with four themes, not a trained encoder.
Switch between word matching, pooled vectors and MaxSim, and watch recall and nDCG change. Press Break it to see how a long document's pooled vector drifts toward the average of everything.
Do it yourself
ExploreThe notebook ranks eight documents for eight queries both ways, with the same embeddings in Python and R, and scores each retriever with recall at 1.
Notebook exercise · ~12 min
Pooled retrieval against token-level MaxSim
Rank documents for short queries by the cosine of averaged token vectors and by late-interaction MaxSim, and score both with recall at 1.
- Pooled retrieval
For each query, score every document by the cosine between the query's mean token vector and the document's mean token vector. Store the name of the top document for each query, in query order, in
pooled_top.Show a solution
Pythondef pooled(q, d): return cosine(E[q].mean(0), E[d].mean(0)) pooled_top = [names[int(np.argmax([pooled(q, d) for d in docs]))] for q in queries]Rpooled <- function(q, d) cosine(colMeans(E[q, , drop = FALSE]), colMeans(E[d, , drop = FALSE])) pooled_top <- sapply(queries, function(q) names[which.max(sapply(docs, function(d) pooled(q, d)))]) - Late interaction with MaxSim
Now score each document by MaxSim: for every query token take its largest dot product with any token of the document (the vectors have unit length, so that is a cosine) and add them up. Store the top document's name for each query in
maxsim_top.Show a solution
Pythondef maxsim(q, d): return (E[q] @ E[d].T).max(axis=1).sum() maxsim_top = [names[int(np.argmax([maxsim(q, d) for d in docs]))] for q in queries]Rmaxsim <- function(q, d) sum(apply(E[q, , drop = FALSE] %*% t(E[d, , drop = FALSE]), 1, max)) maxsim_top <- sapply(queries, function(q) names[which.max(sapply(docs, function(d) maxsim(q, d)))]) - Recall at 1
Recall at 1 is the share of queries whose top document is the relevant one. Store the two recalls, pooled first and MaxSim second, in
recall1.Show a solution
Pythonrecall1 = [np.mean(np.array(pooled_top) == relevant), np.mean(np.array(maxsim_top) == relevant)]Rrecall1 <- c(mean(pooled_top == relevant), mean(maxsim_top == relevant))
In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.
In CodeSplash
In the appSemantic search in Analyze ranks a text column for your queries by words (BM25), by sentence embeddings or both, and can rerank the top candidates with ColBERT MaxSim, a one-time download you approve. Compare the rankings on a few queries with known answers before trusting either.
Check: the lab
CheckpointCheck yourself
Why does pooled retrieval miss a document that contains both query tokens exactly?
Show the answer
Answer: a
A mean vector summarizes everything at once. A few exact matches among many other tokens move it only a little.
Check yourself
What does late interaction cost compared with one vector per document?
Show the answer
Answer: a
That's why it's often used to rerank a short list from a cheaper first-stage retriever, as Semantic search does.
Recap
Recap- Pooling dilutes a few exact matches among many other tokens.
- MaxSim sums each query token's best document match.
- Compare retrievers with recall on queries you didn't tune on, and count the cost.
Concepts in this lesson