向量检索/向量相似性计算方法(持续更新ing...)

本文探讨了向量检索中的两种关键方法:余弦相似度和线性核(点积)。通过sklearn库展示了它们的实现,并介绍了Faiss和annoy库在大规模数据上的向量检索应用。此外,还提及了排序、向量存储(向量数据库)的相关理论和术语,以及相关资源。

诸神缄默不语-个人优快云博文目录

本文介绍各种用于向量检索的向量相似性计算方法,将会简单介绍各种方法的优缺点等信息,并用toy example给出代码示例。

1. 余弦相似度

使用sklearn.metrics.pairwise.cosine_similarity实现。
最终得到的值会在[-1,1]之间。

API官方文档:sklearn.metrics.pairwise.cosine_similarity — scikit-learn 1.1.2 documentation
对计算公式的介绍:https://scikit-learn.org/stable/modules/metrics.html#cosine-similarity

计算公式:
k ( x , y ) = x y ⊤ ∥ x ∥ ∥ y ∥ k(x, y) = \frac{x y^\top}{\|x\| \|y\|} k(x,y)=x∥∥yxy

计算一组向量之间的两两相似度,代码撰写方法:

from sklearn.metrics.pairwise import cosine_similarity
cos_sim_matrix = cosine_similarity(train_feature)
#入参是一个二维矩阵,每行是一个样本特征

2. 线性核(点积)

2.1 sklearn实现

使用sklearn.metrics.pairwise.linear_kernel实现。

API官方文档:sklearn.metrics.pairwise.linear_kernel — scikit-learn 1.1.2 documentation

计算公式:
k ( x , y ) = x ⊤ y k(x, y) = x^\top y k(x,y)=xy

3. 向量检索

解决方案

  1. Faiss包:L2和/或点积向量。速度飞快
    原理:聚合键,基于簇中心查找邻居
    facebookresearch/faiss: A library for efficient similarity search and clustering of dense vectors.
  2. annoy库:可以在大规模数据上检索(200W条已试可用)
    spotify/annoy: Approximate Nearest Neighbors in C++/Python optimized for memory usage and loading/saving to disk
    pip install annoy
    示例代码:
#用TFIDF做向量(一个做文本表征的示例),然后用annoy算法计算相似度

#TFIDF部分
from sklearn.feature_extraction.text import TfidfVectorizer

tfidf=TfidfVectorizer(max_features=500)
sp_tfidf=tfidf.fit_transform(corpus)  #corpus是一个文本列表

#annoy部分
from annoy import AnnoyIndex
from tqdm import tqdm

#存储
f=sp_tfidf.shape[1]  # Length of item vector that will be indexed

t = AnnoyIndex(f, 'angular')
for i in tqdm(range(len(corpus))):
    t.add_item(i, sp_tfidf[i].toarray().squeeze())  #第2个元素是一个数组

t.build(10) # 10 trees
t.save('存储路径.ann')

#调用
u = AnnoyIndex(f, 'angular')
u.load('存储路径.ann') # super fast, will just mmap the file
print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors
#返回值是向量的索引
#如果用get_nns_by_vector的话第一个入参就改成向量,如sp_tfidf[0].toarray().squeeze()

理论和术语

  1. 单调性

4. 排序

一般来说顺序是先检索(返回一个集合)然后再排序

  1. 拟合回归模型并计算MSE
    参考资料:Recommending movies: ranking | TensorFlow Recommenders
  2. Listwise ranking
    参考资料:Listwise ranking | TensorFlow Recommenders

5. 向量存储(向量数据库)

  1. VBASE

其他参考资料

  1. 我还没看:
    1. 最近邻搜索(NN)、最大内积搜索(MIPS)与(A)LSH算法 - 知乎
    2. ChatGPT盛行的当下,向量数据库为大模型配备了一个超级大脑
    3. (2021 NeurIPS) SPANN: Highly-efficient Billion-scale Approximate Nearest Neighbor Search
    4. (2020 SIGIR) ColBERT: Efficient and Effective Passage Search via Contextualized Late Interaction over BERT
    5. facebookresearch/contriever: Contriever: Unsupervised Dense Information Retrieval with Contrastive Learning
    6. (2023 TMLR) Unsupervised Dense Information Retrieval with Contrastive Learning
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值