Files
faiss/tutorial/python/8-PQFastScanRefine.py
Subhadeep Karan 451ca8429d Fix typos in documentation, tutorials, and C API (#4666)
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/4666

*   `for OpenMP support support` → `for OpenMP support`
*   `neccesary` → `necessary`
*   `journal= {arXiv:1702.08734},,` → `journal = {arXiv:1702.08734},`
*   `verinsion` → `version`
*   `consistency_check--config` → `consistency_check --config`
*   `etracted` → `extracted` (2 occurrences)
*   `sb` → `xb` (variable reference)
*   `statists` → `statistics`
*   `elemnts` → `elements`

Reviewed By: alibeklfc

Differential Revision: D86474214

fbshipit-source-id: f63272423b0a767f7576cd433004c87d85e2d740
2025-11-07 21:38:52 -08:00

39 lines
1.3 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import faiss
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') # 64-dim *nb queries
xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.
m = 8 # 8 specifies that the number of sub-vector is 8
k = 4 # number of dimension in extracted vector
n_bit = 4 # 4 specifies that each sub-vector is encoded as 4 bits
bbs = 32 # build block size ( bbs % 32 == 0 ) for PQ
index = faiss.IndexPQFastScan(d, m, n_bit, faiss.METRIC_L2)
index_refine = faiss.IndexRefineFlat(index)
# construct FastScan and run index refinement
assert not index_refine.is_trained
index_refine.train(xb) # Train vectors data index within mockup database
assert index_refine.is_trained
index_refine.add(xb)
params = faiss.IndexRefineSearchParameters(k_factor=3)
D, I = index_refine.search(xq[:5], 10, params=params)
print(I)
print(D)
index.nprobe = 10 # make comparable with experiment above
D, I = index.search(xq[:5], k) # search
print(I[-5:])