pipeline_tag: 句子相似度
language: 法语
license: mit
datasets:
- unicamp-dl/mmarco
metrics:
- 召回率
tags:
- 段落检索
library_name: sentence-transformers
base_model: cmarkea/distilcamembert-base
model-index:
- name: biencoder-distilcamembert-mmarcoFR
results:
- task:
type: 句子相似度
name: 段落检索
dataset:
type: unicamp-dl/mmarco
name: mMARCO-fr
config: 法语
split: 验证集
revision: None
metrics:
- type: recall_at_500
name: Recall@500
value: 87.9
- type: recall_at_100
name: Recall@100
value: 76.4
- type: recall_at_10
name: Recall@10
value: 49.2
- type: map_at_10
name: MAP@10
value: 26.2
- type: ndcg_at_10
name: nDCG@10
value: 31.9
- type: mrr_at_10
name: MRR@10
value: 26.8
biencoder-distilcamembert-mmarcoFR
这是一个用于法语的密集单向量双编码器模型,可用于语义搜索。该模型将查询和段落映射到768维密集向量,通过余弦相似度计算相关性。
使用方法
以下是使用Sentence-Transformers、FlagEmbedding或Huggingface Transformers调用该模型的示例。
使用Sentence-Transformers
首先安装库:pip install -U sentence-transformers
。然后可以如下使用模型:
from sentence_transformers import SentenceTransformer
queries = ["这是一个查询示例。", "这是第二个示例。"]
passages = ["这是一个段落示例。", "这是另一个示例。"]
model = SentenceTransformer('antoinelouis/biencoder-distilcamembert-mmarcoFR')
q_embeddings = model.encode(queries, normalize_embeddings=True)
p_embeddings = model.encode(passages, normalize_embeddings=True)
similarity = q_embeddings @ p_embeddings.T
print(similarity)
使用FlagEmbedding
首先安装库:pip install -U FlagEmbedding
。然后可以如下使用模型:
from FlagEmbedding import FlagModel
queries = ["这是一个查询示例。", "这是第二个示例。"]
passages = ["这是一个段落示例。", "这是另一个示例。"]
model = FlagModel('antoinelouis/biencoder-distilcamembert-mmarcoFR')
q_embeddings = model.encode(queries, normalize_embeddings=True)
p_embeddings = model.encode(passages, normalize_embeddings=True)
similarity = q_embeddings @ p_embeddings.T
print(similarity)
使用Transformers
首先安装库:pip install -U transformers
。然后可以如下使用模型:
from transformers import AutoTokenizer, AutoModel
from torch.nn.functional import normalize
def mean_pooling(model_output, attention_mask):
"""在上下文词嵌入上执行均值池化,同时忽略掩码标记。"""
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
queries = ["这是一个查询示例。", "这是第二个示例。"]
passages = ["这是一个段落示例。", "这是另一个示例。"]
tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-distilcamembert-mmarcoFR')
model = AutoModel.from_pretrained('antoinelouis/biencoder-distilcamembert-mmarcoFR')
q_input = tokenizer(queries, padding=True, truncation=True, return_tensors='pt')
p_input = tokenizer(passages, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
q_output = model(**encoded_queries)
p_output = model(**encoded_passages)
q_embeddings = mean_pooling(q_output, q_input['attention_mask'])
q_embedddings = normalize(q_embeddings, p=2, dim=1)
p_embeddings = mean_pooling(p_output, p_input['attention_mask'])
p_embedddings = normalize(p_embeddings, p=2, dim=1)
similarity = q_embeddings @ p_embeddings.T
print(similarity)
评估
模型在mMARCO-fr的小型开发集上进行评估,该数据集包含6,980个查询和880万候选段落。我们报告了平均倒数排名(MRR)、归一化折损累积增益(NDCG)、平均准确率(MAP)以及不同截断点的召回率(R@k)。要了解其在法语神经检索模型中的表现,请查看DécouvrIR排行榜。
训练
数据
我们使用来自mMARCO数据集的法语训练样本,这是MS MARCO的多语言机器翻译版本,包含880万段落和53.9万训练查询。我们没有使用官方数据集提供的BM25负样本,而是从12个不同的密集检索器中挖掘更难负样本,使用msmarco-hard-negatives蒸馏数据集。
实现
模型从cmarkea/distilcamembert-base检查点初始化,并通过交叉熵损失(如DPR)优化,温度为0.05。在32GB NVIDIA V100 GPU上微调20个周期(即65.7k步),使用AdamW优化器,批量大小为152,峰值学习率为2e-5,前500步预热并线性调度。我们将问题和段落的最大序列长度设置为128个标记。使用余弦相似度计算相关性分数。
引用
@online{louis2024decouvrir,
author = 'Antoine Louis',
title = 'DécouvrIR: 法语信息检索模型鲁棒性评估基准',
publisher = 'Hugging Face',
month = '三月',
year = '2024',
url = 'https://huggingface.co/spaces/antoinelouis/decouvrir',
}