Views
No views yet
hypothesis_template="This text is about {}." as this is the template used during fine-tuning.zero-shot-classification pipeline like so:1from transformers import pipeline
2nlp = pipeline("zero-shot-classification", model="joeddav/bart-large-mnli-yahoo-answers")
3
4sequence_to_classify = "Who are you voting for in 2020?"
5candidate_labels = ["Europe", "public health", "politics", "elections"]
6hypothesis_template = "This text is about {}."
7nlp(sequence_to_classify, candidate_labels, multi_class=True, hypothesis_template=hypothesis_template)1# pose sequence as a NLI premise and label as a hypothesis
2from transformers import BartForSequenceClassification, BartTokenizer
3nli_model = BartForSequenceClassification.from_pretrained('joeddav/bart-large-mnli-yahoo-answers')
4tokenizer = BartTokenizer.from_pretrained('joeddav/bart-large-mnli-yahoo-answers')
5
6premise = sequence
7hypothesis = f'This text is about {label}.'
8
9# run through model pre-trained on MNLI
10x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
11 max_length=tokenizer.max_len,
12 truncation_strategy='only_first')
13logits = nli_model(x.to(device))[0]
14
15# we throw away "neutral" (dim 1) and take the probability of
16# "entailment" (2) as the probability of the label being true
17entail_contradiction_logits = logits[:,[0,2]]
18probs = entail_contradiction_logits.softmax(dim=1)
19prob_label_is_true = probs[:,1]This text is about {class name}. For each example in the training set, a true and a randomly-selected false label hypothesis are fed to the model which must predict which labels are valid and which are false..68 and .72 for the unseen and seen labels, respectively. In order to adjust for the in-vs-out of distribution labels, we subtract a fixed amount of 30% from the normalized probabilities of the seen labels, as described in Yin et al. 2019 and our blog post.