🚀 DMetaSoul/sbert-chinese-general-v2-distill
此模型是之前开源通用语义匹配模型的蒸馏版本(仅4层 BERT),适用于通用语义匹配场景。从效果来看,该模型在各种任务上泛化能力更好且编码速度更快。
🚀 快速开始
此模型可通过不同框架进行使用,下面将为你详细介绍。
📦 安装指南
使用 Sentence-Transformers 框架
若使用 sentence-transformers 框架,需先进行安装,命令如下:
pip install -U sentence-transformers
使用 HuggingFace Transformers 框架
若使用 HuggingFace Transformers 框架,可直接通过其载入模型,无需额外安装依赖。
💻 使用示例
基础用法
使用 Sentence-Transformers 框架
from sentence_transformers import SentenceTransformer
sentences = ["我的儿子!他猛然间喊道,我的儿子在哪儿?", "我的儿子呢!他突然喊道,我的儿子在哪里?"]
model = SentenceTransformer('DMetaSoul/sbert-chinese-general-v2-distill')
embeddings = model.encode(sentences)
print(embeddings)
使用 HuggingFace Transformers 框架
from transformers import AutoTokenizer, AutoModel
import torch
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
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 = ["我的儿子!他猛然间喊道,我的儿子在哪儿?", "我的儿子呢!他突然喊道,我的儿子在哪里?"]
tokenizer = AutoTokenizer.from_pretrained('DMetaSoul/sbert-chinese-general-v2-distill')
model = AutoModel.from_pretrained('DMetaSoul/sbert-chinese-general-v2-distill')
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
📚 详细文档
评估结果
这里主要跟蒸馏前对应的 teacher 模型作了对比:
性能对比
属性 |
详情 |
模型类型 |
Teacher 模型为 BERT-12-layers (102M),Student 模型为 BERT-4-layers (45M),参数量比例为 0.44x |
计算耗时 |
Teacher 模型为 23s,Student 模型为 12s,耗时降低 47% |
延迟 |
Teacher 模型为 38ms,Student 模型为 20ms,延迟降低 47% |
吞吐量 |
Teacher 模型为 418 sentence/s,Student 模型为 791 sentence/s,吞吐量提升 1.9x |
精度对比
|
csts_dev |
csts_test |
afqmc |
lcqmc |
bqcorpus |
pawsx |
xiaobu |
Avg |
Teacher |
77.19% |
72.59% |
36.79% |
76.91% |
49.62% |
16.24% |
63.15% |
56.07% |
Student |
76.49% |
73.33% |
26.46% |
64.26% |
46.02% |
11.83% |
52.45% |
50.12% |
差距 (绝对值) |
- |
- |
- |
- |
- |
- |
- |
-5.95% |
基于1万条数据测试,GPU设备是V100,batch_size=16,max_seq_len=256
📄 许可证
文档中未提及相关许可证信息。
📧 引用与作者
若你需要引用此模型或联系作者,可通过以下邮箱:
E-mail: xiaowenbin@dmetasoul.com