Algorithm 1: The algorithm of SimANS. Input: Queries and their positive documents dense retrieval model M
1 Build the ANN index on D using M. query with their relevance scores { s (g, di)} from D. 3 Compute the relevance scores of each query and its
positive documents { s (g, D+)}. 4 Generate the sampling probabilities of retrieved top-k negatives { pi } for each query using Eq. 3. 6 while M has not converged do
Sample a batch from {(g, D+,D-)}. Sample ambiguous negatives for each instance from the batch according to { pi }. Optimize parameters of M using the batch and sampled negatives. 10 end
- 2.4 利用对比学习微调方式构建负例方法 对比学习是优化向量化模型的常用训练方法:
- 目的:优化向量化模型,使其向量化后的文本,相似的在向量空间距离近,不相似的在向量空 间距离远。文档召回场景下,做对比学习(有监督)需要三元组(问题,文档正例,文档负 例)。文档正例是和问题密切相关的文档片段,文档负例是和问题不相关的文档片段,可以是 精挑细选的,也可以是随机出来的。
- 构建方法:如果是随机出来的话,完全可以用同一个 batch 里,其他问题的文档正例当作某一 个问题的文档负例,如果想要效果好,还需要有比较大的 batch size。损失函数是基于批内负 样本的交叉熵损失,如下公式所示,9、d 分别表示问题和文档正例对应的向量,「为温度系 数,sim 函数可以是 cos 相似度或者点积。 论文:SimCSE: Simple Contrastive Learning of Sentence Embeddings
•实现方法: 分别将 B1 个问题,和 B2 个文档片段通过向量化模型变成向量形式,然后通过矩阵乘积计算每个问 题和文档的相似度,最后通过交叉熵损失进行优化。如果文档负例仅来自于同一个 batch 的其他样 本的文档正例,那么 B1=B2;如果人工的给每个样本陪 k 个文档负例(比如可以通过难例挖掘得 到),那么 B2=(k+1)*B1。 q_reps = self.encode (query)# 问题矩阵维度(B1,d) d_reps = self.encode (doc)# 文档矩阵维度(B2,d) score = torch.matmul (q_reps, d_reps. transpose (O, 1))#计算相似度矩阵维度:(BI, B2)