faise的安装使用

3 篇文章 0 订阅
2 篇文章 0 订阅

1. Faiss简介

Faiss是Facebook开源的一款用于大规模P维向量最近邻检索的工具。

Faiss is a library for efficient similarity search and clustering of dense vectors. It contains algorithms that search in sets of vectors of any size, up to ones that possibly do not fit in RAM. It also contains supporting code for evaluation and parameter tuning. Faiss is written in C++ with complete wrappers for Python/numpy. Some of the most useful algorithms are implemented on the GPU. It is developed by Facebook AI Research.

他底层采用C ++编写,并且提供了python的封装代码。主要功能是,对于一个给定的向量,在所有已知的向量库中找出与其相似度较高的向量,即该向量的前k个最近邻向量。
尤其是随着万物皆可Embedding时代的到来,Faiss越来越受到人们关注。

2. Faiss安装

可以pip安装

pip install faiss-cpu --no-cache

也可以采用conda安装

#CPU 版本
conda install faiss-cpu -c pytorch

# GPU 版本
conda install faiss-gpu cudatoolkit=8.0 -c pytorch # For CUDA8
conda install faiss-gpu cudatoolkit=9.0 -c pytorch # For CUDA9
conda install faiss-gpu cudatoolkit=10.0 -c pytorch # For CUDA10

3. Faiss Action

faiss的使用方法也比较简单,归纳为以下三个步骤:

  1. 构建向量库,对已知的数据进行向量,最终以矩阵的形式表示
  2. 为矩阵选择合适的index,将第一步得到的矩阵add到index中
  3. search得到最终结果

以IndexFlatL2为例,看一下faiss的用法:

import numpy as np 
import faiss 

d = 64 
nb = 100000
nq = 10000
# 构建向量库
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.

# 关键步骤,build index
index = faiss.IndexFlatL2(d)   
index.add(xb)

k = 4 
D, I = index.search(xq[:5], k)   # 分别返回距离和索引
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值