Encoders, Embeddings and Retrieval

Dive into Deep Learning · §19

Encoders, Embeddings and Retrieval
word vectors · pretrained encoders · text embeddings · sparse, dense and approximate retrieval · retrieval-augmented generation

Finding a Passage and Labeling a Stream Are Jobs for Encoders

The problem

request a prompted decoder
how does the book’s load_array decide whether to shuffle? without the book, Qwen3.5-9B invents a shuffle argument; the answer is one paragraph among 5,324 passages
label 10,000 support messages an hour re-reads its 8 solved examples for every item: about 1,500 tokens per item, against 40 for a fine-tuned encoder

Both jobs need vectors that put related texts close together: one per passage to search, one per token for a small labeling head.

By the End of This Chapter You Can

Goals

  • say which statistic of a text a word vector factorizes, and check it;
  • fine-tune an encoder for sentences, pairs and tags, and say when it beats a prompt;
  • train a text embedder contrastively, and explain why raw pooled vectors retrieve poorly;
  • build BM25 and dense retrieval, fuse their rankings and rerank;
  • choose an approximate index by recall, latency and bytes;
  • measure a retrieval-augmented generator stage by stage, and locate its failures.

Running example: this book, 5,324 passages, and 220 questions with known sources, drafted and checked by models; the load_array question travels through the decks.

Idea: turn text into vectors that place related texts close together, then find the close ones fast.

Three Movements: Vectors, Finding, Using What Is Found

Chapter map

movement section question
vectors Word Embeddings which statistic does a word vector factorize?
Pretrained Encoders when does a fine-tuned encoder beat a prompt?
Text Embeddings from Contrastive Training why do pooled states retrieve poorly until trained?
finding Sparse, Dense and Approximate Retrieval what floor must vectors beat, and what does an index trade for speed?
using what is found Retrieval-Augmented Generation how is each stage of the system measured?
Failures and Fixes in RAG which stage lost each wrong answer, and does a fix chosen in advance help?
  • Uses: transformers; prompting, scoring and error bars.
  • Skips: pretraining an encoder, agents that search, images.