Debian Codegraph @

tutorial/python/2-IVFFlat.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 k = 4
21 quantizer = faiss.IndexFlatL2(d) # the other index
22 index = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
23 # here we specify METRIC_L2, by default it performs inner-product search
24
25 assert not index.is_trained
26 index.train(xb)
27 assert index.is_trained
28
29 index.add(xb) # add may be a bit slower as well
30 D, I = index.search(xq, k) # actual search
31 print(I[-5:]) # neighbors of the 5 last queries
32 index.nprobe = 10 # default nprobe is 1, try a few more
33 D, I = index.search(xq, k)
34 print(I[-5:]) # neighbors of the 5 last queries