Debian Codegraph @

tutorial/python/3-IVFPQ.py

raw
1 # Copyright (c) Meta Platforms, Inc. and 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 numpy as np
7
8 d = 64 # dimension
9 nb = 100000 # database size
10 nq = 10000 # nb of queries
11 np.random.seed(1234) # make reproducible
12 xb = np.random.random((nb, d)).astype('float32')
13 xb[:, 0] += np.arange(nb) / 1000.
14 xq = np.random.random((nq, d)).astype('float32')
15 xq[:, 0] += np.arange(nq) / 1000.
16
17 import faiss
18
19 nlist = 100
20 m = 8
21 k = 4
22 quantizer = faiss.IndexFlatL2(d) # this remains the same
23 index = faiss.IndexIVFPQ(quantizer, d, nlist, m, 8)
24 # 8 specifies that each sub-vector is encoded as 8 bits
25 index.train(xb)
26 index.add(xb)
27 D, I = index.search(xb[:5], k) # sanity check
28 print(I)
29 print(D)
30 index.nprobe = 10 # make comparable with experiment above
31 D, I = index.search(xq, k) # search
32 print(I[-5:])