Deep Learning
Embeddings and similarity search
Learn Embeddings and similarity search: 34 practice questions and answers with an interactive quiz — from the offline AI Learning app.
Questions & Answers
Tap a question to reveal its answer. Written for self-study and interview preparation.
What is the key difference between sparse and dense embeddings, and when would you combine them?
Sparse embeddings (e.g., TF-IDF, BM25, SPLADE) are high-dimensional vectors where most entries are zero and dimensions map to vocabulary terms, giving strong exact lexical matching and interpretability. Dense embeddings (e.g., from transformer encoders) are low-dimensional continuous vectors capturing semantic similarity but can miss rare exact terms. Hybrid retrieval combines both, typically fusing scores with weighted sum or Reciprocal Rank Fusion, to capture both keyword precision and semantic recall.
Why is cosine similarity often preferred over Euclidean distance for comparing text embeddings?
Cosine similarity measures the angle between vectors, ignoring magnitude, which makes it robust to differences in vector norm that often correlate with token count or frequency rather than meaning. Many embedding models are trained with cosine/dot-product objectives, so cosine aligns with the learned geometry. For L2-normalized vectors, cosine similarity and Euclidean distance produce equivalent rankings, since one is a monotonic function of the other.
Explain how HNSW (Hierarchical Navigable Small World) achieves fast approximate nearest neighbor search.
HNSW builds a multi-layer proximity graph where upper layers are sparse long-range links and lower layers are dense local links, like a skip list over a navigable small-world graph. Search starts at the top layer's entry point, greedily moves to the closest neighbor, then descends layers, refining the candidate set with a priority queue of size efSearch. This yields logarithmic-scale search complexity with high recall, trading memory (storing graph edges) for speed.
What is Product Quantization (PQ) and how does it reduce memory for vector search?
Product Quantization splits each vector into m subvectors and learns a separate k-means codebook (typically 256 centroids) per subspace, encoding each subvector as a 1-byte centroid index. A D-dimensional float32 vector thus compresses to m bytes, often a 10-50x reduction. Distances are approximated via precomputed lookup tables (asymmetric distance computation), enabling fast scoring at the cost of quantization error.
What problem does the IVF (Inverted File) index solve, and what is the role of nprobe?
IVF accelerates search by partitioning the vector space with k-means into nlist Voronoi cells and assigning each vector to its nearest centroid, so queries only scan a few cells instead of the whole dataset. At query time, nprobe controls how many of the closest cells are searched: higher nprobe increases recall but reduces speed. It is commonly combined with PQ (IVFPQ) to also compress the stored residuals.
Why must you use the same embedding model (and version) for indexing documents and encoding queries?
Embedding spaces are model-specific; different models or even versions place the same concept at different coordinates, so vectors from two models are not comparable and similarity scores become meaningless. Query and document vectors must live in the same learned space for nearest-neighbor distances to reflect semantic relatedness. Changing the model requires re-embedding the entire corpus, not just new queries.
What is the difference between bi-encoders and cross-encoders in semantic search?
A bi-encoder encodes the query and each document independently into fixed vectors, enabling precomputation and fast ANN retrieval but losing fine-grained query-document interaction. A cross-encoder feeds the query and document together through the model and outputs a relevance score, capturing rich interactions for much higher accuracy but at high cost since nothing can be precomputed. The standard pattern is bi-encoder retrieval followed by cross-encoder reranking of the top-k.
What is the curse of dimensionality and how does it affect nearest-neighbor search?
As dimensionality grows, distances between points concentrate so the ratio between nearest and farthest neighbor approaches 1, making it hard to distinguish true neighbors and degrading exact and approximate search. Volume grows exponentially, so data becomes sparse and tree-based partitioning indexes lose their pruning power. Learned embeddings mitigate this by mapping data onto a lower-dimensional semantic manifold where meaningful structure is preserved.
Explain Matryoshka Representation Learning (MRL) and its practical benefit for retrieval.
Matryoshka embeddings are trained so that meaningful information is front-loaded into the leading dimensions, meaning a prefix of the full vector (e.g., the first 256 of 1536 dims) is itself a usable lower-quality embedding. This lets you truncate vectors to trade accuracy for lower storage and faster search without retraining or re-embedding. A common pattern is a coarse search on truncated vectors followed by reranking with the full-dimensional vectors.
In FAISS, what is the difference between IndexFlatIP and IndexFlatL2, and how do you do cosine similarity?
IndexFlatL2 does exact brute-force search using squared Euclidean distance, while IndexFlatIP uses inner (dot) product. For cosine similarity you L2-normalize all vectors and then use IndexFlatIP, because the dot product of unit vectors equals their cosine. Both 'Flat' indexes are exact and have no training step, serving as accuracy baselines for approximate indexes.
import faiss, numpy as np
faiss.normalize_L2(xb) # in-place L2 normalize
index = faiss.IndexFlatIP(d)
index.add(xb)
faiss.normalize_L2(xq)
scores, ids = index.search(xq, k) # scores ~ cosineWhat are the trade-offs of recall vs. latency in approximate nearest neighbor (ANN) systems?
ANN indexes deliberately sacrifice exactness for speed, so tuning parameters (efSearch in HNSW, nprobe in IVF) lets you slide along a recall-latency curve: more exploration yields higher recall but higher latency and CPU. The right operating point depends on the application's tolerance for missing relevant results versus its latency SLA. You measure this with recall@k against a brute-force ground truth and choose parameters that meet the SLA at acceptable recall.
What is the role of a reranker in a RAG retrieval pipeline, and why not just retrieve more candidates?
A reranker (usually a cross-encoder or LLM-based scorer) re-scores the top-k ANN candidates with deeper query-document interaction, sharply improving precision at the top ranks that the LLM actually reads. Simply retrieving more candidates increases recall but floods the context with noise and exceeds token budgets, hurting answer quality. The pattern is high-recall cheap retrieval, then high-precision expensive reranking on a small candidate set.
How does chunking strategy affect embedding-based retrieval quality?
Chunk size controls a granularity trade-off: chunks that are too large dilute the embedding with multiple topics and hurt precision, while chunks that are too small lose context needed for relevance and answers. Overlapping windows reduce information loss at boundaries, and semantic or structure-aware chunking (by sections/sentences) keeps coherent units together. Optimal chunking is corpus- and model-dependent and should be tuned against retrieval metrics.
What does it mean for an embedding space to be anisotropic, and why is it a problem?
Anisotropy means the embeddings occupy a narrow cone in the vector space rather than spreading uniformly, so even unrelated items have high cosine similarity, compressing the usable similarity range. This 'representation degeneration' is common in raw transformer outputs and reduces discriminative power. Mitigations include whitening, mean-centering, contrastive fine-tuning (e.g., SimCSE), or normalization that flattens the dominant directions.
Compare dot product, cosine, and Euclidean distance as similarity metrics for embeddings.
Dot product is sensitive to both angle and magnitude, so it can be dominated by longer vectors and is used when magnitude carries signal (e.g., some MIPS setups). Cosine measures only direction, normalizing out magnitude, which is typical for semantic similarity. Euclidean (L2) measures absolute distance; on L2-normalized vectors it is rank-equivalent to cosine (and to negative dot product), so the choice mostly matters when vectors are not normalized.
What is Maximum Inner Product Search (MIPS) and why can't it directly reuse metric-tree structures?
MIPS finds the vector maximizing the inner product with a query, which is the natural objective for many recommendation and retrieval models. Inner product is not a metric (it violates the triangle inequality and a vector is not closest to itself), so metric-space structures like KD-trees and ball-trees that rely on those properties don't apply directly. Common solutions transform MIPS into nearest-neighbor search via augmentation/normalization or use graph indexes like HNSW that work with arbitrary inner-product scores.
How would you evaluate the quality of an embedding model for a retrieval task?
Use ranking metrics on a labeled query-document set: recall@k and MRR/MAP for whether relevant items are retrieved and ranked high, and nDCG when relevance is graded. Standardized benchmarks like MTEB (and BEIR for retrieval) give cross-domain comparisons, but in-domain evaluation on your own data matters most. Also consider operational factors: dimensionality, latency, cost, and multilingual or domain coverage.
What is contrastive learning and why is it central to training modern embedding models?
Contrastive learning trains embeddings to pull semantically related (positive) pairs together and push unrelated (negative) pairs apart in vector space, typically via InfoNCE loss with in-batch negatives. This directly shapes the geometry so that similarity (cosine/dot) reflects semantic relatedness, which is exactly what retrieval needs. Hard negatives and large batch sizes substantially improve the discriminative quality of the learned space.
Why are hard negatives important when fine-tuning embedding models, and how are they typically mined?
Easy negatives are trivially separable and provide little gradient signal, whereas hard negatives are semantically close-but-incorrect examples that force the model to learn fine distinctions and sharpen decision boundaries. They are commonly mined by retrieving top-ranked non-relevant documents with a baseline retriever (BM25 or an earlier embedding model) for each query. Care is needed to filter false negatives, since unlabeled relevant items mined as negatives can poison training.
What is binary quantization of embeddings and what accuracy-recovery technique is commonly paired with it?
Binary quantization maps each dimension to a single bit (e.g., sign of the value), shrinking storage by ~32x versus float32 and letting similarity be computed with extremely fast Hamming distance. It loses precision, so the standard pattern is a fast binary first-stage search to fetch a large candidate set, then rescoring those candidates with the original float (or int8) vectors. This delivers most of the speed/memory win while recovering recall via rescoring.
How do late-interaction models like ColBERT differ from standard single-vector dense retrieval?
Instead of compressing a document into one vector, ColBERT stores a contextual embedding per token and scores a query-document pair via MaxSim: for each query token it takes the max similarity over all document tokens and sums these. This 'late interaction' retains fine-grained token-level matching, giving accuracy near cross-encoders while remaining precomputable and indexable. The cost is much larger storage and a specialized index, since you keep many vectors per document.
What is the 'lost in the middle' problem and how does retrieval ordering relate to it?
Studies show LLMs attend most strongly to information at the beginning and end of their context, with relevance dropping for facts buried in the middle of a long context. So even with good retrieval, placing the most relevant chunks in the middle of a large prompt can hurt answers. Practical mitigations include reranking to surface fewer, higher-quality chunks and ordering the best evidence at the start or end of the context.
Why might you apply dimensionality reduction (e.g., PCA) to embeddings before indexing, and what is the risk?
Reducing dimensions lowers storage and speeds up distance computations and indexing, and can remove noisy directions that dominate raw transformer outputs. The risk is discarding directions that carry discriminative signal, degrading recall, especially if reduction is fit on a non-representative sample. Matryoshka embeddings are a built-in alternative since they support truncation without a separate fitted projection.
What is the difference between symmetric and asymmetric semantic search, and how does it affect model choice?
In symmetric search, queries and documents are similar in form and length (e.g., duplicate-question detection), so a single encoder treating both the same works well. In asymmetric search, short queries are matched against long passages (typical RAG), so models are often trained with query/passage prefixes or dual objectives to bridge the length/style gap. Choosing or prompting the embedding model to match your task's symmetry materially affects retrieval quality.
How do instruction-tuned / prefix-based embedding models (e.g., E5, Instructor) improve retrieval?
These models prepend task-specific instructions or role prefixes like 'query:' and 'passage:' (or natural-language task descriptions) so the same backbone produces embeddings tailored to the retrieval intent. This helps asymmetric search and lets one model serve multiple tasks by changing the instruction rather than retraining. Using the exact prefixes the model was trained with is critical, since omitting or mismatching them degrades quality.
# E5-style usage
q = model.encode('query: how to reset password')
d = model.encode('passage: To reset your password, go to settings...')What is Reciprocal Rank Fusion (RRF) and why is it popular for hybrid search?
RRF combines multiple ranked lists by summing 1/(k + rank) for each item across lists (k often ~60), rewarding items ranked high in any source. It needs only ranks, not calibrated scores, so it cleanly merges incomparable scorers like BM25 and dense cosine without score normalization. This simplicity and robustness make it a default fusion method for hybrid lexical+semantic retrieval.
def rrf(rank_lists, k=60):
scores = {}
for lst in rank_lists:
for rank, doc in enumerate(lst):
scores[doc] = scores.get(doc, 0) + 1/(k + rank)
return sorted(scores, key=scores.get, reverse=True)What are the main challenges of keeping a vector index fresh under frequent inserts, updates, and deletes?
Graph indexes like HNSW support inserts but handle deletes poorly, often using soft-delete tombstones that bloat the graph and require periodic rebuilds to reclaim quality and space. Updates usually mean delete-plus-reinsert, and bulk re-embedding is needed when the model changes. Production systems address this with segment-based architectures (immutable segments + background merges/compaction) and versioned reindexing to avoid downtime.
How can metadata filtering interact badly with ANN search (the 'filtered search' problem)?
Pre-filtering can leave too few candidates in the graph region, breaking ANN navigability, while post-filtering may discard most retrieved neighbors and miss valid matches, hurting recall. Highly selective filters are especially problematic because the relevant subset is sparse in the index. Modern systems use filter-aware/hybrid approaches (e.g., HNSW with inline filtering or partitioned indexes) to keep recall high under constraints.
Why are embeddings typically L2-normalized before being stored in a vector database?
Normalizing to unit length makes the dot product equal the cosine similarity, so inner-product indexes directly yield cosine rankings and similarity scores fall in a consistent [-1, 1] range. It removes magnitude effects (often tied to length/frequency) that would otherwise distort comparisons, and it stabilizes thresholds across the corpus. Many ANN libraries assume normalized vectors when configured for cosine.
What is recall@k as a metric for ANN systems, and how do you obtain ground truth for it?
Recall@k for an ANN index is the fraction of the true top-k nearest neighbors that the approximate search actually returns in its top-k, measuring fidelity to exact search rather than human relevance. Ground truth is computed with exact brute-force (flat) search over the same vectors and metric. It is the key metric for tuning ANN parameters (efSearch, nprobe) against latency.
What is query expansion / HyDE, and how does it improve embedding-based retrieval?
HyDE (Hypothetical Document Embeddings) prompts an LLM to generate a hypothetical answer/document for the query, then embeds that richer text instead of (or alongside) the short query to retrieve real documents. This bridges the lexical/semantic gap between terse queries and verbose passages, improving asymmetric retrieval. The trade-off is added LLM latency/cost and the risk of hallucinated content steering retrieval off-topic.
How do multimodal embeddings (e.g., CLIP) enable cross-modal similarity search?
Models like CLIP are trained contrastively to map images and text into a shared embedding space so that matching image-text pairs have high similarity. This lets you search images with text queries (and vice versa) using ordinary cosine/dot-product nearest-neighbor search in the joint space. The key requirement is that all modalities are encoded into the same aligned space, typically by the same trained model family.
What practical issues arise from very high embedding dimensionality (e.g., 3072+ dims) in production retrieval?
High dimensions increase memory footprint and bandwidth (a 3072-dim float32 vector is ~12 KB), raise per-comparison compute cost, and inflate index build time and RAM. They can also amplify distance concentration with limited extra signal beyond a point. Teams mitigate with Matryoshka truncation, quantization (int8/binary/PQ), or PCA, balancing recall against cost.
When should you fine-tune an embedding model versus using an off-the-shelf one for your domain?
Off-the-shelf general models are best when your domain language overlaps common web text and you lack labeled pairs; they avoid training cost and overfitting. Fine-tuning (typically contrastive with in-domain query-document pairs and hard negatives) pays off for specialized jargon, proprietary terminology, or when benchmark recall on your own data is poor. The decision should be driven by measured in-domain retrieval metrics, not assumptions, and weigh the cost of labeling and maintenance.
Related topics in Deep Learning
Put it into practice with AI Learning
This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.
- Free
- No account
- Works offline