Lesson 23 Embeddings Deep Dive
Theme:

Theory

Reading time: ~20 minutes (~1500 words)  ·  Prerequisites: Lessons 20–22 (tokenization, transformers, pre-training)

1. Why Embeddings Matter

Computers work with numbers, not words. An embedding is a learned mapping from discrete objects — words, sentences, images, anything — into a continuous vector space where geometric proximity encodes semantic similarity. If two pieces of text mean roughly the same thing, their embedding vectors point in roughly the same direction.

Embeddings are the bridge between human language and machine computation. They power semantic search, recommendation systems, clustering, classification, anomaly detection, and the retrieval step of RAG (Retrieval-Augmented Generation) pipelines. Understanding how they work, what trade-offs they involve, and how to evaluate them is essential for building production AI systems.

2. Word2Vec: The Foundation

The modern embedding era began with Word2Vec (Mikolov et al., 2013). The core insight was simple: words that appear in similar contexts should have similar vector representations. Word2Vec offered two training objectives:

Both architectures are shallow neural networks — just an input layer, one hidden layer, and an output layer. The hidden layer weights become the word vectors. Despite this simplicity, Word2Vec vectors famously captured relational structure: vec("king") - vec("man") + vec("woman") lands near vec("queen").

Limitations of Static Embeddings

Word2Vec assigns a single vector to each word regardless of context. The word "bank" gets the same vector whether it means a financial institution or a river bank. This is a fundamental limitation that later contextual models (ELMo, BERT, and the transformer-based embedding models we use today) resolved by producing different vectors for the same word depending on its surrounding context.

3. Sentence and Document Embeddings

Most practical tasks require embedding entire sentences, paragraphs, or documents — not individual words. Several approaches emerged:

Choosing Between Local and API Models

Use local models (sentence-transformers) when you need low latency, offline operation, full data privacy, or want to fine-tune. Use API models when you want state-of-the-art quality without managing infrastructure, or when your volume is low enough that per-call pricing is cheaper than GPU hosting.

4. Cosine Similarity and Distance Metrics

With vectors in hand, you need a way to measure how close two vectors are. The three main options are:

Practical guidance: Use cosine similarity unless you have a specific reason not to. It is robust, intuitive, and universally supported by vector databases.

5. Embedding Dimensions and Quality

Embedding models produce vectors of fixed dimensionality — typically between 384 and 3072 dimensions. Higher dimensionality allows the model to represent finer distinctions, but it comes with costs: more storage per vector, slower similarity computation, and higher memory usage for vector indices.

A recent innovation is Matryoshka Representation Learning (MRL), used in models like OpenAI's text-embedding-3 series. Matryoshka embeddings are trained so that the first d dimensions of a larger vector form a useful embedding on their own. You can truncate a 3072-dimensional vector to 256 dimensions and still get reasonable quality — useful for building tiered retrieval systems where a fast, low-dimensional first pass narrows candidates before a full-dimensional reranking step.

6. Vector Databases

When you have thousands or millions of embeddings, you need specialised storage that supports fast approximate nearest neighbour (ANN) search. Exact brute-force comparison (check every vector) is O(n) per query and becomes impractical past a few hundred thousand vectors.

Vector databases solve this with index structures like HNSW (Hierarchical Navigable Small World), IVF (Inverted File Index), and PQ (Product Quantisation). These trade a small amount of recall for dramatically faster search — typically sub-millisecond for millions of vectors.

DatabaseTypeBest for
ChromaDBOpen-source, embeddedPrototyping, small–medium datasets, Python-native workflows
PineconeManaged cloudProduction at scale, zero ops overhead
WeaviateOpen-source + cloudHybrid vector + keyword search, multi-modal
pgvectorPostgreSQL extensionAdding vector search to existing Postgres infrastructure
QdrantOpen-source + cloudHigh performance, filtering, Rust-based
FAISSLibrary (Meta)Research, custom index tuning, GPU-accelerated search

7. Retrieval with Embeddings

The standard retrieval pipeline with embeddings works as follows:

  1. Indexing (offline): Split your document corpus into chunks. Embed each chunk using your chosen model. Store the vectors (plus metadata) in a vector database.
  2. Querying (online): Embed the user's query with the same model. Search the vector database for the top-k nearest vectors. Return the associated text chunks.
  3. Post-processing: Optionally rerank results with a cross-encoder or LLM for higher precision. Feed the top results into an LLM as context (RAG).

Critical detail: the query and the documents must be embedded with the same model. Vectors from different models live in different vector spaces and cannot be compared meaningfully.

Common Pitfall: Chunking Strategy

How you split documents into chunks has a large impact on retrieval quality. Chunks that are too small lose context; chunks that are too large dilute the embedding signal. A typical starting point is 256–512 tokens per chunk with 10–20% overlap. Experiment with your specific data to find the optimal size.

8. MTEB: Evaluating Embedding Quality

The Massive Text Embedding Benchmark (MTEB) is the standard evaluation suite for embedding models. It tests models across 8 task categories — retrieval, semantic textual similarity (STS), classification, clustering, reranking, pair classification, summarisation, and bitext mining — using 56+ datasets.

MTEB scores reveal that no single model dominates all tasks. A model that excels at retrieval may be mediocre at clustering. Always check MTEB scores for the specific task type you care about, not just the overall average. The leaderboard is available at huggingface.co/spaces/mteb/leaderboard.

9. Fine-Tuning Embeddings

When off-the-shelf models do not capture domain-specific notions of similarity, fine-tuning can help. The sentence-transformers library supports several training approaches:

Fine-tuning typically requires hundreds to thousands of labelled examples. Even a small amount of domain-specific training data can yield significant improvements over general-purpose models for specialised vocabularies (legal, medical, scientific) or custom similarity definitions.

Further Reading

Next lesson: Lesson 24 — LoRA and QLoRA Fine-Tuning — adapting pre-trained models to your specific tasks with parameter-efficient methods.