英文

多语言-E5-large

Text Embeddings by Weakly-Supervised Contrastive Pre-training .梁王,南杨,黄晓龙,焦宾星,杨林军,江大新,Rangan Majumder,韦富儒,arXiv 2022

该模型共有24层,嵌入大小为1024。

用法

以下是从MS-MARCO段落排序数据集中对查询和段落进行编码的示例。

import torch.nn.functional as F

from torch import Tensor
from transformers import AutoTokenizer, AutoModel


def average_pool(last_hidden_states: Tensor,
                 attention_mask: Tensor) -> Tensor:
    last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
    return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]


# Each input text should start with "query: " or "passage: ", even for non-English texts.
# For tasks other than retrieval, you can simply use the "query: " prefix.
input_texts = ['query: how much protein should a female eat',
               'query: 南瓜的家常做法',
               "passage: As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
               "passage: 1.清炒南瓜丝 原料:嫩南瓜半个 调料:葱、盐、白糖、鸡精 做法: 1、南瓜用刀薄薄的削去表面一层皮,用勺子刮去瓤 2、擦成细丝(没有擦菜板就用刀慢慢切成细丝) 3、锅烧热放油,入葱花煸出香味 4、入南瓜丝快速翻炒一分钟左右,放盐、一点白糖和鸡精调味出锅 2.香葱炒南瓜 原料:南瓜1只 调料:香葱、蒜末、橄榄油、盐 做法: 1、将南瓜去皮,切成片 2、油锅8成热后,将蒜末放入爆香 3、爆香后,将南瓜片放入,翻炒 4、在翻炒的同时,可以不时地往锅里加水,但不要太多 5、放入盐,炒匀 6、南瓜差不多软和绵了之后,就可以关火 7、撒入香葱,即可出锅"]

tokenizer = AutoTokenizer.from_pretrained('intfloat/multilingual-e5-large')
model = AutoModel.from_pretrained('intfloat/multilingual-e5-large')

# Tokenize the input texts
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')

outputs = model(**batch_dict)
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])

# normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:2] @ embeddings[2:].T) * 100
print(scores.tolist())

支持的语言

该模型从 xlm-roberta-large 初始化,并在多语言数据集的混合中持续训练。它支持来自xlm-roberta的100种语言,但低资源语言可能会出现性能下降。

培训细节

初始化: xlm-roberta-large

第一阶段:对比度预训练与弱监督

Dataset Weak supervision # of text pairs
Filtered 1236321 (title, page content) 1B
1237321 (title, news content) 400M
1238321 translation pairs 2.4B
1239321 (hierarchical section title, passage) 150M
Filtered 12310321 (comment, response) 800M
12311321 (title, abstract) and citation pairs 100M
12312321 (question, answer) 50M
12313321 (input prompt, response) 80M
12314321 - 10M

第二阶段:监督微调

Dataset Language # of text pairs
12315321 English 500k
12316321 English 70k
12317321 English 60k
12318321 English <300k
12319321 English 500k
12320321 Chinese 86k
12321321 English 70k
12322321 English 70k
12323321 English 87k
12324321 English 150k
12325321 11 languages 50k
12326321 16 languages 40k

对于所有标记的数据集,我们只使用其训练集进行微调。

有关其他培训详细信息,请参阅我们在 https://arxiv.org/pdf/2212.03533.pdf 的论文。

Mr. TyDi 上的基准测试结果

Model Avg MRR@10 ar bn en fi id ja ko ru sw te th
BM25 33.3 36.7 41.3 15.1 28.8 38.2 21.7 28.1 32.9 39.6 42.4 41.7
mDPR 16.7 26.0 25.8 16.2 11.3 14.6 18.1 21.9 18.5 7.3 10.6 13.5
BM25 + mDPR 41.7 49.1 53.5 28.4 36.5 45.5 35.5 36.2 42.7 40.5 42.0 49.2
multilingual-e5-small 64.4 71.5 66.3 54.5 57.7 63.2 55.4 54.3 60.8 65.4 89.1 70.1
multilingual-e5-base 65.9 72.3 65.0 58.5 60.8 64.9 56.6 55.8 62.7 69.0 86.6 72.7
multilingual-e5-large 70.5 77.5 73.2 60.8 66.8 68.5 62.5 61.6 65.8 72.7 90.2 76.2

MTEB基准评估

请访问 unilm/e5 ,以重现对 BEIR MTEB benchmark 的评估结果。

对句子转换器的支持

以下是与sentence_transformers一起使用的示例。

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('intfloat/multilingual-e5-large')
input_texts = [
    'query: how much protein should a female eat',
    'query: 南瓜的家常做法',
    "passage: As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 i     s 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or traini     ng for a marathon. Check out the chart below to see how much protein you should be eating each day.",
    "passage: 1.清炒南瓜丝 原料:嫩南瓜半个 调料:葱、盐、白糖、鸡精 做法: 1、南瓜用刀薄薄的削去表面一层皮     ,用勺子刮去瓤 2、擦成细丝(没有擦菜板就用刀慢慢切成细丝) 3、锅烧热放油,入葱花煸出香味 4、入南瓜丝快速翻炒一分钟左右,     放盐、一点白糖和鸡精调味出锅 2.香葱炒南瓜 原料:南瓜1只 调料:香葱、蒜末、橄榄油、盐 做法: 1、将南瓜去皮,切成片 2、油     锅8成热后,将蒜末放入爆香 3、爆香后,将南瓜片放入,翻炒 4、在翻炒的同时,可以不时地往锅里加水,但不要太多 5、放入盐,炒匀      6、南瓜差不多软和绵了之后,就可以关火 7、撒入香葱,即可出锅"
]
embeddings = model.encode(input_texts, normalize_embeddings=True)

包要求

pip install sentence_transformers~=2.2.2

贡献者: michaelfeil

常见问题

1. 我需要为输入文本添加前缀“query:”和“passage:”吗?

是的,这是模型的训练方式,否则会导致性能下降。

这里有一些经验规则:

  • 对于非对称任务,如开放型QA中的段落检索,临时信息检索,使用相应的“query:”和“passage:”。

  • 对于对称任务,如语义相似性,双文本挖掘,去重复检索,请使用“query:”前缀。

  • 如果要将嵌入用作特征,例如线性探测分类,聚类,请使用“query:”前缀。

2.为什么我重新生成的结果与模型卡中报告的结果稍有不同?

不同版本的transformers和pytorch可能会导致微小但非零的性能差异。

引文

如果您发现我们的论文或模型有帮助,请考虑如下引用:

@article{wang2022text,
  title={Text Embeddings by Weakly-Supervised Contrastive Pre-training},
  author={Wang, Liang and Yang, Nan and Huang, Xiaolong and Jiao, Binxing and Yang, Linjun and Jiang, Daxin and Majumder, Rangan and Wei, Furu},
  journal={arXiv preprint arXiv:2212.03533},
  year={2022}
}

限制

长文本将被截断为最多512个标记。