Elffuss BraidHub

kikocis/lancedb public

main · 5.1 KiBRenderedRawPermalink
1
2# Understanding HNSW index
3
4Approximate Nearest Neighbor (ANN) search is a method for finding data points near a given point in a dataset, though not always the exact nearest one. HNSW is one of the most accurate and fastest Approximate Nearest Neighbour search algorithms, It’s beneficial in high-dimensional spaces where finding the same nearest neighbor would be too slow and costly
5
6[Jump to usage](#usage)
7There are three main types of ANN search algorithms:
8
9* **Tree-based search algorithms**: Use a tree structure to organize and store data points.
10* * **Hash-based search algorithms**: Use a specialized geometric hash table to store and manage data points. These algorithms typically focus on theoretical guarantees, and don't usually perform as well as the other approaches in practice.
11* **Graph-based search algorithms**: Use a graph structure to store data points, which can be a bit complex.
12
13HNSW is a graph-based algorithm. All graph-based search algorithms rely on the idea of a k-nearest neighbor (or k-approximate nearest neighbor) graph, which we outline below.
14HNSW also combines this with the ideas behind a classic 1-dimensional search data structure: the skip list.
15
16## k-Nearest Neighbor Graphs and k-approximate Nearest neighbor Graphs
17The k-nearest neighbor graph actually predates its use for ANN search. Its construction is quite simple:
18
19* Each vector in the dataset is given an associated vertex.
20* Each vertex has outgoing edges to its k nearest neighbors. That is, the k closest other vertices by Euclidean distance between the two corresponding vectors. This can be thought of as a "friend list" for the vertex.
21* For some applications (including nearest-neighbor search), the incoming edges are also added.
22
23Eventually, it was realized that the following greedy search method over such a graph typically results in good approximate nearest neighbors:
24
25* Given a query vector, start at some fixed "entry point" vertex (e.g. the approximate center node).
26* Look at that vertex's neighbors. If any of them are closer to the query vector than the current vertex, then move to that vertex.
27* Repeat until a local optimum is found.
28
29The above algorithm also generalizes to e.g. top 10 approximate nearest neighbors.
30
31Computing a k-nearest neighbor graph is actually quite slow, taking quadratic time in the dataset size. It was quickly realized that near-identical performance can be achieved using a k-approximate nearest neighbor graph. That is, instead of obtaining the k-nearest neighbors for each vertex, an approximate nearest neighbor search data structure is used to build much faster.
32In fact, another data structure is not needed: This can be done "incrementally".
33That is, if you start with a k-ANN graph for n-1 vertices, you can extend it to a k-ANN graph for n vertices as well by using the graph to obtain the k-ANN for the new vertex.
34
35One downside of k-NN and k-ANN graphs alone is that one must typically build them with a large value of k to get decent results, resulting in a large index.
36
37
38## HNSW: Hierarchical Navigable Small Worlds
39
40HNSW builds on k-ANN in two main ways:
41
42* Instead of getting the k-approximate nearest neighbors for a large value of k, it sparsifies the k-ANN graph using a carefully chosen "edge pruning" heuristic, allowing for the number of edges per vertex to be limited to a relatively small constant.
43* The "entry point" vertex is chosen dynamically using a recursively constructed data structure on a subset of the data, similarly to a skip list.
44
45This recursive structure can be thought of as separating into layers:
46
47* At the bottom-most layer, an k-ANN graph on the whole dataset is present.
48* At the second layer, a k-ANN graph on a fraction of the dataset (e.g. 10%) is present.
49* At the Lth layer, a k-ANN graph is present. It is over a (constant) fraction (e.g. 10%) of the vectors/vertices present in the L-1th layer.
50
51Then the greedy search routine operates as follows:
52
53* At the top layer (using an arbitrary vertex as an entry point), use the greedy local search routine on the k-ANN graph to get an approximate nearest neighbor at that layer.
54* Using the approximate nearest neighbor found in the previous layer as an entry point, find an approximate nearest neighbor in the next layer with the same method.
55* Repeat until the bottom-most layer is reached. Then use the entry point to find multiple nearest neighbors (e.g. top 10).
56
57
58## Usage
59
60We can combine the above concepts to understand how to build and query an HNSW index in LanceDB.
61
62### Construct index
63
64```python
65import lancedb
66import numpy as np
67uri = "/tmp/lancedb"
68db = lancedb.connect(uri)
69
70# Create 10,000 sample vectors
71data = [
72 {"vector": row, "item": f"item {i}"}
73 for i, row in enumerate(np.random.random((10_000, 1536)).astype('float32'))
74]
75
76# Add the vectors to a table
77tbl = db.create_table("my_vectors", data=data)
78
79# Create and train the HNSW index for a 1536-dimensional vector
80# Make sure you have enough data in the table for an effective training step
81tbl.create_index(index_type=IVF_HNSW_SQ)
82
83```
84
85### Query the index
86
87```python
88# Search using a random 1536-dimensional embedding
89tbl.search(np.random.random((1536))) \
90 .limit(2) \
91 .to_pandas()
92```