README.md
| 1 | --- |
| 2 | language: en |
| 3 | pipeline_tag: zero-shot-classification |
| 4 | tags: |
| 5 | - transformers |
| 6 | datasets: |
| 7 | - nyu-mll/multi_nli |
| 8 | - stanfordnlp/snli |
| 9 | metrics: |
| 10 | - accuracy |
| 11 | license: apache-2.0 |
| 12 | base_model: |
| 13 | - microsoft/deberta-v3-base |
| 14 | library_name: sentence-transformers |
| 15 | --- |
| 16 | |
| 17 | # Cross-Encoder for Natural Language Inference |
| 18 | This model was trained using [SentenceTransformers](https://sbert.net) [Cross-Encoder](https://www.sbert.net/examples/applications/cross-encoder/README.html) class. This model is based on [microsoft/deberta-v3-base](https://huggingface.co/microsoft/deberta-v3-base) |
| 19 | |
| 20 | ## Training Data |
| 21 | The model was trained on the [SNLI](https://nlp.stanford.edu/projects/snli/) and [MultiNLI](https://cims.nyu.edu/~sbowman/multinli/) datasets. For a given sentence pair, it will output three scores corresponding to the labels: contradiction, entailment, neutral. |
| 22 | |
| 23 | ## Performance |
| 24 | - Accuracy on SNLI-test dataset: 92.38 |
| 25 | - Accuracy on MNLI mismatched set: 90.04 |
| 26 | |
| 27 | For futher evaluation results, see [SBERT.net - Pretrained Cross-Encoder](https://www.sbert.net/docs/pretrained_cross-encoders.html#nli). |
| 28 | |
| 29 | ## Usage |
| 30 | |
| 31 | Pre-trained models can be used like this: |
| 32 | ```python |
| 33 | from sentence_transformers import CrossEncoder |
| 34 | model = CrossEncoder('cross-encoder/nli-deberta-v3-base') |
| 35 | scores = model.predict([('A man is eating pizza', 'A man eats something'), ('A black race car starts up in front of a crowd of people.', 'A man is driving down a lonely road.')]) |
| 36 | |
| 37 | #Convert scores to labels |
| 38 | label_mapping = ['contradiction', 'entailment', 'neutral'] |
| 39 | labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)] |
| 40 | ``` |
| 41 | |
| 42 | ## Usage with Transformers AutoModel |
| 43 | You can use the model also directly with Transformers library (without SentenceTransformers library): |
| 44 | ```python |
| 45 | from transformers import AutoTokenizer, AutoModelForSequenceClassification |
| 46 | import torch |
| 47 | |
| 48 | model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-deberta-v3-base') |
| 49 | tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-v3-base') |
| 50 | |
| 51 | features = tokenizer(['A man is eating pizza', 'A black race car starts up in front of a crowd of people.'], ['A man eats something', 'A man is driving down a lonely road.'], padding=True, truncation=True, return_tensors="pt") |
| 52 | |
| 53 | model.eval() |
| 54 | with torch.no_grad(): |
| 55 | scores = model(**features).logits |
| 56 | label_mapping = ['contradiction', 'entailment', 'neutral'] |
| 57 | labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)] |
| 58 | print(labels) |
| 59 | ``` |
| 60 | |
| 61 | ## Zero-Shot Classification |
| 62 | This model can also be used for zero-shot-classification: |
| 63 | ```python |
| 64 | from transformers import pipeline |
| 65 | |
| 66 | classifier = pipeline("zero-shot-classification", model='cross-encoder/nli-deberta-v3-base') |
| 67 | |
| 68 | sent = "Apple just announced the newest iPhone X" |
| 69 | candidate_labels = ["technology", "sports", "politics"] |
| 70 | res = classifier(sent, candidate_labels) |
| 71 | print(res) |
| 72 | ``` |