masato12 commited on
Commit
9f172a9
1 Parent(s): 330ff1b

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +91 -0
README.md ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - multi_nli
4
+ library_name: Transformers PHP
5
+ license: mit
6
+ pipeline_tag: zero-shot-classification
7
+ tags:
8
+ - onnx
9
+ thumbnail: https://huggingface.co/front/thumbnails/facebook.png
10
+ ---
11
+
12
+ https://huggingface.co/facebook/bart-large-mnli with ONNX weights to be compatible with Transformers PHP
13
+
14
+
15
+ # bart-large-mnli
16
+
17
+ This is the checkpoint for [bart-large](https://huggingface.co/facebook/bart-large) after being trained on the [MultiNLI (MNLI)](https://huggingface.co/datasets/multi_nli) dataset.
18
+
19
+ Additional information about this model:
20
+ - The [bart-large](https://huggingface.co/facebook/bart-large) model page
21
+ - [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension
22
+ ](https://arxiv.org/abs/1910.13461)
23
+ - [BART fairseq implementation](https://github.com/pytorch/fairseq/tree/master/fairseq/models/bart)
24
+
25
+ ## NLI-based Zero Shot Text Classification
26
+
27
+ [Yin et al.](https://arxiv.org/abs/1909.00161) proposed a method for using pre-trained NLI models as a ready-made zero-shot sequence classifiers. The method works by posing the sequence to be classified as the NLI premise and to construct a hypothesis from each candidate label. For example, if we want to evaluate whether a sequence belongs to the class "politics", we could construct a hypothesis of `This text is about politics.`. The probabilities for entailment and contradiction are then converted to label probabilities.
28
+
29
+ This method is surprisingly effective in many cases, particularly when used with larger pre-trained models like BART and Roberta. See [this blog post](https://joeddav.github.io/blog/2020/05/29/ZSL.html) for a more expansive introduction to this and other zero shot methods, and see the code snippets below for examples of using this model for zero-shot classification both with Hugging Face's built-in pipeline and with native Transformers/PyTorch code.
30
+
31
+ #### With the zero-shot classification pipeline
32
+
33
+ The model can be loaded with the `zero-shot-classification` pipeline like so:
34
+
35
+ ```python
36
+ from transformers import pipeline
37
+ classifier = pipeline("zero-shot-classification",
38
+ model="facebook/bart-large-mnli")
39
+ ```
40
+
41
+ You can then use this pipeline to classify sequences into any of the class names you specify.
42
+
43
+ ```python
44
+ sequence_to_classify = "one day I will see the world"
45
+ candidate_labels = ['travel', 'cooking', 'dancing']
46
+ classifier(sequence_to_classify, candidate_labels)
47
+ #{'labels': ['travel', 'dancing', 'cooking'],
48
+ # 'scores': [0.9938651323318481, 0.0032737774308770895, 0.002861034357920289],
49
+ # 'sequence': 'one day I will see the world'}
50
+ ```
51
+
52
+ If more than one candidate label can be correct, pass `multi_label=True` to calculate each class independently:
53
+
54
+ ```python
55
+ candidate_labels = ['travel', 'cooking', 'dancing', 'exploration']
56
+ classifier(sequence_to_classify, candidate_labels, multi_label=True)
57
+ #{'labels': ['travel', 'exploration', 'dancing', 'cooking'],
58
+ # 'scores': [0.9945111274719238,
59
+ # 0.9383890628814697,
60
+ # 0.0057061901316046715,
61
+ # 0.0018193122232332826],
62
+ # 'sequence': 'one day I will see the world'}
63
+ ```
64
+
65
+
66
+ #### With manual PyTorch
67
+
68
+ ```python
69
+ # pose sequence as a NLI premise and label as a hypothesis
70
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
71
+ nli_model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
72
+ tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
73
+
74
+ premise = sequence
75
+ hypothesis = f'This example is {label}.'
76
+
77
+ # run through model pre-trained on MNLI
78
+ x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
79
+ truncation_strategy='only_first')
80
+ logits = nli_model(x.to(device))[0]
81
+
82
+ # we throw away "neutral" (dim 1) and take the probability of
83
+ # "entailment" (2) as the probability of the label being true
84
+ entail_contradiction_logits = logits[:,[0,2]]
85
+ probs = entail_contradiction_logits.softmax(dim=1)
86
+ prob_label_is_true = probs[:,1]
87
+ ```
88
+
89
+ ---
90
+
91
+ Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [🤗 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).