英文

自然语言推理的交叉编码器(Cross-Encoder)

该模型是使用 SentenceTransformers Cross-Encoder 类别进行训练的。该模型基于 microsoft/deberta-v3-small 模型。

训练数据

该模型基于 SNLI MultiNLI 数据集进行训练。对于给定的句子对,它将输出三个分数,对应于矛盾(contradiction)、推理(entailment)和中性(neutral)标签。

性能

  • SNLI-test数据集的准确率:91.65
  • MNLI不匹配集的准确率:87.55

更多评估结果,请参阅 SBERT.net - Pretrained Cross-Encoder

用法

可以像这样使用预训练模型:

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.')])

#Convert scores to labels
label_mapping = ['contradiction', 'entailment', 'neutral']
labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]

使用Transformers AutoModel

你也可以直接使用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)