sentence-transformers/all-MiniLM-L6-v2
摘要
该模型将句子映射到384维向量,用于聚类和语义搜索等任务,使用对比学习在10亿句对上进行微调。它是sentence-transformers库的一部分,可以与Hugging Face Transformers配合使用。
查看缓存全文
缓存时间: 2026/09/02 17:46
sentence-transformers/all-MiniLM-L6-v2 · Hugging Face
来源:https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 这是一个 sentence-transformers (https://www.sbert.net/) 模型:它将句子和段落映射到384维的密集向量空间,可用于聚类或语义搜索等任务。
用法(Sentence-Transformers)
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#usage-sentence-transformers) 安装 sentence-transformers (https://www.sbert.net/) 后,使用此模型变得很简单:
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)
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#usage-huggingface-transformers) 不使用 sentence-transformers (https://www.sbert.net/) 时,你可以这样使用模型:首先,将输入通过Transformer模型传递,然后需要在上下文化的词嵌入之上应用正确的池化操作。
from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
#均值池化 - 考虑注意力掩码进行正确的平均
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #model_output的第一个元素包含所有词元嵌入
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 = ['This is an example sentence', 'Each sentence is converted']
# 从HuggingFace Hub加载模型
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
# 对句子进行分词
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# 计算词元嵌入
with torch.no_grad():
model_output = model(**encoded_input)
# 执行池化
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# 归一化嵌入
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("句子嵌入:")
print(sentence_embeddings)
背景
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#background)
该项目旨在使用自监督对比学习目标,在非常大的句子级数据集上训练句子嵌入模型。我们使用了预训练的 nreimers/MiniLM-L6-H384-uncased (https://huggingface.co/nreimers/MiniLM-L6-H384-uncased) 模型,并在包含10亿句对的数据集上对其进行微调。我们使用对比学习目标:给定句对中的一个句子,模型应能预测出从一组随机抽样的其他句子中,哪一个实际上是在我们的数据集中与它配对的。
我们是在Hugging Face组织的使用JAX/Flax进行NLP与CV的社区周 (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,以及Google的Flax、JAX和Cloud团队成员关于高效深度学习框架的指导。
预期用途
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#intended-uses) 我们的模型旨在用作句子和短段落编码器。给定输入文本,它输出一个向量,捕捉语义信息。该句子向量可用于信息检索、聚类或句子相似性任务。
默认情况下,超过256个词元的输入文本将被截断。
训练过程
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#training-procedure)
预训练
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#pre-training)
我们使用了预训练的 nreimers/MiniLM-L6-H384-uncased (https://huggingface.co/nreimers/MiniLM-L6-H384-uncased) 模型。关于预训练过程的更详细信息,请参考该模型卡。
微调
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#fine-tuning) 我们使用对比目标微调模型。形式上,我们计算批次中每个可能句子对的余弦相似度。然后,通过与真实配对进行比较,应用交叉熵损失。
超参数
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#hyper-parameters)
我们在TPU v3-8上训练了我们的模型。我们以1024的批次大小(每个TPU核心128)训练了10万步。我们使用了500步的学习率预热。序列长度限制为128个词元。我们使用AdamW优化器,学习率为2e-5。完整的训练脚本可在此当前仓库中获取:train_script.py。
训练数据
(https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2#training-data)
我们使用多个数据集的拼接来微调模型。句子对的总数超过10亿。我们根据加权概率对每个数据集进行采样,具体配置详见 data_config.json 文件。
| 数据集 | 论文 | 训练元组数 |
|---|---|---|
| Reddit评论 (2015-2018) (https://github.com/PolyAI-LDN/conversational-datasets/tree/master/reddit) | 论文 (https://arxiv.org/abs/1904.06472) | 726,484,430 |
| S2ORC (https://github.com/allenai/s2orc) | 引用对(摘要) 论文 (https://aclanthology.org/2020.acl-main.447/) | 116,288,806 |
| WikiAnswers (https://github.com/afader/oqa#wikianswers-corpus) | 重复问题对 论文 (https://doi.org/10.1145/2623330.2623677) | 77,427,422 |
| PAQ (https://github.com/facebookresearch/PAQ) | (问题,答案)对 论文 (https://arxiv.org/abs/2102.07033) | 64,371,441 |
| S2ORC (https://github.com/allenai/s2orc) | 引用对(标题) 论文 (https://aclanthology.org/2020.acl-main.447/) | 52,603,982 |
| S2ORC (https://github.com/allenai/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 (https://microsoft.github.io/msmarco/) | 三元组 论文 (https://doi.org/10.1145/3404835.3462804) | 9,144,553 |
| GOOAQ:具有多样化答案类型的开放问题回答 (https://github.com/allenai/gooaq) | 论文 (https://arxiv.org/pdf/2104.08727.pdf) | 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 |
| Code Search (https://huggingface.co/datasets/code_search_net) | 1,151,414 | |
| COCO (https://cocodataset.org/#home) | 图像标题 论文 (https://link.springer.com/chapter/10.1007%2F978-3-319-10602-1_48) | 828,395 |
| SPECTER (https://github.com/allenai/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 (https://huggingface.co/datasets/search_qa) | 论文 (https://arxiv.org/abs/1704.05179) | 582,261 |
| Eli5 (https://huggingface.co/datasets/eli5) | 论文 (https://doi.org/10.18653/v1/p19-1346) | 325,475 |
| Flickr 30k (https://shannon.cs.illinois.edu/DenotationGraph/) | 论文 (https://transacl.org/ojs/index.php/tacl/article/view/229/33) | 317,695 |
| Stack Exchange (https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) | 重复问题(标题) | 304,525 |
| AllNLI(SNLI (https://nlp.stanford.edu/projects/snli/) 和 MultiNLI (https://cims.nyu.edu/~sbowman/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 |
| Sentence Compression (https://github.com/google-research-datasets/sentence-compression) | 论文 (https://www.aclweb.org/anthology/D13-1155/) | 180,000 |
| Wikihow (https://github.com/pvl/wikihow_pairs_dataset) | 论文 (https://arxiv.org/abs/1810.09305) | 128,542 |
| Altlex (https://github.com/chridey/altlex/) | 论文 (https://aclanthology.org/P16-1135.pdf) | 112,696 |
| Quora Question Triplets (https://quoradata.quora.com/First-Quora-Dataset-Release-Question-Pairs) | 103,663 | |
| Simple Wikipedia (https://cs.pomona.edu/~dkauchak/simplification/) | 论文 (https://www.aclweb.org/anthology/P11-2117/) | 102,225 |
| Natural Questions (NQ) (https://ai.google.com/research/NaturalQuestions) | 论文 (https://transacl.org/ojs/index.php/tacl/article/view/1455) | 100,231 |
| SQuAD2.0 (https://rajpurkar.github.io/SQuAD-explorer/) | 论文 (https://aclanthology.org/P18-2124.pdf) | 87,599 |
| TriviaQA (https://huggingface.co/datasets/trivia_qa) | 73,346 | |
| 总计 | 1,170,060,424 |
相似文章
使用 Sentence Transformers 训练和微调多向量嵌入模型
本文介绍了如何使用 Sentence Transformers 库训练和微调多向量嵌入模型,展示了其 v6.0 更新中的新 MultiVectorEncoder 类型,并在医疗检索任务中演示了其卓越的性能。
基于Sentence Transformers的多向量(延迟交互)嵌入模型
Sentence Transformers v6.0引入了MultiVectorEncoder,支持ColBERT风格的延迟交互检索,通过熟悉的API增强了多向量嵌入模型的功能。
unsloth/MiniMax-M3-GGUF
Unsloth 发布了 MiniMax-M3 多模态模型的 GGUF 量化版本,支持图像-文本到文本任务,兼容 Transformers、llama.cpp、vLLM 等推理引擎。
使用 Sentence Transformers 的多模态 Embedding 与 Reranker 模型
Sentence Transformers v5.4 引入了对多模态嵌入和重排序的支持,允许用户使用统一的 API 对文本、图像、音频和视频进行编码和比较。
使用 Sentence Transformers 训练和微调多模态 Embedding 与 Reranker 模型
本文提供了使用 Sentence Transformers 库训练和微调多模态 Embedding 与 Reranker 模型的技术指南,展示了在基于 Qwen3-VL 的视觉文档检索任务上的性能提升。