Faiss教程:索引(1)
索引是faiss的关键知识,我们重点介绍下。
索引方法汇总
有些索引名,我就不翻译了,根据英文名去学习更准确。
| 索引名 | 类名 | index_factory | 主要参数 | 字节数/向量 | 精准检索 | 备注 |
|---|---|---|---|---|---|---|
| 精准的L2搜索 | IndexFlatL2 | "Flat" | d | 4*d | yes | brute-force |
| 精准的内积搜索 | IndexFlatIP | "Flat" | d | 4*d | yes | 归一化向量计算cos |
| Hierarchical Navigable Small World graph exploration | IndexHNSWFlat | "HNSWx,Flat" | d, M | 4*d + 8 * M | no | - |
| 倒排文件 | IndexIVFFlat | "IVFx,Flat" | quantizer, d, nlists, metric | 4*d | no | 需要另一个量化器来建立倒排 |
| Locality-Sensitive Hashing (binary flat index) | IndexLSH | - | d, nbits | nbits/8 | yes | optimized by using random rotation instead of random projections |
| Scalar quantizer (SQ) in flat mode | IndexScalarQuantizer | "SQ8" | d | d | yes | 每个维度项可以用4 bit表示,但是精度会受到一定影响 |
| Product quantizer (PQ) in flat mode | IndexPQ | "PQx" | d, M, nbits | M (if nbits=8) | yes | - |
| IVF and scalar quantizer | IndexIVFScalarQuantizer | "IVFx,SQ4" "IVFx,SQ8" | quantizer, d, nlists, qtype | d or d/2 | no | 有两种编码方式:每个维度项4bit或8bit |
| IVFADC (coarse quantizer+PQ on residuals) | IndexIVFPQ | "IVFx,PQy" | quantizer, d, nlists, M, nbits | M+4 or M+8 | no | 内存和数据id(int、long)相关,目前只支持 nbits <= 8 |
| IVFADC+R (same as IVFADC with re-ranking based on codes) | IndexIVFPQR | "IVFx,PQy+z" | quantizer, d, nlists, M, nbits, M_refine, nbits_refine | M+M_refine+4 or M+M_refine+8 | no | - |
Cell-probe方法
加速查找的典型方法是对数据集进行划分,我们采用了基于Multi-probing(best-bin KD树变体)的分块方法。
- 特征空间被切分为ncells个块
- 数据被划分到这些块中(k-means可根据最近欧式距离),归属关系存储在ncells个节点的倒排列表中
- 搜索时,检索离目标距离最近的nprobe个块
- 根据倒排列表检索nprobe个块中的所有数据。
这便是IndexIVFFlat,它需要另一个索引来记录倒排列表。
IndexIVFKmeans 和 IndexIVFSphericalKmeans 不是对象而是方法,它们可以返回IndexIVFFlat对象。
注意:对于高维的数据,要达到较好的召回,需要的nprobes可能很大
和LSH的关系
最流行的cell-probe方法可能是原生的LSH方法,可参考E2LSH。然而,这个方法及其变体有两大弊端:
- 需要大量的哈希函数(=分块数),来达到可以接受的结果
- 哈希函数很难基于输入动态调整,实际应用中容易返回次优结果
LSH的示例
n_bits = 2 * d
lsh = faiss.IndexLSH (d, n_bits)
lsh.train (x_train)
lsh.add (x_base)
D, I = lsh.search (x_query, k)
d是输入数据的维度,nbits是存储向量的bits数目。
PQ的示例
m = 16 # number of subquantizers
n_bits = 8 # bits allocated per subquantizer
pq = faiss.IndexPQ (d, m, n_bits) # Create the index
pq.train (x_train) # Training
pq.add (x_base) # Populate the index
D, I = pq.search (x_query, k) # Perform a search
带倒排的PQ:IndexIVFPQ
coarse_quantizer = faiss.IndexFlatL2 (d)
index = faiss.IndexIVFPQ (coarse_quantizer, d,
ncentroids, m, 8)
index.nprobe = 5
复合索引
使用PQ作粗粒度量化器的Cell Probe方法
相应的文章见:The inverted multi-index, Babenko & Lempitsky, CVPR'12。在Faiss中可使用MultiIndexQuantizer,它不需要add任何向量,因此将它应用在IndexIVF时需要设置quantizer_trains_alone。
nbits_mi = 12 # c
M_mi = 2 # m
coarse_quantizer_mi = faiss.MultiIndexQuantizer(d, M_mi, nbits_mi)
ncentroids_mi = 2 ** (M_mi * nbits_mi)
index = faiss.IndexIVFFlat(coarse_quantizer_mi, d, ncentroids_mi)
index.nprobe = 2048
index.quantizer_trains_alone = True
预过滤PQ编码,汉明距离的计算比PQ距离计算快6倍,通过对PQ中心的合理重排序,汉明距离可以正确地替代PQ编码距离。在搜索时设置汉明距离的阈值,可以避免PQ比较的大量运算。
# IndexPQ
index = faiss.IndexPQ (d, 16, 8)
# before training
index.do_polysemous_training = true
index.train (...)
# before searching
index.search_type = faiss.IndexPQ.ST_polysemous
index.polysemous_ht = 54 # the Hamming threshold
index.search (...)
# IndexIVFPQ
index = faiss.IndexIVFPQ (coarse_quantizer, d, 16, 8)
# before training
index. do_polysemous_training = true
index.train (...)
# before searching
index.polysemous_ht = 54 # the Hamming threshold
index.search (...)
阈值设定是注意两点:
- 阈值在0到编码bit数(16*8)之间
- 阈值越小,留下的需要计算的PQ中心数越少,推荐<1/2*bits
复合索引中也可以建立多级PQ量化索引。
预处理和后处理
为了获得更好的索引,可以remap向量ids,对数据集进行变换,re-rank检索结果等。
Faiss id mapping
默认情况下,Faiss为每个向量设置id。有些Index实现了add_with_ids方法,为向量添加64bit的ids,检索时返回ids而不需返回原始向量。
index = faiss.IndexFlatL2(xb.shape[1])
ids = np.arange(xb.shape[0])
index.add_with_ids(xb, ids) # this will crash, because IndexFlatL2 does not support add_with_ids
index2 = faiss.IndexIDMap(index)
index2.add_with_ids(xb, ids) # works, the vectors are stored in the underlying index
IndexIVF原生提供了ass_with_ids方法,就不需要IndexIDMap了。
预变换
| 变换方法 | 类名 | 备注 |
|---|---|---|
| random rotation | RandomRotationMatrix | useful to re-balance components of a vector before indexing in an IndexPQ or IndexLSH |
| remapping of dimensions | RemapDimensionsTransform | 为适应索引推荐的维度,通过重排列减少或增加向量维度d |
| PCA | PCAMatrix | 降维 |
| OPQ rotation | OPQMatrix | OPQ通过旋转输入向量更利于PQ编码,见 Optimized product quantization, Ge et al., CVPR'13 |
换行可以通过train进行训练,通过apply应用到数据上。这些变化可以通过IndexPreTransform方法应用到索引上。
# the IndexIVFPQ will be in 256D not 2048
coarse_quantizer = faiss.IndexFlatL2 (256)
sub_index = faiss.IndexIVFPQ (coarse_quantizer, 256, ncoarse, 16, 8)
# PCA 2048->256
# also does a random rotation after the reduction (the 4th argument)
pca_matrix = faiss.PCAMatrix (2048, 256, 0, True)
#- the wrapping index
index = faiss.IndexPreTransform (pca_matrix, sub_index)
# will also train the PCA
index.train(...)
# PCA will be applied prior to addition
index.add(...)
IndexRefineFlat
对搜索结果进行精准重排序
q = faiss.IndexPQ (d, M, nbits_per_index)
rq = faiss.IndexRefineFlat (q)
rq.train (xt)
rq.add (xb)
rq.k_factor = 4
D, I = rq:search (xq, 10)
从IndexPQ的最近4*10个邻域中,计算真实距离,返回最好的10个结果。注意IndexRefineFlat需要积累全向量,占用内存较高。
IndexShards
如果数据分开为多个索引,查询时需要合并结果集。这在多GPU以及平行查询中是必需的。
Faiss教程:索引(1)的更多相关文章
- 【转】微信公众账号 Senparc.Weixin.MP SDK 开发教程 索引
微信公众账号 Senparc.Weixin.MP SDK 开发教程 索引 Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到 ...
- Faiss教程:基础
Faiss对一些基础算法提供了非常高效的实现:k-means.PCA.PQ编解码. 聚类 假设2维tensor x: ncentroids = 1024 niter = 20 verbose = Tr ...
- Spring教程索引
Spring教程索引 2016-11-15 1 入门 1 概述.深入浅出Spring(一)Spring概述 2 体系结构 3 环境设置 4 Hello World 实例 5 IoC 容器 IoC容 ...
- Faiss教程:索引(2)
索引的I/O与复制 所有的函数都是深复制,我们不需要关心对象关系. I/O函数: write_index(index, "large.index"): 写索引到文件 Index * ...
- Orchard教程索引页
Orchard官方教程(译)索引 链接标注 原文 则表示未译,其他带有中文标题的表示译文内容. 入门 安装Orchard--Installing Orchard 通过zip包手动安装Orchard-- ...
- 微信公众账号 Senparc.Weixin.MP SDK 开发教程 索引
Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享. ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程 索引
Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享. ...
- Senparc.Weixin SDK 微信公众号 .NET 开发教程 索引
Senparc.WeixinSDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享.也欢迎大 ...
- Faiss教程:入门
Faiss处理固定维度d的数据,矩阵每一行表示一个向量,每列表示向量的一项.Faiss采用32-bit浮点型存储. 假设xb为数据集,维度为\(nb\times{d}\):xq是查询数据,维度为\(n ...
随机推荐
- 转:mvc 当中 [ValidateAntiForgeryToken] 的作用
一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...
- jQuery Chosen 使用
jQuery Chosen 使用 1. jQuery Chosen 选择框下拉值重置: 1 2 3 4 //重置 $("#easySelBrands").val(" ...
- adb 切换android输入法
自动化测试执行时,使用了appium输入法,再手动使用时,需要进入设置里面进行切换.adb 也提供了一条命令使用命令切换 1.找出android里面有多少输入法:adb shell ime list ...
- tomcat 8.0 进程没有全部杀死
The web application [FileIO_new_interface] created a ThreadLocal with key of type [java.lang.ThreadL ...
- ELK logstash邮件报警
这个方法有一个问题就是我这边不能给我们公司的邮箱发邮件.还有就是我们有两个邮箱一个是腾讯企业邮箱,还有一个就是我们的集团邮箱 使用下面的这个方法是不能给我们的集团邮箱发邮件的.第二个问题就是这个方法给 ...
- JDK1.6新特性,基础类库篇,集合框架(Collections)
2006 年底,Sun 公司发布了 Java Standard Edition 6(Java SE 6)的最终正式版,代号 Mustang(野马). 集合框架增强如下: 1. 增加了如下新接口(Int ...
- 使用组件构建Android应用程序
原文链接:http://android.eoe.cn/topic/android_sdk 应用程序组件 Android's application framework lets you create ...
- Android: 设置 app 字体大小不跟随系统字体调整而变化
在做 app 内字体大小的需求,类似于 微信中设置字体大小. 那么就需要 app 不跟随系统字体大小调整而变化,找到了两个方法. 方法1: 重写 getResource() 方法,修改 configu ...
- [nginx]站点目录及文件访问控制
nginx.conf配置文件 http ->多个server -> 多个location ->可限制目录和文件访问(根据i扩展名限制或者rewrite.) 根据目录或扩展名,禁止用户 ...
- android 中毛玻璃效果的实现
最近在做一款叫叽叽的App(男银懂的),其中有一个功能需要对图片处理实现毛玻璃的特效 进过一番预研,找到了3中实现方案,其中各有优缺点: 1.如果系统的api在16以上,可以使用系统提供的方法直接处理 ...