|
| 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