Before You Start
- Required knowledge
- Tokenization, TF-IDF, cosine similarity, and matrix factorization.
- Libraries used
- NumPy
- Dataset and source
- Embedded documents and relevance-labeled queries; no external embedding service.
- Expected output
- Sparse and dense retrieval rankings with Recall@k and MRR evaluation.
Lesson Videos
Embeddings for Retrieval โ Theory & Concepts
Read this before attempting the exercises. This lab is intentionally small and offline. The goal is not to use a production embedding API yet, but to understand the retrieval pipeline end to end.
1. What This Lab Actually Builds
This notebook implements a toy retrieval system with two baselines:
query
-> tokenize
-> vectorize
-> compare against document vectors
-> rank documents by similarity
-> evaluate with Recall@k and MRR
The two retrieval baselines are:
- โTF-IDF: sparse lexical matching
- โco-occurrence + PPMI + SVD + mean word vectors: a small dense-ish semantic baseline
So this lab is about retrieval mechanics, similarity, and evaluation more than about large pretrained embedding models.
2. Sparse Baseline: TF-IDF
TF-IDF gives each document a sparse vector over the vocabulary.
TF: how often a word appears in this document
IDF: how rare that word is across documents
TF-IDF = TF ร IDF
Why it works:
- โimportant topic words get larger weight
- โcommon words get downweighted
- โretrieval is simple cosine similarity over sparse vectors
Why it fails:
- โit depends on word overlap
- โit does not naturally connect synonyms
- โquery/document wording differences hurt retrieval quality
In this notebook, TF-IDF is the first baseline because it is transparent and easy to debug.
3. Dense-ish Baseline in This Lab
The second baseline is not a modern sentence-transformer. It is a small classic pipeline:
documents
-> tokenize
-> build word co-occurrence matrix
-> convert to PPMI matrix
-> apply SVD
-> get word vectors
-> average word vectors to form document vectors
This gives you a dense-ish document embedding from corpus statistics alone.
Co-occurrence
If two words appear in similar contexts, their vectors should become similar.
PPMI
Raw co-occurrence overweights very frequent words. PPMI corrects this by emphasizing word pairs that co-occur more than chance would predict.
SVD
SVD compresses the large co-occurrence-based matrix into a lower-dimensional representation while keeping the strongest structure.
Mean document vector
A document vector is formed by averaging the normalized word vectors of the words in that document.
This is a simple baseline, but it already shows why dense retrieval can sometimes recover semantic similarity better than TF-IDF.
4. Similarity and Normalization
The notebook uses cosine similarity.
cosine(q, d) = (q ยท d) / (||q|| ||d||)
Why normalize:
- โit removes the effect of raw vector magnitude
- โranking depends on direction rather than scale
- โdocument length matters less after normalization
If both query and document vectors are already L2-normalized, then:
cosine similarity = dot product
That identity is important in retrieval systems because many vector databases optimize dot-product search on normalized embeddings.
5. What the Evaluation Means Here
Each query in this toy lab has one gold document ID.
So the metrics are deliberately simple:
Recall@k
For this notebook, Recall@k is effectively:
Did the correct document appear anywhere in the top-k?
MRR
MRR cares about how early the first correct result appears:
rank 1 -> 1.0
rank 2 -> 0.5
rank 3 -> 0.333...
So:
- โRecall@k measures whether you retrieved the answer at all
- โMRR measures how high you ranked it
Both matter in real search and RAG systems.
6. What This Lab Is Not Doing Yet
This notebook does not yet cover:
- โANN indexing or FAISS
- โvector databases
- โchunking and context packing for RAG
- โbi-encoder / cross-encoder reranking pipelines
- โpretrained sentence embedding APIs
Those belong to later labs in this week.
So if you see a simple dense baseline here, that is intentional: the lab is teaching the retrieval core first.
7. Practical Takeaways
After this lab, you should be able to explain:
- โwhy TF-IDF is a strong lexical baseline
- โwhy dense embeddings can recover semantic similarity beyond exact word overlap
- โwhy normalization matters for retrieval
- โwhy Recall@k and MRR capture different retrieval behavior
- โwhy a retrieval system should always be judged by ranking quality, not by intuition alone
8. Interview Framing
A strong answer for this lab is:
I would start with a sparse baseline like TF-IDF,
then compare it against a dense embedding baseline,
then evaluate both with Recall@k and MRR,
because better retrieval is about ranking quality, not just vector construction.
That is the main idea this notebook is trying to teach.
Ready to practice?
Setup