Skip to content

Commit 4489773

Browse files
Xiao Fufacebook-github-bot
Xiao Fu
authored andcommitted
Add tutorial for FastScan (#3465)
Summary: Pull Request resolved: #3465 This commit include python version of tutorial for FastScan. It includes all the parameters enabled within PQFastScan. Reviewed By: junjieqi Differential Revision: D57594044 fbshipit-source-id: cb12679b6fc241a654b9545c5bc7bd0517aa1813
1 parent c1528b5 commit 4489773

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tutorial/python/7-PQFastScan.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
import faiss
7+
import numpy as np
8+
9+
d = 64 # dimension
10+
nb = 100000 # database size
11+
nq = 10000 # nb of queries
12+
np.random.seed(1234) # make reproducible
13+
xb = np.random.random((nb, d)).astype('float32') # 64-dim *nb queries
14+
xb[:, 0] += np.arange(nb) / 1000.
15+
xq = np.random.random((nq, d)).astype('float32')
16+
xq[:, 0] += np.arange(nq) / 1000.
17+
18+
m = 8 # 8 specifies that the number of sub-vector is 8
19+
k = 4 # number of dimension in etracted vector
20+
n_bit = 4 # 4 specifies that each sub-vector is encoded as 4 bits
21+
bbs = 32 # build block size ( bbs % 32 == 0 ) for PQ
22+
index = faiss.IndexPQFastScan(d, m, n_bit, faiss.METRIC_L2, bbs)
23+
# construct FastScan Index
24+
25+
assert not index.is_trained
26+
index.train(xb) # Train vectors data index within mockup database
27+
assert index.is_trained
28+
29+
index.add(xb)
30+
D, I = index.search(xb[:5], k) # sanity check
31+
print(I)
32+
print(D)
33+
index.nprobe = 10 # make comparable with experiment above
34+
D, I = index.search(xq, k) # search
35+
print(I[-5:]) # neighbors of the 5 last queries

0 commit comments

Comments
 (0)