说明文档
all-distilroberta-v1
这是一个 sentence-transformers 模型:它将句子和段落映射到 768 维的密集向量空间,可用于聚类或语义搜索等任务。
使用方法 (Sentence-Transformers)
当您安装了 sentence-transformers 后,使用此模型变得非常简单:
pip install -U sentence-transformers
然后您可以这样使用该模型:
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/all-distilroberta-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-distilroberta-v1')
model = AutoModel.from_pretrained('sentence-transformers/all-distilroberta-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)
背景
本项目旨在使用自监督对比学习目标,在非常大的句子级数据集上训练句子嵌入模型。我们使用了预训练的 distilroberta-base 模型,并在 10 亿个句子对的数据集上进行了微调。我们使用对比学习目标:给定句子对中的一个句子,模型应该预测在数据集中与它配对的句子是众多随机采样的其他句子中的哪一个。
我们是在 Hugging Face 组织的 社区周 - JAX/Flax 用于 NLP & CV 期间开发此模型的。作为项目 用 10 亿训练对训练出最好的句子嵌入模型 的一部分,我们开发了此模型。我们受益于高效硬件基础设施来运行项目:7 个 TPU v3-8,以及来自 Google Flax、JAX 和 Cloud 团队成员关于高效深度学习框架的帮助。
预期用途
我们的模型旨在用作句子和短段落的编码器。给定输入文本,它输出一个捕获语义信息的向量。该句子向量可用于信息检索、聚类或句子相似度任务。
默认情况下,超过 128 个词片的输入文本会被截断。
训练过程
预训练
我们使用预训练的 distilroberta-base。有关预训练程序的更多详细信息,请参阅模型卡片。
微调
我们使用对比目标对模型进行微调。形式上,我们计算批次中每个可能的句子对的余弦相似度。 然后通过与真实配对进行比较来应用交叉熵损失。
超参数
我们在 TPU v3-8 上训练模型。训练 920k 步,批量大小为 512(每个 TPU 核心 64)。
我们使用 500 的学习率预热。序列长度限制为 128 个标记。我们使用 AdamW 优化器,学习率为 2e-5。完整的训练脚本可在此当前仓库中访问:train_script.py。
训练数据
我们使用多个数据集的连接来微调模型。句子对的总数超过 10 亿。
我们根据加权概率对每个数据集进行采样,具体配置在 data_config.json 文件中详细说明。
| 数据集 | 论文 | 训练元组数量 |
|---|---|---|
| Reddit 评论 (2015-2018) | 论文 | 726,484,430 |
| S2ORC 引用对(摘要) | 论文 | 116,288,806 |
| WikiAnswers 重复问题对 | 论文 | 77,427,422 |
| PAQ(问题,答案)对 | 论文 | 64,371,441 |
| S2ORC 引用对(标题) | 论文 | 52,603,982 |
| S2ORC(标题,摘要) | 论文 | 41,769,185 |
| Stack Exchange(标题,正文)对 | - | 25,316,456 |
| MS MARCO 三元组 | 论文 | 9,144,553 |
| GOOAQ: 多样化答案类型的开放问答 | 论文 | 3,012,496 |
| Yahoo 答案(标题,答案) | 论文 | 1,198,260 |
| 代码搜索 | - | 1,151,414 |
| COCO 图像 caption | 论文 | 828,395 |
| SPECTER 引用三元组 | 论文 | 684,100 |
| Yahoo 答案(问题,答案) | 论文 | 681,164 |
| Yahoo 答案(标题,问题) | 论文 | 659,896 |
| SearchQA | 论文 | 582,261 |
| Eli5 | 论文 | 325,475 |
| Flickr 30k | 论文 | 317,695 |
| Stack Exchange 重复问题(标题) | 304,525 | |
| AllNLI(SNLI 和 MultiNLI) | 论文 SNLI, 论文 MultiNLI | 277,230 |
| Stack Exchange 重复问题(正文) | 250,519 | |
| Stack Exchange 重复问题(标题+正文) | 250,460 | |
| 句子压缩 | 论文 | 180,000 |
| Wikihow | 论文 | 128,542 |
| Altlex | 论文 | 112,696 |
| Quora 问题三元组 | - | 103,663 |
| 简单维基百科 | 论文 | 102,225 |
| 自然问题 (NQ) | 论文 | 100,231 |
| SQuAD2.0 | 论文 | 87,599 |
| TriviaQA | - | 73,346 |
| 总计 | 1,124,818,467 |
sentence-transformers/all-distilroberta-v1
作者 sentence-transformers
创建时间: 2022-03-02 23:29:05+00:00
更新时间: 2025-03-06 13:35:55+00:00
在 Hugging Face 上查看