Skip to content

Commit 03f0f02

Browse files
committed
add PET
1 parent 14e2234 commit 03f0f02

File tree

16 files changed

+2394
-2
lines changed

16 files changed

+2394
-2
lines changed

examples/few_shot/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ Few-Shot Learning 旨在研究如何从少量有监督的训练样本中学习
1212
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |------------ | ------------ | ---------- |
1313
| P-tuning | ERNIE1.0 | 55.70 | 83.28 | 63.43 | 35.36 | 60.54 | 50.02 | 54.51 | 50.14 | 54.93 | 41.16 |
1414
| EFL | ERNIE1.0 | 54.47 | 84.10 | 60.10 | 35.12 | 56.61 | 56.57 | 53.59 | 46.37 | 61.21 | 36.56 |
15-
15+
| PET | ERNIE1.0 | 56.38 | 86.88 | 61.90 | 36.90 | 61.10 | 56.51 | 55.02 | 50.31 | 59.72 | 39.11 |
1616
## 策略库
1717
- [P-tuning](./p-tuning)
1818
- [EFL](./efl)
19-
- PET(Todo)
19+
- [PET](./pet)
2020

2121
## References
2222
[1]X. Liu et al., “GPT Understands, Too,” arXiv:2103.10385 [cs], Mar. 2021, Accessed: Mar. 22, 2021. [Online]. Available: http://arxiv.org/abs/2103.10385
2323
[2] Wang, Sinong, Han Fang, Madian Khabsa, Hanzi Mao, and Hao Ma. “Entailment as Few-Shot Learner.” ArXiv:2104.14690 [Cs], April 29, 2021. http://arxiv.org/abs/2104.14690.
24+
[3] Wang, S., Fang, H., Khabsa, M., Mao, H., and Ma, H., “Entailment as Few-Shot Learner”, ArXiv:2001.07676 [Cs], 2021. https://arxiv.org/abs/2001.07676

examples/few_shot/pet/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [PET](https://arxiv.org/abs/2001.07676)
2+
3+
[PET](https://arxiv.org/abs/2001.07676) (Exploiting Cloze Questions for Few Shot Text Classification and Natural Language Inference) 提出将输入示例转换为完形填空式短语,以帮助语言模型理解给定的任务
4+
5+
## 代码结构及说明
6+
```
7+
|—— pet.py # PET 策略的训练、评估主脚本
8+
|—— dataset.py # PET 策略针对 FewCLUE 9 个数据集的任务转换逻辑,以及明文 -> 训练数据的转换
9+
|—— model.py # PET 的网络结构
10+
|—— evaluate.py # 针对 FewCLUE 9 个数据集的评估函数
11+
|—— predict.py # 针对 FewCLUE 9 个数据集进行预测
12+
```
13+
14+
15+
## 基于 FewCLUE 进行 PET 实验
16+
PaddleNLP 内置了 FewCLUE 数据集,可以直接用来进行 PET 策略训练、评估、预测,并生成 FewCLUE 榜单的提交结果,参与 FewCLUE 竞赛。
17+
18+
### 数据准备
19+
基于 FewCLUE 数据集进行实验只需要 1 行代码,这部分代码在 `pet.py` 脚本中
20+
21+
```
22+
from paddlenlp.datasets import load_dataset
23+
24+
# 通过指定 "fewclue" 和数据集名字 name="tnews" 即可一键加载 FewCLUE 中的 tnews 数据集
25+
train_ds, dev_ds, public_test_ds = load_dataset("fewclue", name="tnews", splits=("train_0", "dev_0", "test_public"))
26+
````
27+
### 模型训练&评估
28+
通过如下命令,指定 GPU 0 卡, 在 FewCLUE 的 `tnews` 数据集上进行训练&评估
29+
```
30+
#task_name="iflytek"
31+
task_name="tnews"
32+
#task_name="eprstmt"
33+
#task_name="bustm"
34+
#task_name="ocnli"
35+
#task_name="csl"
36+
#task_name="csldcp"
37+
#task_name="cluewsc"
38+
#task_name="chid"
39+
python -u -m paddle.distributed.launch --gpus "0" \
40+
pet.py \
41+
--task_name ${task_name} \
42+
--device gpu \
43+
--pattern_id 0 \
44+
--save_dir ./${task_name} \
45+
--index 0 \
46+
--batch_size 16 \
47+
--learning_rate 1E-4 \
48+
--epochs 10 \
49+
--max_seq_length 512 \
50+
--language_model "ernie-1.0" \
51+
```
52+
参数含义说明
53+
- `task_name`: FewCLUE 中的数据集名字
54+
- `device`: 使用 cpu/gpu 进行训练
55+
- `pattern_id` 完形填空的模式
56+
- `save_dir`: 模型存储路径
57+
- `max_seq_length`: 文本的最大截断长度
58+
59+
模型每训练 1 个 epoch, 会在验证集上进行评估
60+
61+
### 模型预测
62+
通过如下命令,指定 GPU 0 卡, 在 `FewCLUE` 的 `iflytek` 数据集上进行预测
63+
```
64+
#task_name="iflytek"
65+
task_name="tnews"
66+
#task_name="eprstmt"
67+
#task_name="bustm"
68+
#task_name="ocnli"
69+
#task_name="csl"
70+
#task_name="csldcp"
71+
#task_name="cluewsc"
72+
#task_name="chid"
73+
python -u -m paddle.distributed.launch --gpus "0" predict.py \
74+
--task_name ${task_name} \
75+
--device gpu \
76+
--init_from_ckpt "./${task_name}/model_120/model_state.pdparams" \
77+
--output_dir "./${task_name}/output" \
78+
--batch_size 32 \
79+
--max_seq_length 512
80+
```
81+
82+
## References
83+
[1] Wang, S., Fang, H., Khabsa, M., Mao, H., and Ma, H., “Entailment as Few-Shot Learner”, ArXiv:2001.07676 [Cs], 2021. https://arxiv.org/abs/2001.07676

0 commit comments

Comments
 (0)