🚀 自然语言推理跨编码器
本模型是用于自然语言推理的跨编码器,借助 SentenceTransformers 的 Cross-Encoder 类进行训练。它能对给定的句子对输出对应“矛盾”“蕴含”“中立”三个标签的得分。
🚀 快速开始
本模型可用于自然语言推理任务,通过输入句子对,输出对应不同标签的得分。同时,它也支持零样本分类任务。
✨ 主要特性
📦 安装指南
文档未提及具体安装步骤,可参考 SentenceTransformers 和 Transformers 官方文档进行安装。
💻 使用示例
基础用法
使用预训练模型进行推理:
from sentence_transformers import CrossEncoder
model = CrossEncoder('cross-encoder/nli-roberta-base')
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 库直接调用模型:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-roberta-base')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-roberta-base')
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-roberta-base')
sent = "Apple just announced the newest iPhone X"
candidate_labels = ["technology", "sports", "politics"]
res = classifier(sent, candidate_labels)
print(res)
📚 详细文档
训练数据
模型在 SNLI 和 MultiNLI 数据集上进行训练。对于给定的句子对,它将输出对应“矛盾”“蕴含”“中立”三个标签的得分。
性能评估
评估结果请参考 SBERT.net - Pretrained Cross-Encoder。
📄 许可证
本模型使用的许可证为 Apache-2.0。
📋 信息表格
属性 |
详情 |
模型类型 |
自然语言推理跨编码器 |
训练数据 |
SNLI 和 MultiNLI 数据集 |
评估指标 |
准确率 |
许可证 |
Apache-2.0 |
基础模型 |
FacebookAI/roberta-base |
库名称 |
sentence-transformers |