Views
No views yet
zero-shot-classification pipeline like so:1from transformers import pipeline
2classifier = pipeline("zero-shot-classification",
3 model="mjwong/contriever-mnli")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
4# device = "cuda:0" or "cpu"
5device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
6
7model_name = "mjwong/contriever-mnli"
8tokenizer = AutoTokenizer.from_pretrained(model_name)
9model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
11premise = "But I thought you'd sworn off coffee."
12hypothesis = "I thought that you vowed to drink more coffee."
13
14input = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")
15output = model(input["input_ids"].to(device))
16prediction = torch.softmax(output["logits"][0], -1).tolist()
17label_names = ["entailment", "neutral", "contradiction"]
18prediction = {name: round(float(pred) * 100, 2) for pred, name in zip(prediction, label_names)}
19print(prediction)| Datasets | mnli_dev_m | mnli_dev_mm | anli_test_r1 | anli_test_r2 | anli_test_r3 |
|---|---|---|---|---|---|
| contriever-mnli | 0.821 | 0.822 | 0.247 | 0.281 | 0.312 |
| contriever-msmarco-mnli | 0.820 | 0.819 | 0.244 | 0.296 | 0.306 |