faiss/tutorial/python/2-IVFFlat.py

35 lines
1.2 KiB
Python
Raw Normal View History

# Copyright (c) Facebook, Inc. and its affiliates.
2017-02-23 06:26:44 +08:00
#
# This source code is licensed under the MIT license found in the
2017-02-23 06:26:44 +08:00
# LICENSE file in the root directory of this source tree.
import numpy as np
2017-12-08 01:12:19 +08:00
2017-02-23 06:26:44 +08:00
d = 64 # dimension
nb = 100000 # database size
nq = 10000 # nb of queries
np.random.seed(1234) # make reproducible
xb = np.random.random((nb, d)).astype('float32')
xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.
import faiss
nlist = 100
k = 4
quantizer = faiss.IndexFlatL2(d) # the other index
index = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
2017-12-08 01:12:19 +08:00
# here we specify METRIC_L2, by default it performs inner-product search
2017-02-23 06:26:44 +08:00
assert not index.is_trained
index.train(xb)
assert index.is_trained
index.add(xb) # add may be a bit slower as well
D, I = index.search(xq, k) # actual search
2017-12-08 01:12:19 +08:00
print(I[-5:]) # neighbors of the 5 last queries
2017-02-23 06:26:44 +08:00
index.nprobe = 10 # default nprobe is 1, try a few more
D, I = index.search(xq, k)
2017-12-08 01:12:19 +08:00
print(I[-5:]) # neighbors of the 5 last queries