🚀 E5-small
本模型通過弱監督對比預訓練生成文本嵌入,有12層,嵌入大小為384。它能有效處理英文文本,在文本檢索、語義相似度等任務中表現出色。不過,該模型僅支持英文,且長文本會被截斷為最多512個標記。
🚀 快速開始
模型替換提示
2023年5月消息:請切換到 e5-small-v2,它性能更優且用法相同。
模型引用論文
Text Embeddings by Weakly-Supervised Contrastive Pre-training。作者包括Liang Wang、Nan Yang、Xiaolong Huang、Binxing Jiao、Linjun Yang、Daxin Jiang、Rangan Majumder、Furu Wei ,於2022年發表在arXiv上。
✨ 主要特性
- 特定前綴要求:每個輸入文本應以 "query: " 或 "passage: " 開頭。對於檢索以外的任務,可僅使用 "query: " 前綴。
- 多任務適用性:適用於多種自然語言處理任務,如開放問答中的段落檢索、即席信息檢索、語義相似度、釋義檢索、線性探測分類和聚類等。
- 性能一致性:儘管不同版本的
transformers
和 pytorch
可能導致細微的性能差異,但整體表現穩定。
- 餘弦相似度分佈:由於使用低溫0.01的InfoNCE對比損失,餘弦相似度得分通常分佈在0.7到1.0之間,但這對文本嵌入任務的相對排序無影響。
📦 安裝指南
使用 sentence_transformers
庫時,需安裝指定版本:
pip install sentence_transformers~=2.2.2
💻 使用示例
基礎用法
以下是對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]
input_texts = ['query: how much protein should a female eat',
'query: summit define',
"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: Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."]
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-small')
model = AutoModel.from_pretrained('intfloat/e5-small')
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'])
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:2] @ embeddings[2:].T) * 100
print(scores.tolist())
高級用法
使用 sentence_transformers
庫的示例:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('intfloat/e5-small')
input_texts = [
'query: how much protein should a female eat',
'query: summit define',
"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: Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."
]
embeddings = model.encode(input_texts, normalize_embeddings=True)
📚 詳細文檔
訓練詳情
訓練詳情請參考論文 https://arxiv.org/pdf/2212.03533.pdf。
基準評估
可查看 unilm/e5 以復現該模型在 BEIR 和 MTEB基準 上的評估結果。
常見問題解答
1. 是否需要在輸入文本中添加 "query: " 和 "passage: " 前綴?
是的,模型按此方式訓練,否則性能會下降。以下是一些經驗法則:
- 對於非對稱任務(如開放問答中的段落檢索、即席信息檢索),分別使用 "query: " 和 "passage: " 前綴。
- 對於對稱任務(如語義相似度、釋義檢索),使用 "query: " 前綴。
- 若將嵌入用作特徵(如線性探測分類、聚類),使用 "query: " 前綴。
2. 為什麼我復現的結果與模型卡片中報告的結果略有不同?
不同版本的 transformers
和 pytorch
可能導致細微但非零的性能差異。
3. 為什麼餘弦相似度得分分佈在0.7到1.0之間?
這是已知且預期的行為,因為模型使用低溫0.01的InfoNCE對比損失。對於文本檢索或語義相似度等文本嵌入任務,重要的是得分的相對順序而非絕對值,因此這不應成為問題。
🔧 技術細節
本模型有12層,嵌入大小為384。它基於弱監督對比預訓練方法,通過在輸入文本中添加特定前綴("query: " 和 "passage: ")來學習文本嵌入。在訓練過程中,使用了低溫0.01的InfoNCE對比損失,這導致餘弦相似度得分通常分佈在0.7到1.0之間。
📄 許可證
本模型使用MIT許可證。
引用
如果您認為我們的論文或模型有幫助,請按以下方式引用:
@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個標記。
貢獻者
michaelfeil