Week 10 · Search and retrieval

Compare keyword and dense retrieval with a known relevance label.

← Week 9 · Subject contents · Week 11 →

Compare keyword and dense retrieval with a known relevance label.

Work through the problem

A keyword scorer uses lexical overlap. Dense retrieval compares vector representations, often with cosine similarity. In the controlled example, fixed embeddings give truck and lorry nearby directions. That exposes the difference in retrieval behaviour while holding the tiny corpus constant.

Recall@k asks whether a labelled relevant passage appears among the first k hits in a one-relevant-document case. A higher semantic similarity can still rank a misleading passage first. Validate on queries with known relevant passages and include hard negatives.

Run the Python lab

import numpy as np
docs = ["lorry maintenance checklist","truck toy collection","salary payment policy"]
query = "truck maintenance"
dense = np.array([[1.,0.],[.6,.8],[0.,1.]])
q = np.array([1.,0.])
lexical = np.array([len(set(query.split()) & set(d.split())) for d in docs])
semantic = dense@q/np.linalg.norm(dense,axis=1)
for name, score in [("Keywords",lexical),("Toy dense",semantic)]:
    ranking = np.argsort(-score,kind="stable").tolist()
    print(name,"ranking",ranking,"recall@1",int(ranking[0]==0))
assert semantic.argmax()==0

Output from the lab

Keywords ranking [0, 1, 2] recall@1 1
Toy dense ranking [0, 1, 2] recall@1 1

Practice

  1. What happens on the query “truck toy”?

Check your reasoning

  1. These fixed query vectors have no compositional model. A real retriever must represent the whole query and be tested on such contrasts.

← Week 9 · Subject contents · Week 11 →

Further reading · Jurafsky and Martin · Speech and Language Processing