S30 Logo
S30 AI Labwww.thes30.com
Back
#51

Embeddings for Retrieval

Hard๐Ÿ”Ž RAG & RetrievalW11 D1

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.

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?

Python 3 โ€” Notebook
0/7 solved
1
Dataset & Setup

Setup

Loading editor...
2
Section 0 โ€” Corpus + labeled queries
2

Section 0 โ€” Corpus + labeled queries

Loading editor...
3
Section 1 โ€” Tokenization + vocabulary
1

Section 1 โ€” Tokenization + vocabulary

Loading editor...
4
Section 2 โ€” Similarity metrics

Section 2 โ€” Similarity metrics

Loading editor...
5
Section 3 โ€” TF-IDF retrieval baseline

Section 3 โ€” TF-IDF retrieval baseline

Loading editor...
6
Section 4 โ€” Dense-ish baseline: co-occurrence + SVD word vectors

Section 4 โ€” Dense-ish baseline: co-occurrence + SVD word vectors

Loading editor...
7
Section 5 โ€” Evaluation: Recall@k and MRR
1

Section 5 โ€” Evaluation: Recall@k and MRR

Loading editor...

Need help? Share feedback