模型:
这是一个 sentence-transformers 模型:它将句子和段落映射到一个768维的稠密向量空间中,可用于聚类或语义搜索等任务。该模型是在 mMARCO 数据集的法语部分上进行训练的。
如果你已经安装了 sentence-transformers ,那么使用这个模型就很容易:
pip install -U sentence-transformers
然后你可以像这样使用模型:
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('antoinelouis/biencoder-camembert-base-mmarcoFR')
embeddings = model.encode(sentences)
print(embeddings)
🤗 Transformers 如果没有 sentence-transformers ,你可以这样使用模型:首先,将输入传递给变换器模型,然后必须在上下文化的词嵌入顶部应用正确的汇聚操作。
from transformers import AutoTokenizer, AutoModel
import torch
#Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
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)
# Sentences we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-camembert-base-mmarcoFR')
model = AutoModel.from_pretrained('antoinelouis/biencoder-camembert-base-mmarcoFR')
# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling. In this case, mean pooling.
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
我们在mMARCO-fr的较小开发集上对模型进行了评估,该集包含6,980个查询和8.8M个候选段落。
| MRR@10 | NDCG@10 | MAP@10 | Recall@10 | Recall@100 | Recall@500 |
|---|---|---|---|---|---|
| 28.53 | 33.72 | 27.93 | 51.46 | 77.82 | 89.13 |
下面,我们将其结果与在相同数据集上微调的其他双编码器模型进行了比较:
| model | MRR@10 | NDCG@10 | MAP@10 | Recall@10 | Recall@100 (↑) | Recall@500 | |
|---|---|---|---|---|---|---|---|
| 0 | biencoder-camembert-base-mmarcoFR | 28.53 | 33.72 | 27.93 | 51.46 | 77.82 | 89.13 |
| 1 | 1238321 | 28.04 | 33.28 | 27.5 | 51.07 | 77.68 | 88.67 |
| 2 | 1239321 | 27.6 | 32.92 | 27.09 | 50.97 | 77.41 | 87.79 |
| 3 | 12310321 | 27.63 | 32.7 | 27.01 | 50.1 | 76.85 | 88.73 |
| 4 | 12311321 | 26.8 | 31.87 | 26.23 | 49.2 | 76.44 | 87.87 |
| 5 | 12312321 | 27.2 | 32.22 | 26.63 | 49.41 | 75.71 | 86.88 |
| 6 | 12313321 | 26.36 | 31.26 | 25.82 | 47.93 | 75.42 | 86.78 |
| 7 | 12314321 | 26.3 | 31.14 | 25.74 | 47.67 | 74.57 | 86.33 |
| 8 | 12315321 | 25.75 | 30.63 | 25.24 | 47.22 | 73.96 | 85.64 |
| 9 | 12316321 | 26.17 | 30.91 | 25.67 | 47.06 | 73.5 | 85.69 |
| 10 | 12317321 | 25.49 | 30.39 | 24.99 | 47.1 | 73.48 | 86.09 |
| 11 | 12318321 | 25.18 | 29.83 | 24.64 | 45.77 | 73.16 | 85.13 |
| 12 | 12319321 | 26.22 | 30.99 | 25.69 | 47.29 | 73.09 | 84.95 |
| 13 | 12320321 | 25.94 | 30.72 | 25.43 | 46.98 | 73.07 | 84.76 |
| 14 | 12321321 | 24.57 | 29.08 | 24.04 | 44.51 | 72.54 | 85.13 |
| 15 | 12322321 | 24.72 | 29.58 | 24.25 | 46.05 | 72.19 | 84.6 |
| 16 | 12323321 | 25.43 | 30.1 | 24.88 | 46.13 | 72.16 | 83.84 |
| 17 | 12324321 | 24.74 | 29.41 | 24.23 | 45.4 | 71.52 | 84.42 |
| 18 | 12325321 | 24.77 | 29.37 | 24.21 | 45.2 | 70.84 | 83.25 |
| 19 | 12326321 | 23.86 | 28.56 | 23.39 | 44.47 | 70.57 | 83.58 |
| 20 | 12327321 | 24.39 | 28.96 | 23.91 | 44.58 | 70.36 | 82.88 |
| 21 | 12328321 | 23.94 | 28.44 | 23.46 | 43.77 | 70.08 | 82.86 |
| 22 | 12329321 | 24.78 | 29.24 | 24.23 | 44.58 | 69.59 | 82.18 |
| 23 | 12330321 | 23.38 | 27.97 | 22.91 | 43.5 | 68.96 | 81.61 |
| 24 | 12331321 | 22.4 | 26.84 | 21.95 | 41.96 | 68.88 | 82.14 |
| 25 | 12332321 | 22.87 | 27.26 | 22.37 | 42.3 | 68.78 | 81.39 |
| 26 | 12333321 | 22.86 | 27.34 | 22.41 | 42.62 | 68.4 | 81.54 |
| 27 | 12334321 | 22.44 | 26.84 | 21.97 | 41.84 | 68.17 | 80.9 |
| 28 | 12335321 | 22.29 | 26.57 | 21.8 | 41.25 | 66.78 | 79.83 |
| 29 | 12336321 | 20.06 | 24.09 | 19.66 | 37.78 | 64.27 | 77.39 |
| 30 | 12337321 | 20.32 | 24.36 | 19.9 | 38.16 | 63.98 | 77.23 |
| 31 | 12338321 | 17.7 | 21.29 | 17.31 | 33.59 | 58.76 | 73.45 |
| 32 | 12339321 | 14.94 | 18.22 | 14.59 | 29.46 | 51.94 | 66.3 |
| 33 | 12340321 | 12.44 | 15.1 | 12.14 | 24.28 | 47.82 | 63.37 |
我们使用 camembert-base 模型,并在一个500K语句对的法语数据集上进行了微调。我们使用对比学习目标:给定一对中的一句话,模型应该预测它在我们的数据集中与之配对的一组随机抽样的其他句子。形式上,我们从批次的每个可能的句子对中计算余弦相似度。然后,我们通过与真正的配对进行比较,使用温度为0.05的交叉熵损失进行计算。
超参数我们在一块32GB内存的Tesla V100 GPU上训练了该模型,共进行了20个时期(即65.7k步),使用了批大小为152。我们使用了带有初始学习率2e-05的AdamW优化器,权重衰减为0.01,前500个步骤的学习率预热,并且学习率的线性衰减。序列长度限制为128个标记。
数据我们使用法语版本的 mMARCO 数据集对模型进行了微调。mMARCO是MS MARCO数据集的多语言机器翻译版本,是一个大规模的检索数据集,包括:
@online{louis2023,
author = 'Antoine Louis',
title = 'biencoder-camembert-base-mmarcoFR: A Biencoder Model Trained on French mMARCO',
publisher = 'Hugging Face',
month = 'may',
year = '2023',
url = 'https://huggingface.co/antoinelouis/biencoder-camembert-base-mmarcoFR',
}