Skip to content

Commit 9c2dc7c

Browse files
committed
Adding documentation
1 parent 079c71e commit 9c2dc7c

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

TESTING_RECALL.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Testing recall
2+
3+
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
56+
57+
hnsw_index.init_index(max_elements=num_elements, ef_construction=200, M=16)
58+
bf_index.init_index(max_elements=num_elements)
59+
60+
# Controlling the recall for hnsw by setting ef:
61+
# higher ef leads to better accuracy, but slower search
62+
hnsw_index.set_ef(200)
63+
64+
# Set number of threads used during batch search/construction in hnsw
65+
# By default using all available cores
66+
hnsw_index.set_num_threads(1)
67+
68+
print("Adding batch of %d elements" % (len(data)))
69+
hnsw_index.add_items(data)
70+
bf_index.add_items(data)
71+
72+
print("Indices built")
73+
74+
# Generating query data
75+
query_data = np.float32(np.random.random((nun_queries, dim)))
76+
77+
# Query the elements and measure recall:
78+
labels_hnsw, distances_hnsw = hnsw_index.knn_query(query_data, k)
79+
labels_bf, distances_bf = bf_index.knn_query(query_data, k)
80+
81+
# Measure recall
82+
correct = 0
83+
for i in range(nun_queries):
84+
for label in labels_hnsw[i]:
85+
for correct_label in labels_bf[i]:
86+
if label == correct_label:
87+
correct += 1
88+
break
89+
90+
print("recall is :", float(correct)/(k*nun_queries))
91+
```

0 commit comments

Comments
 (0)