模型:
Zero-Shot 模型旨在对德语和英语的学术搜索日志进行分类。由苏黎世联邦理工学院(ETH Zürich)的学生开发。
该模型使用 Huggingface 上 Meta 提供的 bart-large-mnli 检查点进行训练。然后,根据此项目的需求进行了微调。
此方法基于自然语言推理(NLI),请参阅 Yin et al. 。以下教程摘自 bart-large-mnli 的模型卡。
使用零射分类管道:可以使用 zero-shot-classification 管道加载模型,如下所示:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="oigele/Fb_improved_zeroshot")
然后,可以使用此管道将序列分类为您指定的任何类名。
sequence_to_classify = "natural language processing" candidate_labels = ['Location & Address', 'Employment', 'Organizational', 'Name', 'Service', 'Studies', 'Science'] classifier(sequence_to_classify, candidate_labels)
如果有多个候选标签可能是正确的,请使用 multi_class=True 将每个类别独立计算:
candidate_labels = ['Location & Address', 'Employment', 'Organizational', 'Name', 'Service', 'Studies', 'Science'] classifier(sequence_to_classify, candidate_labels, multi_class=True)使用手动 PyTorch
# pose sequence as a NLI premise and label as a hypothesis
from transformers import AutoModelForSequenceClassification, AutoTokenizer
nli_model = AutoModelForSequenceClassification.from_pretrained('oigele/Fb_improved_zeroshot/')
tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
premise = sequence
hypothesis = f'This is {label}.'
# run through model pre-trained on MNLI
x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
truncation_strategy='only_first')
logits = nli_model(x.to(device))[0]
# we throw away "neutral" (dim 1) and take the probability of
# "entailment" (2) as the probability of the label being true
entail_contradiction_logits = logits[:,[0,2]]
probs = entail_contradiction_logits.softmax(dim=1)
prob_label_is_true = probs[:,1]