模型简介
模型特点
模型能力
使用案例
🚀 all - mpnet - base - v2
这是一个句子转换器模型:它可以将句子和段落映射到一个384维的密集向量空间,可用于聚类或语义搜索等任务。
🚀 快速开始
本模型可借助 sentence - transformers 库轻松使用,也可使用 HuggingFace Transformers 库调用。以下分别介绍使用方法。
✨ 主要特性
- 能够将句子和段落映射到384维的密集向量空间。
- 可用于信息检索、聚类或句子相似度等任务。
📦 安装指南
若要使用 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-v2')
embeddings = model.encode(sentences)
print(embeddings)
使用 HuggingFace 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('sentence-transformers/all-mpnet-base-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-mpnet-base-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)
📚 详细文档
评估结果
若要对该模型进行自动评估,请参考 句子嵌入基准测试:https://seb.sbert.net
背景
本项目旨在使用自监督对比学习目标,在非常大的句子级数据集上训练句子嵌入模型。我们使用了预训练的 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和云团队成员在高效深度学习框架方面的支持。
预期用途
我们的模型旨在用作句子和短段落编码器。给定输入文本,它会输出一个捕获语义信息的向量。该句子向量可用于信息检索、聚类或句子相似度任务。
默认情况下,输入文本中超过512个词块的部分将被截断。
训练过程
预训练
我们使用了预训练的 microsoft/mpnet - base
模型。有关预训练过程的更多详细信息,请参考该模型的卡片。
微调
我们使用对比目标对模型进行微调。具体来说,我们计算批次中每对可能句子的余弦相似度,然后通过与真实对进行比较来应用交叉熵损失。
超参数
我们在TPU v3 - 8上训练模型。使用1024的批次大小(每个TPU核心128)进行了100k步的训练。我们使用了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 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml) (标题 + 正文, 答案) 对 | - | 21,396,559 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml) (标题, 答案) 对 | - | 21,396,559 |
MS MARCO 三元组 | 论文 | 9,144,553 |
GOOAQ: 具有多种答案类型的开放式问答 | 论文 | 3,012,496 |
[雅虎问答](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 |
[雅虎问答](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset) (问题, 答案) | [论文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 681,164 |
[雅虎问答](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 |
维基指南 | 论文 | 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,170,060,424 |
📄 许可证
本项目采用 Apache 2.0 许可证。







