ONNX 模型库
返回模型

说明文档

msmarco-MiniLM-L12-cos-v5

这是一个 sentence-transformers 模型:它将句子和段落映射到 768 维的密集向量空间,专为语义搜索设计。它基于 MS MARCO Passages 数据集 的 50 万个(查询,答案)对进行训练。关于语义搜索的入门介绍,请参阅:SBERT.net - Semantic Search

使用方法 (Sentence-Transformers)

安装 sentence-transformers 后,使用此模型变得非常简单:

pip install -U sentence-transformers

然后你可以这样使用该模型:

from sentence_transformers import SentenceTransformer, util

query = "How many people live in London?"
docs = ["Around 9 Million people live in London", "London is known for its financial district"]

#Load the model
model = SentenceTransformer('sentence-transformers/msmarco-MiniLM-L12-cos-v5')

#Encode query and documents
query_emb = model.encode(query)
doc_emb = model.encode(docs)

#Compute dot score between query and all document embeddings
scores = util.dot_score(query_emb, doc_emb)[0].cpu().tolist()

#Combine docs & scores
doc_score_pairs = list(zip(docs, scores))

#Sort by decreasing score
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)

#Output passages & scores
for doc, score in doc_score_pairs:
    print(score, doc)

使用方法 (HuggingFace Transformers)

如果没有使用 sentence-transformers,你可以这样使用该模型:首先将输入通过 transformer 模型,然后需要在上下文词嵌入之上应用正确的池化操作。

from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F

#Mean Pooling - Take average of all tokens
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output.last_hidden_state #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)


#Encode text
def encode(texts):
    # Tokenize sentences
    encoded_input = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')

    # Compute token embeddings
    with torch.no_grad():
        model_output = model(**encoded_input, return_dict=True)

    # Perform pooling
    embeddings = mean_pooling(model_output, encoded_input['attention_mask'])

    # Normalize embeddings
    embeddings = F.normalize(embeddings, p=2, dim=1)
	
    return embeddings


# Sentences we want sentence embeddings for
query = "How many people live in London?"
docs = ["Around 9 Million people live in London", "London is known for its financial district"]

# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/msmarco-MiniLM-L12-cos-v5")
model = AutoModel.from_pretrained("sentence-transformers/msmarco-MiniLM-L12-cos-v5")

#Encode query and docs
query_emb = encode(query)
doc_emb = encode(docs)

#Compute dot score between query and all document embeddings
scores = torch.mm(query_emb, doc_emb.transpose(0, 1))[0].cpu().tolist()

#Combine docs & scores
doc_score_pairs = list(zip(docs, scores))

#Sort by decreasing score
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)

#Output passages & scores
for doc, score in doc_score_pairs:
    print(score, doc)

技术细节

以下是关于如何使用此模型的一些技术细节:

设置
维度 768
产生归一化嵌入
池化方法 Mean pooling
适用的得分函数 dot-product (util.dot_score)、cosine-similarity (util.cos_sim) 或 euclidean distance

注意:当使用 sentence-transformers 加载时,此模型会产生长度为 1 的归一化嵌入。在这种情况下,dot-product 和 cosine-similarity 是等价的。建议使用 dot-product,因为它更快。Euclidean 距离与 dot-product 成正比,也可以使用。

引用 & 作者

此模型由 sentence-transformers 训练。

如果你觉得这个模型有帮助,欢迎引用我们的论文 Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks

@inproceedings{reimers-2019-sentence-bert,
    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
    author = "Reimers, Nils and Gurevych, Iryna",
    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
    month = "11",
    year = "2019",
    publisher = "Association for Computational Linguistics",
    url = "http://arxiv.org/abs/1908.10084",
}

sentence-transformers/msmarco-MiniLM-L12-cos-v5

作者 sentence-transformers

sentence-similarity sentence-transformers
↓ 933.3K ♥ 10

创建时间: 2022-03-02 23:29:05+00:00

更新时间: 2024-11-05 16:57:30+00:00

在 Hugging Face 上查看

文件 (28)

.gitattributes
1_Pooling/config.json
README.md
config.json
config_sentence_transformers.json
flax_model.msgpack
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
openvino/openvino_model_qint8_quantized.bin
openvino/openvino_model_qint8_quantized.xml
pytorch_model.bin
sentence_bert_config.json
special_tokens_map.json
tf_model.h5
tokenizer.json
tokenizer_config.json
vocab.txt