You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Selecting HNSW parameters for a specific use case highly impacts the search quality. One way to test the quality of the constructed index is to compare the HNSW search results to the actual results (i.e., the actual `k` nearest neighbors).
4
+
For that cause, the API enables creating a simple "brute-force" index in which vectors are stored as is, and searching for the `k` nearest neighbors to a query vector requires going over the entire index.
5
+
Comparing between HNSW and brute-force results may help with finding the desired HNSW parameters for achieving a satisfying recall, based on the index size and data dimension.
6
+
7
+
### Brute force index API
8
+
`hnswlib.BFIndex(space, dim)` creates a non-initialized index in space `space` with integer dimension `dim`.
9
+
10
+
`hnswlib.BFIndex` methods:
11
+
12
+
`init_index(max_elements)` initializes the index with no elements.
13
+
14
+
max_elements defines the maximum number of elements that can be stored in the structure.
15
+
16
+
`add_items(data, ids)` inserts the data (numpy array of vectors, shape:`N*dim`) into the structure.
17
+
`ids` are optional N-size numpy array of integer labels for all elements in data.
18
+
19
+
`delete_vector(label)` delete the element associated with the given `label` so it will be omitted from search results.
20
+
21
+
`knn_query(data, k = 1)` make a batch query for `k `closest elements for each element of the
22
+
`data` (shape:`N*dim`). Returns a numpy array of (shape:`N*k`).
23
+
24
+
`load_index(path_to_index, max_elements = 0)` loads the index from persistence to the uninitialized index.
25
+
26
+
`save_index(path_to_index)` saves the index from persistence.
27
+
28
+
### measuring recall example
29
+
30
+
```
31
+
import hnswlib
32
+
import numpy as np
33
+
34
+
dim = 32
35
+
num_elements = 100000
36
+
k = 10
37
+
nun_queries = 10
38
+
39
+
# Generating sample data
40
+
data = np.float32(np.random.random((num_elements, dim)))
41
+
42
+
# Declaring index
43
+
hnsw_index = hnswlib.Index(space='l2', dim=dim) # possible options are l2, cosine or ip
44
+
bf_index = hnswlib.BFIndex(space='l2', dim=dim)
45
+
46
+
# Initing both hnsw and brute force indices
47
+
# max_elements - the maximum number of elements (capacity). Will throw an exception if exceeded
48
+
# during insertion of an element.
49
+
# The capacity can be increased by saving/loading the index, see below.
50
+
#
51
+
# hnsw construction params:
52
+
# ef_construction - controls index search speed/build speed tradeoff
53
+
#
54
+
# M - is tightly connected with internal dimensionality of the data. Strongly affects the memory consumption (~M)
55
+
# Higher M leads to higher accuracy/run_time at fixed ef/efConstruction
0 commit comments