mirror of
https://github.com/facebookresearch/faiss.git
synced 2025-06-03 21:54:02 +08:00
Changelog: - changed license: BSD+Patents -> MIT - propagates exceptions raised in sub-indexes of IndexShards and IndexReplicas - support for searching several inverted lists in parallel (parallel_mode != 0) - better support for PQ codes where nbit != 8 or 16 - IVFSpectralHash implementation: spectral hash codes inside an IVF - 6-bit per component scalar quantizer (4 and 8 bit were already supported) - combinations of inverted lists: HStackInvertedLists and VStackInvertedLists - configurable number of threads for OnDiskInvertedLists prefetching (including 0=no prefetch) - more test and demo code compatible with Python 3 (print with parentheses) - refactored benchmark code: data loading is now in a single file
33 lines
1018 B
Python
33 lines
1018 B
Python
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
import numpy as np
|
|
|
|
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
|
|
m = 8
|
|
k = 4
|
|
quantizer = faiss.IndexFlatL2(d) # this remains the same
|
|
index = faiss.IndexIVFPQ(quantizer, d, nlist, m, 8)
|
|
# 8 specifies that each sub-vector is encoded as 8 bits
|
|
index.train(xb)
|
|
index.add(xb)
|
|
D, I = index.search(xb[:5], k) # sanity check
|
|
print(I)
|
|
print(D)
|
|
index.nprobe = 10 # make comparable with experiment above
|
|
D, I = index.search(xq, k) # search
|
|
print(I[-5:])
|