Views
No views yet
zero-shot-classification pipeline like so:1from transformers import AutoTokenizer, pipeline
2model = "mjwong/drama-base-xnli-anli"
3classifier = pipeline("zero-shot-classification",
4 model=model)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/drama-base-xnli-anli"
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 | hi | ru | sw | th | tr | ur | vi | zh |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| drama-base-xnli-anli | 0.788 | 0.689 | 0.708 | 0.715 | 0.696 | 0.732 | 0.737 | 0.647 | 0.711 | 0.636 | 0.676 | 0.664 | 0.588 | 0.708 | 0.710 |
| drama-large-xnli-anli | 0.799 | 0.698 | 0.730 | 0.721 | 0.717 | 0.754 | 0.754 | 0.649 | 0.718 | 0.652 | 0.678 | 0.656 | 0.594 | 0.719 | 0.719 |
| Datasets | mnli_dev_m | mnli_dev_mm | anli_test_r1 | anli_test_r2 | anli_test_r3 |
|---|---|---|---|---|---|
| drama-base-xnli-anli | 0.781 | 0.787 | 0.500 | 0.420 | 0.440 |
| drama-large-xnli-anli | 0.794 | 0.796 | 0.534 | 0.446 | 0.452 |