说明文档
multi-qa-distilbert-cos-v1
这是一个 sentence-transformers 模型:它将句子和段落映射到 768 维的密集向量空间,专为语义搜索设计。该模型基于来自不同来源的 2.15 亿个(问题,答案)对进行训练。关于语义搜索的介绍,请参阅:SBERT.net - 语义搜索
使用方法 (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/multi-qa-distilbert-cos-v1')
#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/multi-qa-distilbert-cos-v1")
model = AutoModel.from_pretrained("sentence-transformers/multi-qa-distilbert-cos-v1")
#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 成正比,也可以使用。
背景
该项目旨在使用大规模句子级数据集和自监督对比学习目标来训练句子嵌入模型。我们使用对比学习目标:给定一对句子中的某个句子,模型需要预测在随机采样的其他句子中,哪一个实际上与它在该数据集中是配对的。
这个模型是在 Hugging Face 社区周 - JAX/Flax 用于 NLP 和 CV 期间开发的,该活动由 Hugging Face 组织。作为项目 使用 10 亿训练对训练最好的句子嵌入模型 的一部分,我们开发了这个模型。我们受益于高效硬件基础设施来运行项目:7 个 TPU v3-8,以及来自 Google Flax、JAX 和云团队成员关于高效深度学习框架的指导。
预期用途
我们的模型旨在用于语义搜索:它将查询/问题和对段落编码到密集向量空间中。它可以为给定的文本段落找到相关文档。
请注意,存在 512 个词片的限制:超过该长度的文本将被截断。另请注意,该模型仅在最长 250 个词片的输入文本上进行训练。对于较长的文本,它可能无法很好地工作。
训练过程
完整的训练脚本可在当前仓库中访问:train_script.py。
预训练
我们使用预训练的 distilbert-base-uncased 模型。关于预训练过程的更多详细信息,请参阅模型卡片。
训练
我们使用多个数据集的合并来微调模型。总共有约 2.15 亿个(问题,答案)对。
我们根据加权概率对每个数据集进行采样,具体配置在 data_config.json 文件中详细说明。
该模型使用 MultipleNegativesRankingLoss 进行训练,使用 Mean-pooling、cosine-similarity 作为相似度函数,scale 设置为 20。
| 数据集 | 训练样本数量 |
|---|---|
| WikiAnswers 来自 WikiAnswers 的重复问题对 | 77,427,422 |
| PAQ 为维基百科每个段落自动生成的(问题,段落)对 | 64,371,441 |
| Stack Exchange 来自所有 StackExchange 的(标题,正文)对 | 25,316,456 |
| Stack Exchange 来自所有 StackExchange 的(标题,答案)对 | 21,396,559 |
| MS MARCO 来自必应搜索引擎的 50 万个查询的三元组(查询,答案,硬负样本) | 17,579,773 |
| GOOAQ: Open Question Answering with Diverse Answer Types 来自 300 万 Google 查询和 Google 特色摘要的(查询,答案)对 | 3,012,496 |
| Amazon-QA 来自亚马逊产品页面的(问题,答案)对 | 2,448,839 |
| Yahoo Answers 来自 Yahoo Answers 的(标题,答案)对 | 1,198,260 |
| Yahoo Answers 来自 Yahoo Answers 的(问题,答案)对 | 681,164 |
| Yahoo Answers 来自 Yahoo Answers 的(标题,问题)对 | 659,896 |
| SearchQA 来自 14 万个问题的(问题,答案)对,每个问题都有 Google 前 5 条摘要 | 582,261 |
| ELI5 来自 Reddit ELI5(像我是五岁一样解释)的(问题,答案)对 | 325,475 |
| Stack Exchange 重复问题对(标题) | 304,525 |
| Quora Question Triplets 来自 Quora Questions Pairs 数据集的(问题,重复问题,硬负样本)三元组 | 103,663 |
| Natural Questions (NQ) 来自 10 万个真实 Google 查询及其相关维基百科段落的(问题,段落)对 | 100,231 |
| SQuAD2.0 来自 SQuAD2.0 数据集的(问题,段落)对 | 87,599 |
| TriviaQA (问题,证据)对 | 73,346 |
| 总计 | 214,988,242 |
sentence-transformers/multi-qa-distilbert-cos-v1
作者 sentence-transformers
创建时间: 2022-03-02 23:29:05+00:00
更新时间: 2024-11-05 17:18:43+00:00
在 Hugging Face 上查看