返回模型
说明文档
bart-large-mnli
这是 bart-large 在 MultiNLI (MNLI) 数据集上训练后的检查点。
关于此模型的更多信息:
- bart-large 模型页面
- BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension
- BART fairseq 实现
基于NLI的零样本文本分类
Yin等人 提出了一种使用预训练NLI模型作为现成零样本序列分类器的方法。该方法的工作原理是将待分类的序列作为NLI的前提(premise),并为每个候选标签构建一个假设。例如,如果我们想评估一个序列是否属于"政治"类别,我们可以构建一个假设:This text is about politics.(这段文本是关于政治的)。然后将蕴含和矛盾的概率转换为标签概率。
这种方法在许多情况下出奇地有效,特别是与BART和Roberta等较大的预训练模型一起使用时。请参阅 这篇博客文章 了解此方法和其他零样本方法的更详细介绍,并参阅下面的代码示例,了解如何使用Hugging Face内置pipeline和原生Transformers/PyTorch代码进行零样本分类。
使用零样本分类pipeline
可以通过 zero-shot-classification pipeline 加载该模型,如下所示:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="facebook/bart-large-mnli")
然后,您可以使用此pipeline将序列分类为您指定的任何类别名称。
sequence_to_classify = "one day I will see the world"
candidate_labels = ['travel', 'cooking', 'dancing']
classifier(sequence_to_classify, candidate_labels)
#{'labels': ['travel', 'dancing', 'cooking'],
# 'scores': [0.9938651323318481, 0.0032737774308770895, 0.002861034357920289],
# 'sequence': 'one day I will see the world'}
如果多个候选标签可能同时正确,请传递 multi_label=True 以独立计算每个类别:
candidate_labels = ['travel', 'cooking', 'dancing', 'exploration']
classifier(sequence_to_classify, candidate_labels, multi_label=True)
#{'labels': ['travel', 'exploration', 'dancing', 'cooking'],
# 'scores': [0.9945111274719238,
# 0.9383890628814697,
# 0.0057061901316046715,
# 0.0018193122232332826],
# 'sequence': 'one day I will see the world'}
使用原生PyTorch
# 将序列作为NLI的前提,标签作为假设
from transformers import AutoModelForSequenceClassification, AutoTokenizer
nli_model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
premise = sequence
hypothesis = f'This example is {label}.'
# 通过在MNLI上预训练的模型运行
x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
truncation_strategy='only_first')
logits = nli_model(x.to(device))[0]
# 我们舍弃"neutral"(中性,维度1),取"entailment"(蕴含,维度2)的概率
# 作为标签为真的概率
entail_contradiction_logits = logits[:,[0,2]]
probs = entail_contradiction_logits.softmax(dim=1)
prob_label_is_true = probs[:,1]
注意:为ONNX权重设置单独的仓库是一个临时解决方案,直到WebML获得更多关注。如果您想让您的模型支持Web,我们建议使用 🤗 Optimum 转换为ONNX,并按照此仓库的结构组织(将ONNX权重放在名为 onnx 的子文件夹中)。
masato12/bart-large-mnli
作者 masato12
zero-shot-classification
Transformers PHP
↓ 0
♥ 0
创建时间: 2024-07-14 22:18:44+00:00
更新时间: 2024-07-14 22:20:28+00:00
在 Hugging Face 上查看文件 (2)
.gitattributes
README.md