Views
No views yet
zero-shot-classification pipeline like so:1from transformers import AutoTokenizer, pipeline
2model = "mjwong/gte-multilingual-base-xnli-anli"
3tokenizer = AutoTokenizer.from_pretrained(model)
4classifier = pipeline("zero-shot-classification",
5 model=model,
6 tokenizer=tokenizer,
7 trust_remote_code=True
8 )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/gte-multilingual-base-xnli-anli"
8tokenizer = AutoTokenizer.from_pretrained(model_name)
9model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True)
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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| gte-multilingual-base-xnli | 0.854 | 0.767 | 0.811 | 0.798 | 0.801 | 0.820 | 0.818 | 0.753 | 0.792 | 0.719 | 0.766 | 0.769 | 0.701 | 0.799 | 0.798 |
| gte-multilingual-base-xnli-anli | 0.843 | 0.738 | 0.793 | 0.773 | 0.776 | 0.801 | 0.788 | 0.727 | 0.775 | 0.689 | 0.746 | 0.747 | 0.687 | 0.773 | 0.779 |
| Datasets | mnli_dev_m | mnli_dev_mm | anli_test_r1 | anli_test_r2 | anli_test_r3 |
|---|---|---|---|---|---|
| gte-multilingual-base-xnli | 0.852 | 0.852 | 0.295 | 0.292 | 0.336 |
| gte-multilingual-base-xnli-anli | 0.834 | 0.837 | 0.567 | 0.445 | 0.443 |