Skip to content
BytePatterns

Inverted-File Search Recall

HardAI & ML#vector-search#inverted-file-index#recall-at-k~35m

Problem

An inverted-file index speeds up vector search by clustering. Given integer vectors and fixed centroids, put each vector in the list of its nearest centroid by squared Euclidean distance, the lowest centroid index on a tie. A search for a query ranks the centroids by distance to it, scans only the lists of the nprobe nearest, and returns the k closest vectors found there, ties broken by vector index. Measure the index against exact search: for every nprobe from 1 to the number of centroids, return the recall at k, the fraction of the exact top-k vectors that the index also returned, averaged over all queries and rounded to 2 decimals.

Examples

Input:  vectors = [(0, 0), (1, 1), (2, 0), (9, 9), (8, 10), (10, 8), (5, 4), (4, 6)],
        centroids = [(1, 0), (9, 9), (5, 5)], queries = [(3, 2), (7, 7)], k = 3
Output: [0.83, 1.0, 1.0]
Why:    (3, 2) probes only the (1, 0) cluster and misses (5, 4), which sits in the next one
Input:  the same vectors and centroids, queries = [(6, 1)], k = 2
Output: [0.5, 1.0, 1.0]
Why:    the query's nearest centroid holds one of its two true neighbours; probing a second list finds the other
Input:  vectors = [(0, 0), (1, 0)], centroids = [(0, 0)], queries = [(5, 5)], k = 2
Output: [1.0]
Why:    edge case, with a single list the index scans everything and matches exact search

Hints

0 / 3

Stuck on the idea rather than the code? Approximate Neighbours covers it.