Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.5 — Making Vectors Smaller
RAG and Vector Search · Example 2 of 2

Compress hard, then re-score a shortlist

1,000 random 16-number vectors are a hard case for compression. Treat the recall values as a demonstration of the pattern, not as numbers to expect on your own embeddings.

Quantisation stores each number of a vector with fewer bits, which shrinks memory and speeds up comparisons while the ranking barely changes. Re-scoring a small shortlist with the full vectors wins back most of the accuracy that heavy compression loses.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write quantise(vec, levels) and restore(codes, levels) for numbers between -1 and 1 (round with math.floor(x + 0.5)). For the vector [-1.0, 0.0, 0.5, 1.0] print the 8-bit codes (levels 255), the restored values with 3 decimals, and the 1-bit codes (levels 1).

02

A collection has 2,000,000 vectors of 768 numbers. Print the memory in megabytes (1 MB = 1,000,000 bytes) as 32-bit floats, 8-bit codes and 1-bit codes.

Explain it without notes

01

What is 8-bit scalar quantisation, how much memory does it save, and what is the largest error on a number between -1 and 1?

02

Why can a ranking stay nearly unchanged when every number in the vectors is slightly wrong?

03

What is re-scoring a shortlist, and why does it recover accuracy lost to compression?

04

Give the memory for 5 million vectors of 768 numbers as float32, 8-bit and binary.

Compress hard, then re-score a shortlist
Sign in to run this example in your browser.

Expected output

levels  bits   recall@3 from codes only   recall@3 after re-scoring a shortlist of 10
   255     8   1.000                      1.000
    15     4   0.883                      1.000
     3     2   0.617                      0.917
     1     1   0.317                      0.583