Product Quantization: Vector Compression for ANN Search › Worked Example
Language AI HandbookAll chapters Product Quantization: Vector Compression for ANN Search › Worked ExampleOne section · Sections in this chapter5 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let us trace through PQ with a tiny example to make the mechanics concrete before looking at code. Suppose your embeddings are 4-dimensional (just to keep numbers small) and you use subspaces, centroids each.
Working through a small example is valuable for understanding the mechanics and for building intuition about the error properties of PQ. In this example we will see exactly where approximation error enters and why it tends to be small for true nearest neighbors while still being large enough to occasionally produce ranking errors for borderline cases.
Training data (6 vectors):
| Vector | Dim 1-2 (subspace 1) | Dim 3-4 (subspace 2) |
|---|---|---|
| a | [0.1, 0.2] | [0.8, 0.9] |
| b | [0.0, 0.3] | [0.7, 0.8] |
| c | [0.9, 0.8] | [0.1, 0.2] |
| d | [0.8, 0.9] | [0.0, 0.1] |
| e | [0.5, 0.5] | [0.5, 0.4] |
| f | [0.4, 0.6] | [0.6, 0.5] |
Notice that the training data has a clear geometric structure: vectors a and b have small values in subspace 1 and large values in subspace 2, while c and d are the reverse. Vectors e and f occupy a middle region. K-means in each subspace will naturally discover these clusters, and the resulting centroids will be positioned to cover the data distribution efficiently. Each centroid ends up representing a cluster of points, and the centroid's coordinates are the mean of all the points in its cluster.
After k-means in subspace 1, suppose we get centroids , , .
After k-means in subspace 2, suppose we get centroids , , .
Step 1: Encode database vectors. We find the nearest centroid in each subspace for each vector:
Encoding vector a = [0.1, 0.2, 0.8, 0.9]:
- Subspace 1: is closest to , so .
- Subspace 2: is closest to , so .
- Code for a: .
The code is just two integers, but it carries meaningful geometric information: it tells us that this vector lives in the region of subspace 1 near centroid 0 and the region of subspace 2 near centroid 0. Any query that is also near those centroids will receive a low approximate distance estimate, correctly reflecting the true proximity.
Encoding vector c = [0.9, 0.8, 0.1, 0.2]:
- Subspace 1: is closest to , so .
- Subspace 2: is closest to , so .
- Code for c: .
Encoding vector e = [0.5, 0.5, 0.5, 0.4]:
- Subspace 1: is closest to , so .
- Subspace 2: is closest to , so .
- Code for e: .
Step 2: Build lookup tables for query. Suppose :
Build lookup tables by computing the distance from each query subvector to every centroid:
Step 3: Compute approximate distances. Now use the lookup tables to estimate the distance from to each encoded database vector:
- Distance to a (code ):
- Distance to c (code ):
- Distance to e (code ):
The ranking is: a is closest, then e, then c. This matches the true ranking based on the raw vectors (which you can verify by computing exact distances from to each vector). Vector a, which shares a similar geometric structure to the query in both subspaces, receives the lowest approximate distance. Vector c, which is far in both subspaces, receives the highest.
Step 4: Verify the approximation error. The true distance from to is:
The approximate distance was 0.0151, so the absolute error is 0.013. This is larger than the true distance itself, but the relative ranking is correct. The approximation overestimates the distance to because both centroids are slightly displaced from 's actual position, and both displacements add positive contributions to the estimated distance. However, because is also close to 's centroids, the lookup table values and are still much smaller than the values for the other centroids, so the ranking is preserved. This is the fundamental property that makes PQ useful: even though the absolute distances are approximate, the relative ordering among the closest candidates is preserved well enough for top- retrieval to succeed with high probability.