说明文档
multi-qa-MiniLM-L6-dot-v1
这是一个 sentence-transformers 模型:它将句子和段落映射到 384 维的密集向量空间,专为语义搜索设计。它已在来自不同来源的 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-MiniLM-L6-dot-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
#CLS Pooling - Take output from first token
def cls_pooling(model_output):
return model_output.last_hidden_state[:,0]
#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 = cls_pooling(model_output)
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-MiniLM-L6-dot-v1")
model = AutoModel.from_pretrained("sentence-transformers/multi-qa-MiniLM-L6-dot-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)
技术细节
以下是关于如何使用此模型的技术细节:
| 设置 | 值 |
|---|---|
| 维度 | 384 |
| 生成归一化嵌入 | 否 |
| 池化方法 | CLS 池化 |
| 适用评分函数 | 点积(例如 util.dot_score) |
背景
该项目旨在使用自监督对比学习目标在非常大规规的句子级数据集上训练句子嵌入模型。我们使用对比学习目标:给定数据集中的一对句子,模型应该预测在这组随机采样的其他句子中,哪个句子实际上是与我这个句子配对的。
这个模型是在 Hugging Face 组织的社区周 - 使用 JAX/Flax 进行 NLP 和 CV 期间开发的。我们作为以下项目的一部分开发了这个模型: 使用 10 亿训练对训练史上最好的句子嵌入模型。我们受益于高效硬件基础设施:7 个 TPU v3-8,以及来自 Google 的 Flax、JAX 和云团队成员关于高效深度学习框架的指导。
预期用途
我们的模型用于语义搜索:它将查询/问题和文本段落编码到密集向量空间中。它可以为给定的段落找到相关文档。
请注意,存在 512 个词片的限制:超过该长度的文本将被截断。另外请注意,该模型仅在最长 250 个词片的输入文本上进行过训练。对于较长的文本,它可能效果不佳。
训练过程
完整的训练脚本可以在当前仓库中访问:train_script.py。
预训练
我们使用预训练的 nreimers/MiniLM-L6-H384-uncased 模型。请参阅模型卡片以获取有关预训练程序的更多详细信息。
训练
我们使用多个数据集的合并来微调模型。总共我们有约 2.15 亿个(问题,答案)对。
我们根据加权概率对每个数据集进行采样,具体配置详见 data_config.json 文件。
该模型使用 MultipleNegativesRankingLoss 进行训练,使用 CLS 池化,点积作为相似度函数,缩放值为 1。
| 数据集 | 训练元组数量 |
|---|---|
| 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 万个问题的(问题,答案)对,每个问题对应 5 个 Google 搜索片段 | 582,261 |
| ELI5 来自 Reddit ELI5(像解释给我听一样解释)的(问题,答案)对 | 325,475 |
| Stack Exchange 重复问题对(标题) | 304,525 |
| Quora Question Triplets 来自 Quora 问题对数据集的(问题,重复问题,硬负样本)三元组 | 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-MiniLM-L6-dot-v1
作者 sentence-transformers
创建时间: 2022-03-02 23:29:05+00:00
更新时间: 2024-11-05 20:22:38+00:00
在 Hugging Face 上查看