Zero-Shot Classification
ONNX
Safetensors
bart
cotran2 commited on
Commit
77fa764
1 Parent(s): 53d0347

Upload folder using huggingface_hub

Browse files
Files changed (9) hide show
  1. .gitattributes +7 -32
  2. README.md +81 -0
  3. config.json +49 -0
  4. merges.txt +0 -0
  5. model.safetensors +3 -0
  6. onnx/model.onnx +3 -0
  7. tokenizer.json +0 -0
  8. tokenizer_config.json +1 -0
  9. vocab.json +0 -0
.gitattributes CHANGED
@@ -1,35 +1,10 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
  *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
2
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
 
 
 
 
4
  *.h5 filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  *.tflite filter=lfs diff=lfs merge=lfs -text
6
+ *.tar.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.ot filter=lfs diff=lfs merge=lfs -text
8
+ *.onnx filter=lfs diff=lfs merge=lfs -text
9
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
10
+ model.safetensors filter=lfs diff=lfs merge=lfs -text
 
README.md ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ thumbnail: https://huggingface.co/front/thumbnails/facebook.png
4
+ pipeline_tag: zero-shot-classification
5
+ datasets:
6
+ - multi_nli
7
+ ---
8
+
9
+ # bart-large-mnli
10
+
11
+ 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.
12
+
13
+ Additional information about this model:
14
+ - The [bart-large](https://huggingface.co/facebook/bart-large) model page
15
+ - [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension
16
+ ](https://arxiv.org/abs/1910.13461)
17
+ - [BART fairseq implementation](https://github.com/pytorch/fairseq/tree/master/fairseq/models/bart)
18
+
19
+ ## NLI-based Zero Shot Text Classification
20
+
21
+ [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.
22
+
23
+ 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.
24
+
25
+ #### With the zero-shot classification pipeline
26
+
27
+ The model can be loaded with the `zero-shot-classification` pipeline like so:
28
+
29
+ ```python
30
+ from transformers import pipeline
31
+ classifier = pipeline("zero-shot-classification",
32
+ model="facebook/bart-large-mnli")
33
+ ```
34
+
35
+ You can then use this pipeline to classify sequences into any of the class names you specify.
36
+
37
+ ```python
38
+ sequence_to_classify = "one day I will see the world"
39
+ candidate_labels = ['travel', 'cooking', 'dancing']
40
+ classifier(sequence_to_classify, candidate_labels)
41
+ #{'labels': ['travel', 'dancing', 'cooking'],
42
+ # 'scores': [0.9938651323318481, 0.0032737774308770895, 0.002861034357920289],
43
+ # 'sequence': 'one day I will see the world'}
44
+ ```
45
+
46
+ If more than one candidate label can be correct, pass `multi_label=True` to calculate each class independently:
47
+
48
+ ```python
49
+ candidate_labels = ['travel', 'cooking', 'dancing', 'exploration']
50
+ classifier(sequence_to_classify, candidate_labels, multi_label=True)
51
+ #{'labels': ['travel', 'exploration', 'dancing', 'cooking'],
52
+ # 'scores': [0.9945111274719238,
53
+ # 0.9383890628814697,
54
+ # 0.0057061901316046715,
55
+ # 0.0018193122232332826],
56
+ # 'sequence': 'one day I will see the world'}
57
+ ```
58
+
59
+
60
+ #### With manual PyTorch
61
+
62
+ ```python
63
+ # pose sequence as a NLI premise and label as a hypothesis
64
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
65
+ nli_model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
66
+ tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
67
+
68
+ premise = sequence
69
+ hypothesis = f'This example is {label}.'
70
+
71
+ # run through model pre-trained on MNLI
72
+ x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
73
+ truncation_strategy='only_first')
74
+ logits = nli_model(x.to(device))[0]
75
+
76
+ # we throw away "neutral" (dim 1) and take the probability of
77
+ # "entailment" (2) as the probability of the label being true
78
+ entail_contradiction_logits = logits[:,[0,2]]
79
+ probs = entail_contradiction_logits.softmax(dim=1)
80
+ prob_label_is_true = probs[:,1]
81
+ ```
config.json ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_num_labels": 3,
3
+ "activation_dropout": 0.0,
4
+ "activation_function": "gelu",
5
+ "add_final_layer_norm": false,
6
+ "architectures": [
7
+ "BartForSequenceClassification"
8
+ ],
9
+ "attention_dropout": 0.0,
10
+ "bos_token_id": 0,
11
+ "classif_dropout": 0.0,
12
+ "classifier_dropout": 0.0,
13
+ "d_model": 1024,
14
+ "decoder_attention_heads": 16,
15
+ "decoder_ffn_dim": 4096,
16
+ "decoder_layerdrop": 0.0,
17
+ "decoder_layers": 12,
18
+ "decoder_start_token_id": 2,
19
+ "dropout": 0.1,
20
+ "encoder_attention_heads": 16,
21
+ "encoder_ffn_dim": 4096,
22
+ "encoder_layerdrop": 0.0,
23
+ "encoder_layers": 12,
24
+ "eos_token_id": 2,
25
+ "forced_eos_token_id": 2,
26
+ "gradient_checkpointing": false,
27
+ "id2label": {
28
+ "0": "contradiction",
29
+ "1": "neutral",
30
+ "2": "entailment"
31
+ },
32
+ "init_std": 0.02,
33
+ "is_encoder_decoder": true,
34
+ "label2id": {
35
+ "contradiction": 0,
36
+ "entailment": 2,
37
+ "neutral": 1
38
+ },
39
+ "max_position_embeddings": 1024,
40
+ "model_type": "bart",
41
+ "normalize_before": false,
42
+ "num_hidden_layers": 12,
43
+ "output_past": false,
44
+ "pad_token_id": 1,
45
+ "scale_embedding": false,
46
+ "transformers_version": "4.7.0.dev0",
47
+ "use_cache": true,
48
+ "vocab_size": 50265
49
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cfbb687dbbd9df99fe865e1860350a22aebac4d26ee4bcb50217f1df606a018e
3
+ size 1629437147
onnx/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9cbff8d0ce09d6823f6ec9c32b57005a3bee033023462ff7f7cfb5a25680ebc3
3
+ size 1630315547
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"model_max_length": 1024}
vocab.json ADDED
The diff for this file is too large to render. See raw diff