Update README.md
Browse files
README.md
CHANGED
@@ -4,5 +4,73 @@ pipeline_tag: zero-shot-classification
|
|
4 |
tags:
|
5 |
- code
|
6 |
---
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
tags:
|
5 |
- code
|
6 |
---
|
7 |
+
# Zero-shot text classification (base-sized model) trained with self-supervised tuning
|
8 |
|
9 |
+
Zero-shot text classification model trained with self-supervised tuning (SSTuning).
|
10 |
+
It was introduced in the paper Zero-Shot Text Classification via Self-Supervised Tuning.
|
11 |
+
|
12 |
+
The model backbone is RoBERTa-base.
|
13 |
+
|
14 |
+
## Model description
|
15 |
+
The model is tuned with unlabeled data using a learning objective called first sentence prediction (FSP).
|
16 |
+
The FSP task is designed by considering both the nature of the unlabeled corpus and the input/output format of classification tasks.
|
17 |
+
The training and validation sets are constructed from the unlabeled corpus using FSP. During tuning, BERT-like pre-trained masked language
|
18 |
+
models such as RoBERTa and ALBERT are employed as the backbone, and an output layer for classification is added.
|
19 |
+
The learning objective for FSP is to predict the index of the positive option.
|
20 |
+
A cross-entropy loss is used for tuning the model.
|
21 |
+
|
22 |
+
## Intended uses & limitations
|
23 |
+
The model can be used for zero-shot text classification such sentiment analysis and topic classificaion. No further finetuning is needed.
|
24 |
+
|
25 |
+
The number of labels should be 2 ~ 20.
|
26 |
+
|
27 |
+
### How to use
|
28 |
+
You can try the model with the colab [notebook](https://colab.research.google.com/drive/17bqc8cXFF-wDmZ0o8j7sbrQB9Cq7Gowr?usp=sharing).
|
29 |
+
|
30 |
+
```python
|
31 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
32 |
+
import torch, string, random
|
33 |
+
|
34 |
+
tokenizer = AutoTokenizer.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-base")
|
35 |
+
model = AutoModelForSequenceClassification.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-base")
|
36 |
+
|
37 |
+
text = "I love this place! The food is always so fresh and delicious."
|
38 |
+
|
39 |
+
list_label = ["negative","positve"]
|
40 |
+
|
41 |
+
def add_prefix(text, list_label, label_num = 20, shuffle = False):
|
42 |
+
list_label = [x+'.' if x[-1] != '.' else x for x in list_label]
|
43 |
+
list_label_new = list_label + [tokenizer.pad_token]* (label_num - len(list_label))
|
44 |
+
if shuffle:
|
45 |
+
random.shuffle(list_label_new)
|
46 |
+
s_option = ' '.join(['('+list_ABC[i]+') '+list_label_new[i] for i in range(len(list_label_new))])
|
47 |
+
return f'{s_option} {tokenizer.sep_token} {text}', list_label_new
|
48 |
+
|
49 |
+
text_new, list_label_new = add_prefix(text,list_label,shuffle=False)
|
50 |
+
|
51 |
+
ids = tokenizer.encode(text_new)
|
52 |
+
tokens = tokenizer.convert_ids_to_tokens(ids)
|
53 |
+
encoding = tokenizer([text],truncation=True, padding='max_length',max_length=512)
|
54 |
+
item = {key: torch.tensor(val) for key, val in encoding.items()}
|
55 |
+
logits = model(**item).logits
|
56 |
+
probs = torch.nn.functional.softmax(logits, dim = -1).tolist()
|
57 |
+
predictions = torch.argmax(logits, dim=-1)
|
58 |
+
```
|
59 |
+
|
60 |
+
|
61 |
+
### BibTeX entry and citation info
|
62 |
+
```bibtxt
|
63 |
+
@inproceedings{acl23/SSTuning,
|
64 |
+
author = {Chaoqun Liu and
|
65 |
+
Wenxuan Zhang and
|
66 |
+
Guizhen Chen and
|
67 |
+
Xiaobao Wu and
|
68 |
+
Anh Tuan Luu and
|
69 |
+
Chip Hong Chang and
|
70 |
+
Lidong Bing},
|
71 |
+
title = {Zero-Shot Text Classification via Self-Supervised Tuning},
|
72 |
+
booktitle={Findings of the 2023 ACL},
|
73 |
+
year = {2023},
|
74 |
+
url = {},
|
75 |
+
}
|
76 |
+
```
|