代码内容
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 # make faiss available
index = faiss.IndexFlatL2(d) # build the index
print(index.is_trained)
index.add(xb) # add vectors to the index
print(index.ntotal)
k = 4 # 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
运行,报错信息:
(myfaiss) [root@fair-frog-2 myfaiss]# python myfaiss.py
Traceback (most recent call last):
File "/root/myfaiss/myfaiss.py", line 12, in <module>
import faiss # make faiss available
File "/root/anaconda3/envs/myfaiss/lib/python3.10/site-packages/faiss/__init__.py", line 16, in <module>
from .loader import *
File "/root/anaconda3/envs/myfaiss/lib/python3.10/site-packages/faiss/loader.py", line 65, in <module>
from .swigfaiss import *
File "/root/anaconda3/envs/myfaiss/lib/python3.10/site-packages/faiss/swigfaiss.py", line 13, in <module>
from . import _swigfaiss
ImportError: libmkl_intel_lp64.so.1: cannot open shared object file: No such file or directory
查找一下这个库的地址
(myfaiss) [root@fair-frog-2 myfaiss]# sudo find / -name libmkl_intel_lp64.so.1
/root/anaconda3/lib/libmkl_intel_lp64.so.1
/root/anaconda3/pkgs/mkl-2021.4.0-h06a4308_640/lib/libmkl_intel_lp64.so.1
将路径加到~/.bashrc
最后一行是新增的
(myfaiss) [root@fair-frog-2 myfaiss]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/root/anaconda3/etc/profile.d/conda.sh" ]; then
. "/root/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/root/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
export LD_LIBRARY_PATH=/root/anaconda3/lib:$LD_LIBRARY_PATH
重启session重新运行
输出结果
(myfaiss) [root@fair-frog-2 myfaiss]# python myfaiss.py
True
100000
[[ 0 393 363 78]
[ 1 555 277 364]
[ 2 304 101 13]
[ 3 173 18 182]
[ 4 288 370 531]]
[[0. 7.1751733 7.207629 7.2511625]
[0. 6.3235645 6.684581 6.7999454]
[0. 5.7964087 6.391736 7.2815123]
[0. 7.2779055 7.5279875 7.6628466]
[0. 6.7638035 7.2951202 7.3688145]]
[[ 381 207 210 477]
[ 526 911 142 72]
[ 838 527 1290 425]
[ 196 184 164 359]
[ 526 377 120 425]]
[[ 9900 10500 9309 9831]
[11055 10895 10812 11321]
[11353 11103 10164 9787]
[10571 10664 10632 9638]
[ 9628 9554 10036 9582]]