faiss安装

https://blog.csdn.net/dake1994/article/details/84294573

使用Anaconda安装
使用Anaconda安装使用faiss是最方便快速的方式,facebook会及时推出faiss的新版本conda安装包,在conda安装时会自行安装所需的libgcc, mkl, numpy模块。
faiss的cpu版本目前仅支持Linux和MacOS操作系统,gpu版本提供可在Linux操作系统下用CUDA8.0/CUDA9.0/CUDA9.1编译的版本。
注意,上面语句中的cuda90并不会执行安装CUDA的操作,需要提前自行安装。

#安装cpu版本
#更新conda
conda update conda
#先安装mkl
conda install mkl
#安装faiss-cpu
conda install faiss-cpu -c pytorch
#测试安装是否成功
python -c "import faiss"

========faiss_demo.py========

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')
xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.


index = faiss.IndexFlatL2(d)   # build the index
print(index.is_trained)
index.add(xb)                  # add vectors to the index
print(index.ntotal)


k = 7                          # we want to see 4 nearest neighbors
D, I = index.search(xb[:5], k) # sanity check
print(I)
print(D)


D, I = index.search(xq, k)     # actual search
print(I[:5])                   # neighbors of the 5 first queries
print(I[-5:])                  # neighbors of the 5 last queries
➜  faiss_demo python faiss_demo.py
True
100000
[[   0  393  363   78  924  364  100]
 [   1  555  277  364  617  175 1063]
 [   2  304  101   13  801  134  365]
 [   3  173   18  182  484   64  527]
 [   4  288  370  531  178  381  175]]
[[0.        7.1751733 7.207629  7.2511625 7.321895  7.351989  7.404186 ]
 [0.        6.3235645 6.684581  6.7999454 6.8844795 6.919898  6.927656 ]
 [0.        5.7964087 6.391736  7.2815123 7.640502  7.7231803 7.80859  ]
 [0.        7.2779055 7.5279865 7.6628466 7.7859573 7.790914  7.8467503]
 [0.        6.7638035 7.2951202 7.3688145 7.3900466 7.46482   7.518219 ]]
[[ 381  207  210  477  588  329  417]
 [ 526  911  142   72  300  576   64]
 [ 838  527 1290  425  281  637 1541]
 [ 196  184  164  359  466  599  281]
 [ 526  377  120  425  545  917  161]]
[[ 9900 10500  9309  9831  9810 10808 10568]
 [11055 10895 10812 11321 10260 10403  9829]
 [11353 11103 10164  9787 10719  9380 10118]
 [10571 10664 10632  9638  9589 10203  9894]
 [ 9628  9554 10036  9582 10304  9622 10103]]
原文地址:https://www.cnblogs.com/TMatrix52/p/12056928.html