🚀 自然语言推理跨编码器
本模型利用跨编码器技术,基于预训练语言模型,可对句子对进行自然语言推理任务,输出矛盾、蕴含、中立三种标签的得分,还能用于零样本分类任务。
🚀 快速开始
本模型使用 SentenceTransformers 的 Cross-Encoder 类进行训练,基于 microsoft/deberta-v3-small。
✨ 主要特性
- 多数据集训练:在 SNLI 和 MultiNLI 数据集上进行训练。
- 多任务支持:可用于自然语言推理和零样本分类任务。
- 多方式使用:既可以使用
SentenceTransformers
库调用,也可以直接使用 Transformers
库调用。
📦 安装指南
文档未提及安装步骤,故跳过此章节。
💻 使用示例
基础用法
预训练模型可以按如下方式使用:
from sentence_transformers import CrossEncoder
model = CrossEncoder('cross-encoder/nli-deberta-v3-small')
scores = model.predict([('A man is eating pizza', 'A man eats something'), ('A black race car starts up in front of a crowd of people.', 'A man is driving down a lonely road.')])
label_mapping = ['contradiction', 'entailment', 'neutral']
labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]
高级用法
你也可以直接使用 Transformers
库调用该模型(无需 SentenceTransformers
库):
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-deberta-v3-small')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-v3-small')
features = tokenizer(['A man is eating pizza', 'A black race car starts up in front of a crowd of people.'], ['A man eats something', 'A man is driving down a lonely road.'], padding=True, truncation=True, return_tensors="pt")
model.eval()
with torch.no_grad():
scores = model(**features).logits
label_mapping = ['contradiction', 'entailment', 'neutral']
labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)]
print(labels)
零样本分类用法
该模型还可以用于零样本分类:
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model='cross-encoder/nli-deberta-v3-small')
sent = "Apple just announced the newest iPhone X"
candidate_labels = ["technology", "sports", "politics"]
res = classifier(sent, candidate_labels)
print(res)
📚 详细文档
训练数据
该模型在 SNLI 和 MultiNLI 数据集上进行训练。对于给定的句子对,它将输出对应于矛盾、蕴含、中立三种标签的得分。
性能表现
- SNLI 测试数据集上的准确率:91.65
- MNLI 不匹配集上的准确率:87.55
更多评估结果,请参考 SBERT.net - 预训练跨编码器。
📄 许可证
本项目采用 Apache-2.0 许可证。
属性 |
详情 |
模型类型 |
自然语言推理跨编码器 |
训练数据 |
SNLI 和 MultiNLI 数据集 |
评估指标 |
准确率 |
基础模型 |
microsoft/deberta-v3-small |
库名称 |
sentence-transformers |