Skip to content

Commit 4baf346

Browse files
author
tianxin
authored
Merge pull request #802 from leeyy2020/PET
Add PET for few_shot
2 parents 0bc5fd4 + 45506b4 commit 4baf346

File tree

16 files changed

+1905
-2
lines changed

16 files changed

+1905
-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.63 | 86.88 | 61.90 | 36.90 | 61.10 | 56.51 | 55.02 | 50.31 | 59.72 | 41.35 |
1616
## 策略库
1717
- [P-tuning](./p-tuning)
1818
- [EFL](./efl)
19-
- PET(Todo)
19+
- 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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
python -u -m paddle.distributed.launch --gpus "0" \
31+
pet.py \
32+
--task_name "tnews" \
33+
--device gpu \
34+
--pattern_id 0 \
35+
--save_dir ./tnews \
36+
--index 0 \
37+
--batch_size 16 \
38+
--learning_rate 1E-4 \
39+
--epochs 10 \
40+
--max_seq_length 512 \
41+
--language_model "ernie-1.0" \
42+
```
43+
参数含义说明
44+
- `task_name`: FewCLUE 中的数据集名字
45+
- `device`: 使用 cpu/gpu 进行训练
46+
- `pattern_id` 完形填空的模式
47+
- `save_dir`: 模型存储路径
48+
- `max_seq_length`: 文本的最大截断长度
49+
50+
模型每训练 1 个 epoch, 会在验证集上进行评估
51+
52+
### 模型预测
53+
通过如下命令,指定 GPU 0 卡, 在 `FewCLUE` 的 `iflytek` 数据集上进行预测
54+
```
55+
python -u -m paddle.distributed.launch --gpus "0" predict.py \
56+
--task_name "tnews" \
57+
--device gpu \
58+
--init_from_ckpt "./tnews/model_120/model_state.pdparams" \
59+
--output_dir "./tnews/output" \
60+
--batch_size 32 \
61+
--max_seq_length 512
62+
```
63+
64+
## References
65+
[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)