File size: 3,869 Bytes
e2f42ca
5b5d21e
 
c1f8b59
 
 
 
 
 
 
 
 
 
 
 
 
5b5d21e
e2f42ca
c1f8b59
 
 
5b5d21e
c1f8b59
 
 
 
 
 
e2f42ca
5b5d21e
 
 
 
 
 
 
 
 
 
 
 
7098216
 
5b5d21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7098216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b5d21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
language:
- multilingual
- en
- ar
- bg
- de
- el
- es
- fr
- ru
- sw
- th
- tr
- ur
- vi
- zh
license: mit
datasets:
- xnli
pipeline_tag: zero-shot-classification
widget:
- text: Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU
  candidate_labels: politics, economy, entertainment, environment
base_model: facebook/mcontriever
model-index:
- name: mcontriever-xnli
  results: []
---

# mcontriever-xnli

This model is a fine-tuned version of [facebook/mcontriever](https://huggingface.co/facebook/mcontriever) on the XNLI dataset.

## Model description

[Unsupervised Dense Information Retrieval with Contrastive Learning](https://arxiv.org/abs/2112.09118).
Gautier Izacard, Mathilde Caron, Lucas Hosseini, Sebastian Riedel, Piotr Bojanowski, Armand Joulin, Edouard Grave, arXiv 2021

## How to use the model

### With the zero-shot classification pipeline

The model can be loaded with the `zero-shot-classification` pipeline like so:

```python
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
                      model="mjwong/mcontriever-xnli")
```

You can then use this pipeline to classify sequences into any of the class names you specify.

```python
sequence_to_classify = "Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU"
candidate_labels = ["politics", "economy", "entertainment", "environment"]
classifier(sequence_to_classify, candidate_labels)
```

If more than one candidate label can be correct, pass `multi_class=True` to calculate each class independently:

```python
candidate_labels = ["politics", "economy", "entertainment", "environment"]
classifier(sequence_to_classify, candidate_labels, multi_label=True)
```

### With manual PyTorch

The model can also be applied on NLI tasks like so:

```python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# device = "cuda:0" or "cpu"
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

model_name = "mjwong/mcontriever-xnli"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

premise = "But I thought you'd sworn off coffee."
hypothesis = "I thought that you vowed to drink more coffee."

input = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")
output = model(input["input_ids"].to(device))
prediction = torch.softmax(output["logits"][0], -1).tolist()
label_names = ["entailment", "neutral", "contradiction"]
prediction = {name: round(float(pred) * 100, 2) for pred, name in zip(prediction, label_names)}
print(prediction)
```

### Eval results
The model was evaluated using the XNLI test sets on 14 languages: English (en), Arabic (ar), Bulgarian (bg), German (de), Greek (el), Spanish (es), French (fr), Russian (ru), Swahili (sw), Thai (th), Turkish (tr), Urdu (ur), Vietnam (vi) and Chinese (zh). The metric used is accuracy.

|Datasets|en|ar|bg|de|el|es|fr|ru|sw|th|tr|ur|vi|zh|
| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|[mcontriever-xnli](https://huggingface.co/mjwong/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](https://huggingface.co/mjwong/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|

### Training hyperparameters

The following hyperparameters were used during training:

- learning_rate: 2e-05
- train_batch_size: 16
- eval_batch_size: 16
- seed: 42
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
- lr_scheduler_type: linear
- lr_scheduler_warmup_ratio: 0.1
- num_epochs: 2

### Framework versions
- Transformers 4.28.1
- Pytorch 1.12.1+cu116
- Datasets 2.11.0
- Tokenizers 0.12.1