模型:
intfloat/multilingual-e5-base
Text Embeddings by Weakly-Supervised Contrastive Pre-training .梁王,南阳,黄小龙,焦彬星,杨琳军,江大新,拉甘·马朱姆德,韦魁富,arXiv 2022
这个模型有12层,嵌入大小为768。
以下是从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-base') model = AutoModel.from_pretrained('intfloat/multilingual-e5-base') # 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-base 中初始化,并在混合多语言数据集上进行连续训练。它支持xlm-roberta的100种语言,但低资源语言可能会出现性能下降。
初始化: xlm-roberta-base
第一阶段: 对比预训练,弱监督
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 中的论文。
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 |
查看 unilm/e5 以重现在 BEIR 和 MTEB benchmark 上的评估结果。
以下是与sentence_transformers一起使用的示例。
from sentence_transformers import SentenceTransformer model = SentenceTransformer('intfloat/multilingual-e5-base') 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个令牌。