Truncate Matryoshka Embeddings

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 21. Everything before it counts as cleared.

Matryoshka embedding models (OpenAI's text-embedding-3, Nomic) are trained so the first dimensions carry the most information. You can truncate a 1536-dim vector to 256 dims, renormalise, and keep most of the retrieval quality at a sixth of the storage.

Implement:

  • truncate(E, d): keep the first d columns of each row and renormalise rows to unit length
  • recall_at_k(Q, D, relevant, k): relevant[i] is the index of query i's correct document. Return the fraction of queries whose correct document is among the top-k by dot product, with ties going to the lower index.
  • storage_mb(n, d, bytes_per_value=4): the storage for n vectors of d values, in megabytes (10⁶ bytes)
  • smallest_dim(Q, D, relevant, k, dims, min_ratio): the smallest d in dims whose recall@k is at least min_ratio times the full-dimension recall
#embeddings#retrieval#efficiency2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Truncate Matryoshka Embeddings · TechGradify