Views
No views yet
zero-shot-classification pipeline like so:1from transformers import pipeline
2classifier = pipeline("zero-shot-classification",
3 model="mjwong/mcontriever-xnli")1sequence_to_classify = "Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU"
2candidate_labels = ["politics", "economy", "entertainment", "environment"]
3classifier(sequence_to_classify, candidate_labels)multi_class=True to calculate each class independently:1candidate_labels = ["politics", "economy", "entertainment", "environment"]
2classifier(sequence_to_classify, candidate_labels, multi_label=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/mcontriever-xnli"
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 | en | ar | bg | de | el | es | fr | ru | sw | th | tr | ur | vi | zh |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| mcontriever-xnli | 0.820 | 0.733 | 0.773 | 0.774 | 0.748 | 0.788 | 0.781 | 0.755 | 0.690 | 0.690 | 0.741 | 0.647 | 0.766 | 0.767 |
| mcontriever-msmarco-xnli | 0.822 | 0.731 | 0.763 | 0.775 | 0.752 | 0.785 | 0.778 | 0.749 | 0.694 | 0.682 | 0.738 | 0.641 | 0.759 | 0.768 |