Lesson 9.2core3 h 30 min

Embeddings deep dive

How text becomes searchable geometry

Embeddings are the interface between language and geometry: a retriever can only find what the embedding model places near the query. Understanding how these models are trained (contrastive learning with in-batch negatives), why bi-encoders can be indexed and cross-encoders cannot, and when cosine, dot product and Euclidean distance agree lets you choose, evaluate and even fine-tune embedding models instead of trusting a leaderboard. The same loss trained SimCLR (5.8 Self-supervised and contrastive learning) and CLIP (8.10 Multimodal models), and the same vectors power agent memory (10.4 Memory and context engineering).

You'll be able to

  • Explain why raw BERT embeddings fail at similarity and how contrastive fine-tuning fixes them
  • Write the InfoNCE loss with in-batch negatives and explain the roles of batch size, hard negatives and temperature
  • Prove that cosine, dot product and Euclidean rankings coincide for unit vectors
  • Compare bi-encoders, cross-encoders and late interaction, and place each in a retrieval pipeline
  • Select an embedding model using MTEB as a shortlist and your own retrieval test set as the decider

Key ideas

  1. Raw BERT vectors are anisotropic and untrained for similarity; sentence embeddings come from contrastive fine-tuning on pairs that should be close.
  2. Bi-encoders embed query and document independently (indexable, fast); cross-encoders read them jointly (accurate, O(N) per query) and are used to rerank; ColBERT's late interaction sits between.
  3. InfoNCE with in-batch negatives: L = −(1/B) Σ_i log[exp(s_ii) / Σ_j exp(s_ij)], s_ij = q_i·d_j / τ — a softmax over 'which document belongs to this query'.
Show 4 more key ideas
  1. Quality levers in embedding training: large batches, mined hard negatives and a low temperature (≈0.01–0.05).
  2. For unit vectors ‖a−b‖² = 2 − 2cos θ, so cosine, dot product and Euclidean rankings coincide; use the metric the model card specifies.
  3. Memory is d × 4 bytes per vector in float32; Matryoshka truncation and int8/binary quantization cut it 4–32× with modest recall loss.
  4. MTEB shortlists models; a retrieval test set built from your own documents decides.

Suggested path through this lesson