Downloads · 30 days
37
54% of all-time downloads
Atharva192003/zero-shot-classfier
zero-shot-classfier is a zero-shot classification model from Atharva192003. Use it when you need labels you did not train the model on. It is set up for transformers. The card lists the license as mit.
bart-large-mnli This is the checkpoint for bart-large after being trained on the MultiNLI (MNLI) dataset.
Downloads · 30 days
37
54% of all-time downloads
All-time downloads
69
Public
Repo size
—
Likes
0
Public
Click a slice to open those files.
.json2.3 MB · 83%
From the Hugging Face model README
bart-large-mnli This is the checkpoint for bart-large after being trained on the MultiNLI (MNLI) dataset.
Additional information about this model:
The bart-large model page BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension BART fairseq implementation NLI-based Zero Shot Text Classification Yin et al. proposed a method for using pre-trained NLI models as a ready-made zero-shot sequence classifiers. The method works by posing the sequence to be classified as the NLI premise and to construct a hypothesis from each candidate label. For example, if we want to evaluate whether a sequence belongs to the class "politics", we could construct a hypothesis of This text is about politics.. The probabilities for entailment and contradiction are then converted to label probabilities.
This method is surprisingly effective in many cases, particularly when used with larger pre-trained models like BART and Roberta. See this blog post for a more expansive introduction to this and other zero shot methods, and see the code snippets below for examples of using this model for zero-shot classification both with Hugging Face's built-in pipeline and with native Transformers/PyTorch code.
With the zero-shot classification pipeline The model can be loaded with the zero-shot-classification pipeline like so:
from transformers import pipeline classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") You can then use this pipeline to classify sequences into any of the class names you specify.
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'],
If more than one candidate label can be correct, pass multi_class=True to calculate each class independently:
candidate_labels = ['travel', 'cooking', 'dancing', 'exploration'] classifier(sequence_to_classify, candidate_labels, multi_class=True) #{'labels': ['travel', 'exploration', 'dancing', 'cooking'],
With manual 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]