🚀 bart-large-mnli
这是 bart-large 在 MultiNLI (MNLI) 数据集上训练后的检查点,可用于零样本分类任务。
🚀 快速开始
本模型是在 MultiNLI (MNLI) 数据集上对 bart-large 进行训练后得到的检查点。
关于此模型的更多信息:
✨ 主要特性
基于NLI的零样本文本分类
Yin 等人 提出了一种将预训练的自然语言推理(NLI)模型用作现成的零样本序列分类器的方法。该方法的工作原理是将待分类的序列作为 NLI 的前提,并从每个候选标签构建一个假设。例如,如果我们想评估一个序列是否属于 “政治” 类别,我们可以构建一个假设 This text is about politics.
。然后将蕴含和矛盾的概率转换为标签概率。
在许多情况下,这种方法出奇地有效,特别是与像 BART 和 Roberta 这样的大型预训练模型一起使用时。有关此方法和其他零样本方法的更详细介绍,请参阅 此博客文章。
💻 使用示例
基础用法
使用零样本分类管道
可以使用 zero-shot-classification
管道加载模型:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="facebook/bart-large-mnli")
然后可以使用此管道将序列分类到你指定的任何类别名称中。
sequence_to_classify = "one day I will see the world"
candidate_labels = ['travel', 'cooking', 'dancing']
classifier(sequence_to_classify, candidate_labels)
如果多个候选标签可能正确,可以传递 multi_label=True
来独立计算每个类别的概率:
candidate_labels = ['travel', 'cooking', 'dancing', 'exploration']
classifier(sequence_to_classify, candidate_labels, multi_label=True)
高级用法
使用手动 PyTorch 代码
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}.'
x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
truncation_strategy='only_first')
logits = nli_model(x.to(device))[0]
entail_contradiction_logits = logits[:,[0,2]]
probs = entail_contradiction_logits.softmax(dim=1)
prob_label_is_true = probs[:,1]
📄 许可证
本项目采用 MIT 许可证。