模型库 / facebook/bart-large-mnli

bart-large-mnli

facebook zero-shot-classification transformers
facebook/bart-large-mnli
2,925,365
下载量
1567
收藏数
11
浏览量
mit
许可

简介

This is the checkpoint for bart-large after being trained on the MultiNLI (MNLI) dataset.

模型卡片

许可协议 mit
任务 zero-shot-classification
数据集
multi_nli

模型配置

模型类型 bart
架构 BartForSequenceClassification

模型详情

已翻译

bart-large-mnli

这是 bart-largeMultiNLI (MNLI) 数据集上训练后的 checkpoint。

关于该模型的更多信息:
- bart-large 模型页面
- BART:面向自然语言生成、翻译和理解的去噪序列到序列预训练
- BART fairseq 实现

基于 NLI 的零样本文本分类

Yin 等人 提出了一种方法,利用预训练的 NLI 模型作为现成的零样本序列分类器。该方法将待分类的序列作为 NLI 的前提,并为每个候选标签构建一个假设。例如,如果我们想判断一个序列是否属于"政治"类别,可以构建一个假设 这段文本是关于政治的。。然后,将蕴含和矛盾的 probability 转换为标签 probability。

这种方法在许多情况下出奇地有效,尤其是与 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

# pose sequence as a NLI premise and label as a hypothesis
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}.'

# 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]

标签

jax rust bart text-classification dataset:multi_nli arxiv:1910.13461 arxiv:1909.00161 license:mit

操作


详细信息

厂商
facebook
任务
zero-shot-classification
框架
transformers
模型类型
bart
许可(HF)
mit