模型:
这是一个 sentence-transformers 模型:它将句子和段落映射到一个384维的密集向量空间,可用于聚类或语义搜索等任务。
当你安装了 sentence-transformers 后,使用这个模型变得很容易:
pip install -U sentence-transformers
然后你可以像这样使用模型:
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('obrizum/all-MiniLM-L6-v2')
embeddings = model.encode(sentences)
print(embeddings)
如果没有 sentence-transformers ,你可以像这样使用模型:首先,将您的输入通过变换器模型,然后您必须在上下文化的单词嵌入之上应用正确的汇聚操作。
from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
#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('obrizum/all-MiniLM-L6-v2')
model = AutoModel.from_pretrained('obrizum/all-MiniLM-L6-v2')
# 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
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# Normalize embeddings
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
对于对该模型的自动评估,请参见Sentence Embeddings Benchmark: https://seb.sbert.net
该项目旨在使用自我监督对比学习目标在非常大的句子级数据集上训练句子嵌入模型。我们使用了预训练的 nreimers/MiniLM-L6-H384-uncased 模型,并在10亿个句子对数据集上进行了微调。我们使用对比学习目标:给定一对中的一个句子,模型应该预测在我们的数据集中随机采样的其他句子中的哪一个与之配对。
我们在Hugging Face组织的 Community week using JAX/Flax for NLP & CV 中开发了这个模型,并参与了 Train the Best Sentence Embedding Model Ever with 1B Training Pairs 项目的开发。我们从Google Flax、JAX和Cloud团队成员关于高效深度学习框架的介入以及使用7个TPU v3-8的高效硬件基础设施来运行项目中获得了帮助。
我们的模型旨在用作句子和短段落编码器。给定一个输入文本,它输出一个捕捉语义信息的向量。句向量可以用于信息检索、聚类或句子相似性任务。
默认情况下,超过256个单词的输入文本会被截断。
我们使用了预训练的 nreimers/MiniLM-L6-H384-uncased 模型。有关预训练过程的详细信息,请参阅模型卡片。
我们使用对比目标对模型进行微调。形式上,我们计算批次中每对可能的句子对之间的余弦相似度。然后,通过与真实对进行比较,应用交叉熵损失。
超参数我们在TPU v3-8上训练了我们的模型。我们训练模型100k步,使用了批量大小为1024(每个TPU核心128个)。我们使用了500的学习率预热。序列长度限制为128个标记。我们使用了AdamW优化器,学习率为2e-5。完整的训练脚本可在当前存储库中访问:train_script.py。
训练数据我们使用了多个数据集的连接来微调我们的模型。句子对的总数超过10亿个句子。我们根据数据配置文件(data_config.json)中详细配置的加权概率对每个数据集进行了采样。
| Dataset | Paper | Number of training tuples |
|---|---|---|
| 12311321 | 12312321 | 726,484,430 |
| 12313321 Citation pairs (Abstracts) | 12314321 | 116,288,806 |
| 12315321 Duplicate question pairs | 12316321 | 77,427,422 |
| 12317321 (Question, Answer) pairs | 12318321 | 64,371,441 |
| 12313321 Citation pairs (Titles) | 12314321 | 52,603,982 |
| 12313321 (Title, Abstract) | 12314321 | 41,769,185 |
| 12323321 (Title, Body) pairs | - | 25,316,456 |
| 12323321 (Title+Body, Answer) pairs | - | 21,396,559 |
| 12323321 (Title, Answer) pairs | - | 21,396,559 |
| 12326321 triplets | 12327321 | 9,144,553 |
| 12328321 | 12329321 | 3,012,496 |
| 12330321 (Title, Answer) | 12331321 | 1,198,260 |
| 12332321 | - | 1,151,414 |
| 12333321 Image captions | 12334321 | 828,395 |
| 12335321 citation triplets | 12336321 | 684,100 |
| 12330321 (Question, Answer) | 12331321 | 681,164 |
| 12330321 (Title, Question) | 12331321 | 659,896 |
| 12341321 | 12342321 | 582,261 |
| 12343321 | 12344321 | 325,475 |
| 12345321 | 12346321 | 317,695 |
| 12323321 Duplicate questions (titles) | 304,525 | |
| AllNLI ( 12348321 and 12349321 | 12350321 , 12351321 | 277,230 |
| 12323321 Duplicate questions (bodies) | 250,519 | |
| 12323321 Duplicate questions (titles+bodies) | 250,460 | |
| 12354321 | 12355321 | 180,000 |
| 12356321 | 12357321 | 128,542 |
| 12358321 | 12359321 | 112,696 |
| 12360321 | - | 103,663 |
| 12361321 | 12362321 | 102,225 |
| 12363321 | 12364321 | 100,231 |
| 12365321 | 12366321 | 87,599 |
| 12367321 | - | 73,346 |
| Total | 1,170,060,424 |