🚀 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 許可證。