README.md
2.6 KB · 69 lines · markdown Raw
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 - nreimers/MiniLMv2-L6-H768-distilled-from-RoBERTa-Large
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.
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 For evaluation results, see [SBERT.net - Pretrained Cross-Encoder](https://www.sbert.net/docs/pretrained_cross-encoders.html#nli).
25
26 ## Usage
27
28 Pre-trained models can be used like this:
29 ```python
30 from sentence_transformers import CrossEncoder
31 model = CrossEncoder('cross-encoder/nli-MiniLM2-L6-H768')
32 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.')])
33
34 #Convert scores to labels
35 label_mapping = ['contradiction', 'entailment', 'neutral']
36 labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]
37 ```
38
39 ## Usage with Transformers AutoModel
40 You can use the model also directly with Transformers library (without SentenceTransformers library):
41 ```python
42 from transformers import AutoTokenizer, AutoModelForSequenceClassification
43 import torch
44
45 model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-MiniLM2-L6-H768')
46 tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-MiniLM2-L6-H768')
47
48 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")
49
50 model.eval()
51 with torch.no_grad():
52 scores = model(**features).logits
53 label_mapping = ['contradiction', 'entailment', 'neutral']
54 labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)]
55 print(labels)
56 ```
57
58 ## Zero-Shot Classification
59 This model can also be used for zero-shot-classification:
60 ```python
61 from transformers import pipeline
62
63 classifier = pipeline("zero-shot-classification", model='cross-encoder/nli-MiniLM2-L6-H768')
64
65 sent = "Apple just announced the newest iPhone X"
66 candidate_labels = ["technology", "sports", "politics"]
67 res = classifier(sent, candidate_labels)
68 print(res)
69 ```