Skip to content

Commit 56fc422

Browse files
authored
Add day12 homework tutorial (#581)
* add day12 homework tutorial * transform to relative path
1 parent ac9dbeb commit 56fc422

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

education/day12.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Day12 预训练模型小型化与部署实战作业辅导
2+
3+
本教程旨在辅导同学如何完成 AI Studio课程——[『NLP打卡营』实践课12:预训练模型小型化与部署实战
4+
](https://aistudio.baidu.com/aistudio/projectdetail/1920541)课后作业。
5+
6+
## 1. 对ERNIE-Gram进行fine-tuning得到教师模型
7+
由于我们的蒸馏是在中文情感分析ChnSentiCorp任务上,因此我们需要对PaddleNLP提供的ERNIE-Gram在我们的任务上进行Fine-tuning。下面是详细的步骤:
8+
9+
[PaddleNLP Transformer API](../docs/model_zoo/transformers.rst)查询PaddleNLP所支持的Transformer预训练模型。我们可以在这里找到ERNIE-Gram中的**ernie-gram-zh**
10+
参考AI studio教程中在中文情感分类ChnSentiCorp数据集下对**ERNIE-1.0**进行fine-tuning的方法,即对[PaddleNLP的run_glue脚本](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/benchmark/glue)进行适当修改,使其支持ERNIE-Gram在中文情感分类数据上的fine-tuning。
11+
12+
我们需要导入ERNIE-Gram模型所依赖的相关模块:
13+
14+
```python
15+
from paddlenlp.transformers import ErnieGramForSequenceClassification, ErnieGramTokenizer
16+
```
17+
18+
### a.对现有的run_glue.py脚本修改使其支持ChnSentiCorp任务
19+
20+
增加对ChnSentiCorp数据集的评估指标的配置
21+
22+
```python
23+
METRIC_CLASSES = {
24+
"chnsenticorp": Accuracy,
25+
}
26+
```
27+
28+
增加对使用的预训练模型的配置
29+
30+
```python
31+
MODEL_CLASSES = {
32+
"ernie-gram": (ErnieGramForSequenceClassification, ErnieGramTokenizer),
33+
}
34+
```
35+
36+
获取数据集的调用需要更新为:
37+
38+
```python
39+
train_ds = load_dataset('chnsenticorp', splits='train')
40+
dev_ds = load_dataset('chnsenticorp', splits='dev')
41+
```
42+
43+
接着需要更新`convert_example`函数:
44+
45+
```python
46+
def convert_example(example,
47+
tokenizer,
48+
label_list,
49+
max_seq_length=512,
50+
is_test=False):
51+
"""convert a glue example into necessary features"""
52+
if not is_test:
53+
# `label_list == None` is for regression task
54+
label_dtype = "int64" if label_list else "float32"
55+
# Get the label
56+
label = example['label']
57+
label = np.array([label], dtype=label_dtype)
58+
# Convert raw text to feature
59+
example = tokenizer(example['text'], max_seq_len=max_seq_length)
60+
61+
if not is_test:
62+
return example['input_ids'], example['token_type_ids'], label
63+
else:
64+
return example['input_ids'], example['token_type_ids']
65+
```
66+
67+
### b.在ChnSentiCorp上对ERNIE-Gram进行fine-tuning
68+
69+
现在,我们就可以对ERNIE-Gram模型在ChnSentiCorp数据集上进行finetuning了~
70+
71+
可以使用下面的命令对ERNIE的预训练模型进行finetuning:
72+
73+
```shell
74+
75+
export TASK_NAME=ChnSentiCorp
76+
77+
python -u ./run_glue.py \
78+
--model_type ernie-gram \
79+
--model_name_or_path ernie-gram-zh \
80+
--task_name $TASK_NAME \
81+
--max_seq_length 128 \
82+
--batch_size 24 \
83+
--learning_rate 5e-5 \
84+
--num_train_epochs 3 \
85+
--logging_steps 10 \
86+
--save_steps 10 \
87+
--output_dir ./tmp/$TASK_NAME/ \
88+
--device gpu # or cpu
89+
90+
```
91+
92+
## 2.用Fine-tuned ERNIE-Gram对Bi-LSTM进行蒸馏
93+
94+
由于本节的作业只需要替换蒸馏时使用的教师模型,因此我们只需要在蒸馏前重新导入第一步微调得到的教师模型即可。
95+
96+
假设我们对ERNIE-Gram Fine-tuning得到的最好的模型位于./tmp/ChnSentiCorp/best_model,那么下面的脚本可以导入教师模型:
97+
```python
98+
from paddlenlp.transformers import ErnieGramForSequenceClassification, ErnieGramTokenizer
99+
teacher = ErnieGramForSequenceClassification.from_pretrained("./tmp/ChnSentiCorp/best_model")
100+
```
101+
102+
蒸馏的过程同AI studio教程,这里就不再赘述啦~同学们按着与教程相同的步骤进行即可。

0 commit comments

Comments
 (0)