Views
No views yet
This text is about politics.. The probabilities for entailment and contradiction are then converted to label probabilities.zero-shot-classification pipeline like so:1from transformers import pipeline
2classifier = pipeline("zero-shot-classification",
3 model="facebook/bart-large-mnli")1sequence_to_classify = "one day I will see the world"
2candidate_labels = ['travel', 'cooking', 'dancing']
3classifier(sequence_to_classify, candidate_labels)
4#{'labels': ['travel', 'dancing', 'cooking'],
5# 'scores': [0.9938651323318481, 0.0032737774308770895, 0.002861034357920289],
6# 'sequence': 'one day I will see the world'}multi_label=True to calculate each class independently:1candidate_labels = ['travel', 'cooking', 'dancing', 'exploration']
2classifier(sequence_to_classify, candidate_labels, multi_label=True)
3#{'labels': ['travel', 'exploration', 'dancing', 'cooking'],
4# 'scores': [0.9945111274719238,
5# 0.9383890628814697,
6# 0.0057061901316046715,
7# 0.0018193122232332826],
8# 'sequence': 'one day I will see the world'}1# pose sequence as a NLI premise and label as a hypothesis
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3nli_model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
4tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
5
6premise = sequence
7hypothesis = f'This example is {label}.'
8
9# run through model pre-trained on MNLI
10x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
11 truncation_strategy='only_first')
12logits = nli_model(x.to(device))[0]
13
14# we throw away "neutral" (dim 1) and take the probability of
15# "entailment" (2) as the probability of the label being true
16entail_contradiction_logits = logits[:,[0,2]]
17probs = entail_contradiction_logits.softmax(dim=1)
18prob_label_is_true = probs[:,1]