HuggingFace镜像/nli-roberta-base
模型介绍文件和版本分析
下载使用量0

使用方法

预训练模型可按如下方式使用:

from openmind_hub import snapshot_download
model = CrossEncoder('HangZhou_Ascend/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.')])

#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
import torch_npu
model = AutoModelForSequenceClassification.from_pretrained('root/work/model/nli-roberta-base')
tokenizer = AutoTokenizer.from_pretrained('root/work/model/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)