说明文档
all-MiniLM-L6-v2
这是一个 sentence-transformers 模型:它将句子和段落映射到 384 维的稠密向量空间,可用于聚类或语义搜索等任务。
使用方法 (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-MiniLM-L6-v2')
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-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/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)
背景
该项目旨在使用自监督对比学习目标在超大规模句子级数据集上训练句子嵌入模型。我们使用预训练的 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。得益于高效的硬件基础设施来运行该项目:7 台 TPU v3-8,以及 Google Flax、JAX 和 Cloud 团队成员关于高效深度学习框架的指导。
预期用途
我们的模型旨在用作句子和短段落编码器。给定输入文本,它输出一个捕获语义信息的向量。该句子向量可用于信息检索、聚类或句子相似度任务。
默认情况下,超过 256 个词片的输入文本会被截断。
训练过程
预训练
我们使用预训练的 nreimers/MiniLM-L6-H384-uncased 模型。有关预训练过程的更多详细信息,请参阅模型卡片。
微调
我们使用对比目标对模型进行微调。形式上,我们计算批次中每对可能句子的余弦相似度,然后通过与真实配对比较来应用交叉熵损失。
超参数
我们在 TPU v3-8 上训练模型。我们使用 1024 的批次大小(每个 TPU 核心 128 个)训练 100k 步。我们使用 500 步的学习率预热。序列长度限制为 128 个 token。我们使用 AdamW 优化器,学习率为 2e-5。完整的训练脚本可在当前仓库中访问:train_script.py。
训练数据
我们使用多个数据集的拼接来微调模型。句对总数超过 10 亿。我们按照加权概率对每个数据集进行采样,具体配置详见 data_config.json 文件。
| 数据集 | 论文 | 训练元组数量 |
|---|---|---|
| Reddit comments (2015-2018) | 论文 | 726,484,430 |
| S2ORC Citation pairs (Abstracts) | 论文 | 116,288,806 |
| WikiAnswers Duplicate question pairs | 论文 | 77,427,422 |
| PAQ (Question, Answer) pairs | 论文 | 64,371,441 |
| S2ORC Citation pairs (Titles) | 论文 | 52,603,982 |
| S2ORC (Title, Abstract) | 论文 | 41,769,185 |
| Stack Exchange (Title, Body) pairs | - | 25,316,456 |
| Stack Exchange (Title+Body, Answer) pairs | - | 21,396,559 |
| Stack Exchange (Title, Answer) pairs | - | 21,396,559 |
| MS MARCO triplets | 论文 | 9,144,553 |
| GOOAQ: Open Question Answering with Diverse Answer Types | 论文 | 3,012,496 |
| Yahoo Answers (Title, Answer) | 论文 | 1,198,260 |
| Code Search | - | 1,151,414 |
| COCO Image captions | 论文 | 828,395 |
| SPECTER citation triplets | 论文 | 684,100 |
| Yahoo Answers (Question, Answer) | 论文 | 681,164 |
| Yahoo Answers (Title, Question) | 论文 | 659,896 |
| SearchQA | 论文 | 582,261 |
| Eli5 | 论文 | 325,475 |
| Flickr 30k | 论文 | 317,695 |
| Stack Exchange Duplicate questions (titles) | 304,525 | |
| AllNLI (SNLI 和 MultiNLI | 论文 SNLI, 论文 MultiNLI | 277,230 |
| Stack Exchange Duplicate questions (bodies) | 250,519 | |
| Stack Exchange Duplicate questions (titles+bodies) | 250,460 | |
| Sentence Compression | 论文 | 180,000 |
| Wikihow | 论文 | 128,542 |
| Altlex | 论文 | 112,696 |
| Quora Question Triplets | - | 103,663 |
| Simple Wikipedia | 论文 | 102,225 |
| Natural Questions (NQ) | 论文 | 100,231 |
| SQuAD2.0 | 论文 | 87,599 |
| TriviaQA | - | 73,346 |
| 总计 | 1,170,060,424 |
LegumMagister/MNLP_M2_document_encoder
作者 LegumMagister
创建时间: 2025-05-29 19:34:19+00:00
更新时间: 2025-05-29 19:38:43+00:00
在 Hugging Face 上查看