美文网首页
faiss的安装使用

faiss的安装使用

作者: 雪糕遇上夏天 | 来源:发表于2021-11-25 16:08 被阅读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)   # 分别返回距离和索引

相关文章

  • faiss的安装使用

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

  • Faiss安装

    一、使用conda安装 安装conda(略) 安装faiss 我下载的python版本是3.9的,导致无法安装fa...

  • faiss安装及使用

    一、安装 方法一:通过anaconda安装 下载anaconda: 1. 下载anaconda (仅供参考:htt...

  • Faiss学习:一

    在多个GPU上运行Faiss以及性能测试 一、Faiss的基本使用 1.1在CPU上运行 Faiss的所有算法都是...

  • faiss安装

    在运行代码时,服务依赖faiss,本以为简单的pip install faiss即可解决,却发现安装之后,尝试导入...

  • faiss 使用

    添加faiss到python路径 参照官网: https://github.com/facebookresearc...

  • faiss-1.5.0版在cuda9.0(1080Ti)、cud

    背景 不支持conda安装cuda9.2版本的faiss。faiss-1.5.1版本编译不成功。 步骤 下载fai...

  • 2020-09-22 faiss聚类接口

    Linux下,利用conda安装faiss也比较简单,几个命令即可: 安装cpu版本 更新conda conda ...

  • Faiss - 部署安装与Demo

    1. 概述 Faiss is a library for efficient similarity search ...

  • Faiss cpu 版本安装记

    由于工作需要,临时了解到一个Faiss,据说是一款较好的找相似图的工具,这里主要记录下我安装cpu版本的一个过程。...

网友评论

      本文标题:faiss的安装使用

      本文链接:https://www.haomeiwen.com/subject/hhaatrtx.html