模型简介
模型特点
模型能力
使用案例
🚀 all - mpnet - base - v1
这是一个句子转换器模型:它可以将句子和段落映射到一个768维的密集向量空间,可用于聚类或语义搜索等任务。
🚀 快速开始
✨ 主要特性
- 能够将句子和段落映射到768维的密集向量空间。
- 可用于聚类、语义搜索等多种自然语言处理任务。
📦 安装指南
如果你已经安装了sentence - transformers,使用这个模型会很容易:
pip install -U sentence-transformers
💻 使用示例
基础用法(Sentence - Transformers)
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v1')
embeddings = model.encode(sentences)
print(embeddings)
高级用法(HuggingFace Transformers)
如果没有安装sentence - transformers,你可以这样使用该模型:首先,将输入数据通过Transformer模型,然后对上下文词嵌入应用正确的池化操作。
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('sentence-transformers/all-mpnet-base-v1')
model = AutoModel.from_pretrained('sentence-transformers/all-mpnet-base-v1')
# 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)
📚 详细文档
评估结果
要对该模型进行自动评估,请参考句子嵌入基准:https://seb.sbert.net
背景
该项目旨在使用自监督对比学习目标,在非常大的句子级数据集上训练句子嵌入模型。我们使用了预训练的[microsoft/mpnet - base
](https://huggingface.co/microsoft/mpnet - base)模型,并在一个包含10亿个句子对的数据集上进行了微调。我们采用了对比学习目标:给定一对句子中的一个句子,模型应该预测在一组随机采样的其他句子中,哪一个实际上是与它在数据集中配对的。
我们是在由Hugging Face组织的[使用JAX/Flax进行自然语言处理和计算机视觉的社区周](https://discuss.huggingface.co/t/open - to - the - community - community - week - using - jax - flax - for - nlp - cv/7104)期间开发这个模型的。我们将该模型作为项目[使用10亿个训练对训练有史以来最好的句子嵌入模型](https://discuss.huggingface.co/t/train - the - best - sentence - embedding - model - ever - with - 1b - training - pairs/7354)的一部分进行开发。我们受益于高效的硬件基础设施来运行该项目:7个TPU v3 - 8,以及谷歌Flax、JAX和云团队成员在高效深度学习框架方面的指导。
预期用途
我们的模型旨在用作句子和短段落编码器。给定输入文本,它会输出一个捕获语义信息的向量。句子向量可用于信息检索、聚类或句子相似度任务。
默认情况下,超过128个词块的输入文本会被截断。
训练过程
预训练
我们使用了预训练的[microsoft/mpnet - base
](https://huggingface.co/microsoft/mpnet - base)模型。有关预训练过程的更多详细信息,请参考该模型的卡片。
微调
我们使用对比目标对模型进行微调。形式上,我们计算批次中每对可能句子的余弦相似度,然后通过与真实对进行比较来应用交叉熵损失。
超参数
我们在TPU v3 - 8上训练模型。我们使用512的批次大小(每个TPU核心64)进行了920k步的训练。我们使用了500步的学习率预热。序列长度限制为128个标记。我们使用了AdamW优化器,学习率为2e - 5。完整的训练脚本可以在当前仓库中找到:train_script.py
。
训练数据
我们使用多个数据集的组合来微调模型。句子对的总数超过10亿。我们根据加权概率对每个数据集进行采样,具体配置在data_config.json
文件中详细说明。
数据集 | 论文 | 训练元组数量 |
---|---|---|
[Reddit评论(2015 - 2018)](https://github.com/PolyAI - LDN/conversational - datasets/tree/master/reddit) | 论文 | 726,484,430 |
S2ORC引用对(摘要) | [论文](https://aclanthology.org/2020.acl - main.447/) | 116,288,806 |
[WikiAnswers](https://github.com/afader/oqa#wikianswers - corpus)重复问题对 | 论文 | 77,427,422 |
PAQ(问题,答案)对 | 论文 | 64,371,441 |
S2ORC引用对(标题) | [论文](https://aclanthology.org/2020.acl - main.447/) | 52,603,982 |
S2ORC(标题,摘要) | [论文](https://aclanthology.org/2020.acl - main.447/) | 41,769,185 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)(标题,正文)对 | - | 25,316,456 |
MS MARCO三元组 | 论文 | 9,144,553 |
GOOAQ:具有多种答案类型的开放式问答 | 论文 | 3,012,496 |
[Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset)(标题,答案) | [论文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 1,198,260 |
代码搜索 | - | 1,151,414 |
COCO图像字幕 | [论文](https://link.springer.com/chapter/10.1007%2F978 - 3 - 319 - 10602 - 1_48) | 828,395 |
SPECTER引用三元组 | [论文](https://doi.org/10.18653/v1/2020.acl - main.207) | 684,100 |
[Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset)(问题,答案) | [论文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 681,164 |
[Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset)(标题,问题) | [论文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 659,896 |
SearchQA | 论文 | 582,261 |
Eli5 | [论文](https://doi.org/10.18653/v1/p19 - 1346) | 325,475 |
Flickr 30k | 论文 | 317,695 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)重复问题(标题) | 304,525 | |
AllNLI (SNLI和MultiNLI | [论文SNLI](https://doi.org/10.18653/v1/d15 - 1075),[论文MultiNLI](https://doi.org/10.18653/v1/n18 - 1101) | 277,230 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)重复问题(正文) | 250,519 | |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)重复问题(标题 + 正文) | 250,460 | |
[句子压缩](https://github.com/google - research - datasets/sentence - compression) | [论文](https://www.aclweb.org/anthology/D13 - 1155/) | 180,000 |
Wikihow | 论文 | 128,542 |
Altlex | [论文](https://aclanthology.org/P16 - 1135.pdf) | 112,696 |
[Quora问题三元组](https://quoradata.quora.com/First - Quora - Dataset - Release - Question - Pairs) | - | 103,663 |
简单维基百科 | [论文](https://www.aclweb.org/anthology/P11 - 2117/) | 102,225 |
自然问题(NQ) | 论文 | 100,231 |
[SQuAD2.0](https://rajpurkar.github.io/SQuAD - explorer/) | [论文](https://aclanthology.org/P18 - 2124.pdf) | 87,599 |
TriviaQA | - | 73,346 |
总计 | 1,124,818,467 |
📄 许可证
本项目采用Apache 2.0许可证。







