Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c21ebfc

Browse files
authored
Add English Documents for UTC (PaddlePaddle#4476)
* [UTC] Add English documents * [UTC] Add English documents * [utc] update readmes * [utc] add xclue link
1 parent c1359cb commit c21ebfc

4 files changed

Lines changed: 400 additions & 4 deletions

File tree

applications/zero_shot_text_classification/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
简体中文 | [English](README_en.md)
2+
13
# 零样本文本分类
24

35
**目录**
@@ -27,7 +29,7 @@
2729
**零样本文本分类应用亮点:**
2830

2931
- **覆盖场景全面🎓:** 覆盖文本分类各类主流任务,支持多任务训练,满足开发者多样文本分类落地需求。
30-
- **效果领先🏃:** 具有突出分类效果的UTC模型作为训练基座,提供良好的零样本和小样本学习能力。
32+
- **效果领先🏃:** 具有突出分类效果的UTC模型作为训练基座,提供良好的零样本和小样本学习能力。该模型在[ZeroCLUE](https://www.cluebenchmarks.com/zeroclue.html)[FewCLUE](https://www.cluebenchmarks.com/fewclue.html)均取得榜首(截止2023年1月11日)。
3133
- **简单易用:** 通过Taskflow实现三行代码可实现无标注数据的情况下进行快速调用,一行命令即可开启文本分类,轻松完成部署上线,降低多任务文本分类落地门槛。
3234
- **高效调优✊:** 开发者无需机器学习背景知识,即可轻松上手数据标注及模型训练流程。
3335

@@ -156,7 +158,9 @@ python -u -m paddle.distributed.launch --gpus "0,1" run_train.py \
156158
* `seed`:全局随机种子,默认为 42。
157159
* `model_name_or_path`:进行 few shot 训练使用的预训练模型。默认为 "utc-large"。
158160
* `output_dir`:必须,模型训练或压缩后保存的模型目录;默认为 `None`
159-
* `dev_path`:开发集路径;默认为 `None`
161+
* `dataset_path`:数据集文件所在目录;默认为 `./data/`
162+
* `train_file`:训练集后缀;默认为 `train.txt`
163+
* `dev_file`:开发集后缀;默认为 `dev.txt`
160164
* `max_seq_len`:文本最大切分长度,包括标签的输入超过最大长度时会对输入文本进行自动切分,标签部分不可切分,默认为512。
161165
* `per_device_train_batch_size`:用于训练的每个 GPU 核心/CPU 的batch大小,默认为8。
162166
* `per_device_eval_batch_size`:用于评估的每个 GPU 核心/CPU 的batch大小,默认为8。
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
[简体中文](README.md) | English
2+
3+
# Zero-shot Text Classification
4+
5+
**Table of contents**
6+
- [1. Zero-shot Text Classification Application](#1)
7+
- [2. Quick Start](#2)
8+
- [2.1 Code Structure](#21)
9+
- [2.2 Data Annotation](#22)
10+
- [2.3 Finetuning](#23)
11+
- [2.4 Evaluation](#24)
12+
- [2.5 Inference](#25)
13+
- [2.6 Deployment](#26)
14+
- [2.7 Experiments](#27)
15+
16+
<a name="1"></a>
17+
18+
## 1. Zero-shot Text Classification
19+
20+
This project provides an end-to-end application solution for universal text classification based on Universal Task Classification (UTC) finetuning and goes through the full lifecycle of **data labeling, model training and model deployment**. We hope this guide can help you apply Text Classification techniques with zero-shot ability in your own products or models.
21+
22+
<div align="center">
23+
<img width="700" alt="UTC模型结构图" src="https://user-images.githubusercontent.com/25607475/212268807-66181bcb-d3f9-4086-9d4a-de4d1d0933c2.png">
24+
</div>
25+
26+
Text Classification refers to assigning a set of categories to given input text. Despite the advantages of tuning, applying text classification techniques in practice remains a challenge due to domain adaption and lack of labeled data, etc. This PaddleNLP Zero-shot Text Classification Guide builds on our UTC from the Unified Semantic Matching (USM) model series and provides an industrial-level solution that supports universal text classification tasks, including but not limited to **semantic analysis, semantic matching, intention recognition and event detection**, allowing you accomplish multiple tasks with a single model. Besides, our method brings good generation performance through multi-task pretraining.
27+
28+
**Highlights:**
29+
30+
- **Comprehensive Coverage**🎓: Covers various mainstream tasks of text classification, including but not limited to semantic analysis, semantic matching, intention recognition and event detection.
31+
32+
- **State-of-the-Art Performance**🏃: Strong performance from the UTC model, which ranks first on [ZeroCLUE](https://www.cluebenchmarks.com/zeroclue.html)/[FewCLUE](https://www.cluebenchmarks.com/fewclue.html) as of 01/11/2023.
33+
34+
- **Easy to use**⚡: Three lines of code to use our Taskflow for out-of-box Zero-shot Text Classification capability. One line of command to model training and model deployment.
35+
36+
- **Efficient Tuning**✊: Developers can easily get started with the data labeling and model training process without a background in Machine Learning.
37+
38+
<a name="2"></a>
39+
40+
## 2. Quick start
41+
42+
For quick start, you can directly use ```paddlenlp.Taskflow``` out-of-the-box, leveraging the zero-shot performance. For production use cases, we recommend labeling a small amount of data for model fine-tuning to further improve the performance.
43+
44+
<a name="21"></a>
45+
46+
### 2.1 Code structure
47+
48+
```shell
49+
.
50+
├── deploy/simple_serving/ # model deployment script
51+
├── utils.py # data processing tools
52+
├── run_train.py # model fine-tuning script
53+
├── run_eval.py # model evaluation script
54+
├── label_studio.py # data format conversion script
55+
├── label_studio_text.md # data annotation instruction
56+
└── README.md
57+
```
58+
<a name="22"></a>
59+
60+
### 2.2 Data labeling
61+
62+
We recommend using [Label Studio](https://labelstud.io/) for data labeling. You can export labeled data in Label Studio and convert them into the required input format. Please refer to [Label Studio Data Labeling Guide](./label_studio_text_en.md) for more details.
63+
64+
Here we provide a pre-labeled example dataset `Medical Question Intent Classification Dataset`, which you can download with the following command. We will show how to use the data conversion script to generate training/validation/test set files for fine-tuning.
65+
66+
Download the medical question intent classification dataset:
67+
68+
```shell
69+
wget https://bj.bcebos.com/paddlenlp/datasets/utc-medical.tar.gz
70+
tar -xvf utc-medical.tar.gz
71+
mv utc-medical data
72+
rm utc-medical.tar.gz
73+
```
74+
75+
Generate training/validation set files:
76+
77+
```shell
78+
python label_studio.py \
79+
--label_studio_file ./data/label_studio.json \
80+
--save_dir ./data \
81+
--splits 0.8 0.1 0.1 \
82+
--options ./data/label.txt
83+
```
84+
85+
For multi-task training, you can convert data with script seperately and move them to the same directory.
86+
87+
<a name="23"></a>
88+
89+
### 2.3 Finetuning
90+
91+
Use the following command to fine-tune the model using `utc-large` as the pre-trained model, and save the fine-tuned model to `./checkpoint/model_best/`:
92+
93+
Single GPU:
94+
95+
```shell
96+
python run_train.py \
97+
--device gpu \
98+
--logging_steps 10 \
99+
--save_steps 100 \
100+
--eval_steps 100 \
101+
--seed 1000 \
102+
--model_name_or_path utc-large \
103+
--output_dir ./checkpoint/model_best \
104+
--dataset_path ./data/ \
105+
--max_seq_length 512 \
106+
--per_device_train_batch_size 2 \
107+
--per_device_eval_batch_size 2 \
108+
--gradient_accumulation_steps 8 \
109+
--num_train_epochs 20 \
110+
--learning_rate 1e-5 \
111+
--do_train \
112+
--do_eval \
113+
--do_export \
114+
--export_model_dir ./checkpoint/model_best \
115+
--overwrite_output_dir \
116+
--disable_tqdm True \
117+
--metric_for_best_model macro_f1 \
118+
--load_best_model_at_end True \
119+
--save_total_limit 1
120+
```
121+
122+
Multiple GPUs:
123+
124+
```shell
125+
python -u -m paddle.distributed.launch --gpus "0,1" run_train.py \
126+
--device gpu \
127+
--logging_steps 10 \
128+
--save_steps 100 \
129+
--eval_steps 100 \
130+
--seed 1000 \
131+
--model_name_or_path utc-large \
132+
--output_dir ./checkpoint/model_best \
133+
--dataset_path ./data/ \
134+
--max_seq_length 512 \
135+
--per_device_train_batch_size 2 \
136+
--per_device_eval_batch_size 2 \
137+
--gradient_accumulation_steps 8 \
138+
--num_train_epochs 20 \
139+
--learning_rate 1e-5 \
140+
--do_train \
141+
--do_eval \
142+
--do_export \
143+
--export_model_dir ./checkpoint/model_best \
144+
--overwrite_output_dir \
145+
--disable_tqdm True \
146+
--metric_for_best_model macro_f1 \
147+
--load_best_model_at_end True \
148+
--save_total_limit 1
149+
```
150+
151+
Parameters:
152+
153+
* `device`: Training device, one of 'cpu' and 'gpu' can be selected; the default is GPU training.
154+
* `logging_steps`: The interval steps of log printing during training, the default is 10.
155+
* `save_steps`: The number of interval steps to save the model checkpoint during training, the default is 100.
156+
* `eval_steps`: The number of interval steps to save the model checkpoint during training, the default is 100.
157+
* `seed`: global random seed, default is 42.
158+
* `model_name_or_path`: The pre-trained model used for few shot training. Defaults to "utc-large".
159+
* `output_dir`: Required, the model directory saved after model training or compression; the default is `None`.
160+
* `dataset_path`: The directory to dataset; defaults to `./data`.
161+
* `train_file`: Training file name; defaults to `train.txt`.
162+
* `dev_file`: Development file name; defaults to `dev.txt`.
163+
* `max_seq_len`: The maximum segmentation length of the text and label candidates. When the input exceeds the maximum length, the input text will be automatically segmented. The default is 512.
164+
* `per_device_train_batch_size`: The batch size of each GPU core/CPU used for training, the default is 8.
165+
* `per_device_eval_batch_size`: Batch size per GPU core/CPU for evaluation, default is 8.
166+
* `num_train_epochs`: Training rounds, 100 can be selected when using early stopping method; the default is 10.
167+
* `learning_rate`: The maximum learning rate for training, UTC recommends setting it to 1e-5; the default value is 3e-5.
168+
* `do_train`: Whether to perform fine-tuning training, setting this parameter means to perform fine-tuning training, and it is not set by default.
169+
* `do_eval`: Whether to evaluate, setting this parameter means to evaluate, the default is not set.
170+
* `do_export`: Whether to export, setting this parameter means to export static graph, and it is not set by default.
171+
* `export_model_dir`: Static map export address, the default is `./checkpoint/model_best`.
172+
* `overwrite_output_dir`: If `True`, overwrite the contents of the output directory. If `output_dir` points to a checkpoint directory, use it to continue training.
173+
* `disable_tqdm`: Whether to use tqdm progress bar.
174+
* `metric_for_best_model`: Optimal model metric, UTC recommends setting it to `macro_f1`, the default is None.
175+
* `load_best_model_at_end`: Whether to load the best model after training, usually used in conjunction with `metric_for_best_model`, the default is False.
176+
* `save_total_limit`: If this parameter is set, the total number of checkpoints will be limited. Remove old checkpoints `output directory`, defaults to None.
177+
178+
<a name="24"></a>
179+
180+
### 2.4 Evaluation
181+
182+
Model evaluation:
183+
184+
```shell
185+
python evaluate.py \
186+
--model_path ./checkpoint/model_best \
187+
--test_path ./data/test.txt \
188+
--per_device_eval_batch_size 2 \
189+
--max_seq_len 512 \
190+
--output_dir ./checkpoint_test
191+
```
192+
193+
Parameters:
194+
195+
- `model_path`: The path of the model folder for evaluation, which must contain the model weight file `model_state.pdparams` and the configuration file `model_config.json`.
196+
- `test_path`: The test set file for evaluation.
197+
- `per_device_eval_batch_size`: Batch size, please adjust it according to the machine situation, the default is 8.
198+
- `max_seq_len`: The maximum segmentation length of the text and label candidates. When the input exceeds the maximum length, the input text will be automatically segmented. The default is 512.
199+
200+
<a name="25"></a>
201+
202+
### 2.5 Inference
203+
204+
You can use `paddlenlp.Taskflow` to load your custom model by specifying the path of the model weight file through `task_path`.
205+
206+
```python
207+
>>> from pprint import pprint
208+
>>> from paddlenlp import Taskflow
209+
>>> schema = ["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议", "疾病表述", "后果表述", "注意事项", "功效作用", "医疗费用", "其他"]
210+
>>> my_cls = Taskflow("zero_shot_text_classification", schema=schema, task_path='./checkpoint/model_best', precision="fp16")
211+
>>> pprint(my_cls("中性粒细胞比率偏低"))
212+
```
213+
214+
<a name="26"></a>
215+
216+
### 2.6 Deployment
217+
218+
We provide the deployment solution on the foundation of PaddleNLP SimpleServing, where you can easily build your own deployment service with three-line code.
219+
220+
```
221+
# Save at server.py
222+
from paddlenlp import SimpleServer, Taskflow
223+
224+
schema = ["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议"]
225+
utc = Taskflow("zero_shot_text_classification",
226+
schema=schema,
227+
task_path="../../checkpoint/model_best/",
228+
precision="fp32")
229+
app = SimpleServer()
230+
app.register_taskflow("taskflow/utc", utc)
231+
```
232+
233+
```
234+
# Start the server
235+
paddlenlp server server:app --host 0.0.0.0 --port 8990
236+
```
237+
238+
It supports FP16 (half-precision) and multiple process for inference acceleration.
239+
240+
<a name="27"></a>
241+
242+
### 2.7 Experiments
243+
244+
The results reported here are based on the development set of KUAKE-QIC.
245+
246+
| | Accuracy | Micro F1 | Macro F1 |
247+
| :------: | :--------: | :--------: | :--------: |
248+
| 0-shot | 28.69 | 87.03 | 60.90 |
249+
| 5-shot | 64.75 | 93.34 | 80.33 |
250+
| 10-shot | 65.88 | 93.76 | 81.34 |
251+
| full-set | 81.81 | 96.65 | 89.87 |
252+
253+
where k-shot means that there are k annotated samples per label for training.

applications/zero_shot_text_classification/label_studio_text.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
简体中文 | [English](label_studio_text_en.md)
2+
13
# 文本分类任务Label Studio使用指南
24

35
**目录**
@@ -105,7 +107,7 @@ label-studio start
105107

106108
将导出的文件重命名为``label_studio.json``后,放入``./data``目录下。通过[label_studio.py](./label_studio.py)脚本可转为UTC的数据格式。
107109

108-
在数据转换阶段,我们会自动构造用于模型训练的标签候选信息。例如在医疗意图分类中,标签候选为``["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议", "疾病表述", "后果表述", "注意事项", "功效作用", "医疗费用", "其他"]``可通过``options``参数进行配置
110+
在数据转换阶段,还需要提供标签候选信息,放在`./data/label.txt`文件中,每个标签占一行。例如在医疗意图分类中,标签候选为``["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议", "疾病表述", "后果表述", "注意事项", "功效作用", "医疗费用", "其他"]``也可通过``options``参数直接进行配置
109111

110112
```shell
111113
python label_studio.py \
@@ -122,7 +124,7 @@ python label_studio.py \
122124
- ``label_studio_file``: 从label studio导出的数据标注文件。
123125
- ``save_dir``: 训练数据的保存目录,默认存储在``data``目录下。
124126
- ``splits``: 划分数据集时训练集、验证集所占的比例。默认为[0.8, 0.1, 0.1]表示按照``8:1:1``的比例将数据划分为训练集、验证集和测试集。
125-
- ``options``: 指定分类任务的类别标签。若输入类型为文件,则文件中每行一个标签。默认为None,自动从输入数据中构造标签候选集合,当数据量大时耗时较长。
127+
- ``options``: 指定分类任务的类别标签。若输入类型为文件,则文件中每行一个标签。
126128
- ``is_shuffle``: 是否对数据集进行随机打散,默认为True。
127129
- ``seed``: 随机种子,默认为1000.
128130

0 commit comments

Comments
 (0)