ONNX 模型库
返回模型

说明文档

all-MiniLM-L12-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-L12-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-L12-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L12-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)

评估结果

有关此模型的自动评估,请参阅 Sentence Embeddings Benchmarkhttps://seb.sbert.net


背景

该项目旨在使用自监督对比学习目标在超大规模句子级数据集上训练句子嵌入模型。我们使用了预训练的 microsoft/MiniLM-L12-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 个词片的输入文本会被截断。

训练过程

预训练

我们使用预训练的 microsoft/MiniLM-L12-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 and 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

sheldonrobinson/all-MiniLM-L12-v2

作者 sheldonrobinson

sentence-similarity sentence-transformers
↓ 0 ♥ 0

创建时间: 2024-11-04 17:41:32+00:00

更新时间: 2024-10-11 19:11:39+00:00

在 Hugging Face 上查看

文件 (27)

.gitattributes
1_Pooling/config.json
README.md
config.json
config_sentence_transformers.json
data_config.json
model.safetensors
modules.json
onnx/model.onnx ONNX
onnx/model_O1.onnx ONNX
onnx/model_O2.onnx ONNX
onnx/model_O3.onnx ONNX
onnx/model_O4.onnx ONNX
onnx/model_qint8_arm64.onnx ONNX
onnx/model_qint8_avx512.onnx ONNX
onnx/model_qint8_avx512_vnni.onnx ONNX
onnx/model_quint8_avx2.onnx ONNX
openvino/openvino_model.bin
openvino/openvino_model.xml
pytorch_model.bin
rust_model.ot
sentence_bert_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json
train_script.py
vocab.txt