首页 > 其他分享 >faiss

faiss

时间:2023-03-01 13:22:56浏览次数:36  
标签:index 5.2 xb np print faiss

Faiss是一个高效地稠密向量相似检索和聚类的工具包,

由Facebook开发,由C++编写,并且提供了python2和python3的封装。

 

安装

pip install faiss-cpu
pip install faiss-gpu

 

用法

  • xb 对于数据库,它包含所有必须编入索引的向量,并且我们将搜索它。它的大小是nb-by-d
  • xq对于查询向量,我们需要找到最近的邻居。它的大小是nq-by-d。如果我们只有一个查询向量,则nq = 1
import numpy as np
d = 3                           # dimension
nb = 5                      # database size 从多少个里面找最近的
nq = 2                       # nb of queries 有多少个被查找的

xb = np.array([[1, 1, 1], [1.2, 1.3, 1.2], [3, 3, 3], [5, 5, 5], [5.2, 5.3, 5.2]]).astype('float32')
# xb[:, 0] += np.arange(nb) / 1000.
xq = np.array([[1.1, 1, 1], [5.2, 5.2, 5.2]]).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)
# [[0 1 2 3]      第一行代表 和 xb[0]即[1.1, 1, 1] 最近的 4个元素,由近及远
#  [1 0 2 3]        第二行代表 和 xb[1]即[1.2, 1.3, 1.2] 最近的 4个元素,由近及远
#  [2 1 0 3]
#  [3 4 2 1]
#  [4 3 2 1]]
print(D)
# [[ 0.          0.17000002 12.         48.        ]    对应上面的实际距离
#  [ 0.          0.17000002  9.37       42.57      ]
#  [ 0.          9.37       12.         12.        ]
#  [ 0.          0.16999996 12.         42.57      ]
#  [ 0.          0.16999996 14.969999   47.999996  ]]

D, I = index.search(xq, k)     # actual search
print(I[:5])                   # neighbors of the 5 first queries
print(I[-5:])

 

 

 

参考资料:

https://www.jianshu.com/p/43db601b8af1  faiss 学习笔记(一) | 三种基础索引方式学习

https://zhuanlan.zhihu.com/p/67200902  Faiss库使用方法(一)

标签:index,5.2,xb,np,print,faiss
From: https://www.cnblogs.com/yanshw/p/17167622.html

相关文章