模型:
sentence-transformers/all-MiniLM-L12-v2
任务:
句子相似度数据集:
s2orc flax-sentence-embeddings/stackexchange_xml MS Marco gooaq yahoo_answers_topics code_search_net search_qa eli5 snli multi_nli wikihow natural_questions trivia_qa embedding-data/sentence-compression embedding-data/flickr30k-captions embedding-data/altlex embedding-data/simple-wiki embedding-data/QQP embedding-data/SPECTER embedding-data/PAQ_pairs embedding-data/WikiAnswers 3Aembedding-data/WikiAnswers 3Aembedding-data/PAQ_pairs 3Aembedding-data/QQP 3Aembedding-data/SPECTER 3Aembedding-data/simple-wiki 3Aembedding-data/altlex 3Aembedding-data/flickr30k-captions 3Aembedding-data/sentence-compression 3Atrivia_qa 3Anatural_questions 3Awikihow 3Amulti_nli 3Asnli 3Aeli5 3Asearch_qa 3Acode_search_net 3Ayahoo_answers_topics 3Agooaq 3AMS+Marco 3Aflax-sentence-embeddings/stackexchange_xml 3As2orc语言:
en许可:
apache-2.0这是一个 sentence-transformers 模型:它将句子和段落映射到一个384维的稠密向量空间,可用于聚类或语义搜索等任务。
当您安装了 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)
如果没有 sentence-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-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)
要自动评估此模型,请参阅句子嵌入基准评估:
https://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 项目的一部分开发了这个模型。我们从谷歌的Flax、JAX和Cloud团队成员那里获得了有关高效深度学习框架的干预,以及7个TPU v3-8的高效硬件基础设施来运行项目。
我们的模型旨在用作句子和短段落编码器。给定一个输入文本,它会输出一个捕捉语义信息的向量。句子向量可以用于信息检索、聚类或句子相似性任务。
默认情况下,超过256词片段的输入文本将被截断。
我们使用了预训练的 microsoft/MiniLM-L12-H384-uncased 模型。有关预训练过程的更详细信息,请参考模型卡片。
我们使用了对比目标对模型进行微调。形式上,我们从批次中的每个可能的句子对计算余弦相似度。然后,通过与真实对比较,应用交叉熵损失。
Hyper参数我们在TPU v3-8上训练了我们的模型。我们使用批次大小为1024(每个TPU核心为128)进行了10万步的模型训练。我们使用了500的学习率预热。序列长度限制为128个标记。我们使用了AdamW优化器和2e-5的学习率。完整的训练脚本可以在当前存储库中访问:train_script.py。
训练数据我们使用多个数据集的连接来微调我们的模型。句子对的总数超过10亿个句子。我们按照数据_config.json文件中详细说明的加权概率对每个数据集进行了采样。
Dataset | Paper | Number of training tuples |
---|---|---|
12311321 | 12312321 | 726,484,430 |
12313321 Citation pairs (Abstracts) | 12314321 | 116,288,806 |
12315321 Duplicate question pairs | 12316321 | 77,427,422 |
12317321 (Question, Answer) pairs | 12318321 | 64,371,441 |
12313321 Citation pairs (Titles) | 12314321 | 52,603,982 |
12313321 (Title, Abstract) | 12314321 | 41,769,185 |
12323321 (Title, Body) pairs | - | 25,316,456 |
12323321 (Title+Body, Answer) pairs | - | 21,396,559 |
12323321 (Title, Answer) pairs | - | 21,396,559 |
12326321 triplets | 12327321 | 9,144,553 |
12328321 | 12329321 | 3,012,496 |
12330321 (Title, Answer) | 12331321 | 1,198,260 |
12332321 | - | 1,151,414 |
12333321 Image captions | 12334321 | 828,395 |
12335321 citation triplets | 12336321 | 684,100 |
12330321 (Question, Answer) | 12331321 | 681,164 |
12330321 (Title, Question) | 12331321 | 659,896 |
12341321 | 12342321 | 582,261 |
12343321 | 12344321 | 325,475 |
12345321 | 12346321 | 317,695 |
12323321 Duplicate questions (titles) | 304,525 | |
AllNLI ( 12348321 and 12349321 | 12350321 , 12351321 | 277,230 |
12323321 Duplicate questions (bodies) | 250,519 | |
12323321 Duplicate questions (titles+bodies) | 250,460 | |
12354321 | 12355321 | 180,000 |
12356321 | 12357321 | 128,542 |
12358321 | 12359321 | 112,696 |
12360321 | - | 103,663 |
12361321 | 12362321 | 102,225 |
12363321 | 12364321 | 100,231 |
12365321 | 12366321 | 87,599 |
12367321 | - | 73,346 |
Total | 1,170,060,424 |