Views
No views yet
zero-shot-classification pipeline like so:1from transformers import pipeline
2classifier = pipeline("zero-shot-classification",
3 model="mjwong/bge-large-en-mnli-anli")1sequence_to_classify = "one day I will see the world"
2candidate_labels = ['travel', 'cooking', 'dancing']
3classifier(sequence_to_classify, candidate_labels)multi_class=True to calculate each class independently:1candidate_labels = ['travel', 'cooking', 'dancing', 'exploration']
2classifier(sequence_to_classify, candidate_labels, multi_class=True)1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3# device = "cuda:0" or "cpu"
4device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
5model_name = "mjwong/bge-large-en-mnli-anli"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8premise = "But I thought you'd sworn off coffee."
9hypothesis = "I thought that you vowed to drink more coffee."
10input = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")
11output = model(input["input_ids"].to(device))
12prediction = torch.softmax(output["logits"][0], -1).tolist()
13label_names = ["entailment", "neutral", "contradiction"]
14prediction = {name: round(float(pred) * 100, 2) for pred, name in zip(prediction, label_names)}
15print(prediction)| Datasets | mnli_dev_m | mnli_dev_mm | anli_test_r1 | anli_test_r2 | anli_test_r3 |
|---|---|---|---|---|---|
| bge-large-en-mnli-anli | 0.846 | 0.842 | 0.602 | 0.451 | 0.452 |