Skip to content

Commit

Permalink
[STACL] Add STACL demo (PaddlePaddle#329)
Browse files Browse the repository at this point in the history
* feat: add STACL demo

* refactor: replace use_cuda with device

* docs: add more description

* refactor: overwrite greedy_search

Co-authored-by: Guo Sheng <[email protected]>
  • Loading branch information
gongel and guoshengCS authored May 12, 2021
1 parent fa6b699 commit a8306f8
Show file tree
Hide file tree
Showing 12 changed files with 707 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ python -m paddle.distributed.launch --gpus "0" train.py --config ./config/transf

建议:如果为了更好的效果,可先在整句模型(即`waik=-1`)进行预训练,然后在此基础上根据不同的waitk进行微调来训练不同的waitk模型,训练的命令都同上,下面给出具体的流程以及主要的参数配置:
- Pretrain
Pretrain用来训练整句模型(即`waik=-1`),可在`config/transformer.yaml`文件中配置参数:
用来训练整句模型(即`waik=-1`),可在`config/transformer.yaml`文件中配置参数:
- `waik`表示waik策略,这里设置为-1
- `training_file`表示训练集,数据格式同上文
- `validation_file`表示验证集,数据格式同上文
- `init_from_checkpoint`表示模型目录,从该checkpoint恢复训练,这里设置为空
- `init_from_pretrain_model`表示模型目录,从该checkpoint开始finetune下游任务,这里设置为空
- `use_cuda`表示是否使用GPU,示例设置为True
- `device`选择训练用的设备,支持cpu/gpu/xpu,默认为gpu
- `use_amp`表示混合精度训练,示例设置为False
- Finetune
Finetune用来训练waik模型(即`waitk=1,2,3,4...`),可在`config/transformer.yaml`文件中配置参数:
用来训练waik模型(即`waitk=1,2,3,4...`),可在`config/transformer.yaml`文件中配置参数:
- `waik`表示waik策略,这里设置为3(以wait-3模型为例)
- `training_file`表示训练集,数据格式同上文
- `validation_file`表示验证集,数据格式同上文
- `init_from_checkpoint`表示模型目录,从该checkpoint恢复训练,这里设置`waik=-1`模型的ckeckpoint
- `init_from_pretrain_model`表示模型目录,从该checkpoint开始finetune下游任务,这里设置为空
- `use_cuda`表示是否使用GPU,示例设置为True
- `device`选择训练用的设备,支持cpu/gpu/xpu,默认为gpu
- `use_amp`表示混合精度训练,示例设置为False
## 模型推理

Expand Down Expand Up @@ -142,6 +142,8 @@ perl mosesdecoder/scripts/generic/multi-bleu.perl newstest2017.tok.en < predict.
| Wait_-1(整句模型) |[下载](https://paddlenlp.bj.bcebos.com/models/stacl/nist_zhen_full_sent.tar.gz) |41.41 |
词表下载:[source vocab](https://paddlenlp.bj.bcebos.com/models/stacl/nist.20k.zh.vocab)[target vocab](https://paddlenlp.bj.bcebos.com/models/stacl/nist.10k.en.vocab)

## Demo展示
通过GUI界面的Demo来模拟STACL实时翻译的效果,可查看[demo](./demo)

## 参考文献
1. Vaswani A, Shazeer N, Parmar N, et al. [Attention is all you need](http://papers.nips.cc/paper/7181-attention-is-all-you-need.pdf)[C]//Advances in Neural Information Processing Systems. 2017: 6000-6010.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ training_file: "data/nist2m/train.zh-en.bpe"
# The pattern to match validation data files.
validation_file: "data/nist2m/dev.zhen.bpe"
# The pattern to match test data files.
predict_file: "data/nist2m/testdata/test_08.zh"
predict_file: "data/nist2m/test_08.zh.bpe"
# The file to output the translation results of predict_file to.
output_file: "predict.txt"
# The path of vocabulary file of source language.
Expand All @@ -27,8 +27,8 @@ trg_vocab_fpath: "data/nist2m/nist.10k.en.vocab"
# The <bos>, <eos> and <unk> tokens in the dictionary.
special_token: ["<s>", "<e>", "<unk>"]

# Whether to use cuda
use_cuda: True
# Use which device to train or predict(cpu,gpu,xpu)
device: gpu

# Args for reader, see reader.py for details
pool_size: 200000
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Demo for STACL

该Demo模拟同传模型STACL实时翻译的效果。
<p align="center">
<img src="images/demo_show.gif" height=350 hspace='10'/> <br />
图 1. 效果展示
</p>
用户通过Chinese input文本框进行输入,通过Jieba和BPE得到分词结果。Simultaneous Translation (wait 1)是读取1个token(分词后)后开始实时翻译,同理,Simultaneous Translation (wait 3)是读取3个token后开始实时翻译,Simultaneous Translation (wait 5)是读取5个token后开始实时翻译。最后,Full Sentence Translation(wait -1)是读取所有的token即整句后开始翻译。
由上图可见,waitk越大(waitk=-1可看作waitk=∞),读入的信息越多,实时翻译效果越好。

### 目录结构

```text
.
├── README.md # 文档,本文件
├── demo.py # 启动demo的主程序文件
├── images
│ ├── clear.png # Demo界面元素
│ ├── demo_show.gif # Demo效果展示图
│ └── paddlenlp.png # Demo界面logo
├── model_for_demo.py # STACL模型文件
├── models # 预训练模型路径
│ ├── nist_wait_-1 # waitk=-1(整句模型)
│ ├── nist_wait_1 # waitk=1模型
│ ├── nist_wait_3 # waitk=3模型
│ └── nist_wait_5 # waitk=5模型
├── requirements.txt # 环境依赖文件
└── transformer_demo.yaml # 参数配置文件
```

上述models下的模型可以在这里[下载](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/examples/machine_translation/text_simultaneous_translation/README.md#%E6%A8%A1%E5%9E%8B%E4%B8%8B%E8%BD%BD%E6%9B%B4%E6%96%B0%E4%B8%AD),下载完后将解压后的`transformer.pdparams`分别放在不同的waitk策略对应的子目录下面。

### 参数说明与配置

可以在`config/transformer_demo.yaml` 文件中设置相应的参数,下面给出主要的参数配置:

- `src_bpe_dict`配置源语言(这里是中文)的BPE词表,[中文BPE词表下载](https://paddlenlp.bj.bcebos.com/models/stacl/2M.zh2en.dict4bpe.zh)
- `src_vocab_fpath`配置源语言(这里是中文)词表,[source vocab](https://paddlenlp.bj.bcebos.com/models/stacl/nist.20k.zh.vocab)
- `trg_vocab_fpath`配置目标语言(这里是英文)词表,[target vocab](https://paddlenlp.bj.bcebos.com/models/stacl/nist.10k.en.vocab)
- `device`选择预测用的设备,支持cpu/gpu/xpu,默认为cpu

### 环境依赖

- attrdict==2.0.1
- PyYAML==5.4.1
- subword_nmt==0.3.7
- jieba==0.42.1

安装命令:`pip install -r requirements.txt`

### 使用说明

1. 下载好预训练模型,并放在对应的目录下;
2. 下载好词表(源语言词表,目标语言词表,BPE词表),并在配置文件`transformer_demo.yaml`中修改相应的参数;
3. 运行`demo.py`
4. 出现界面,在Chinese input文本框中输入中文,按【回车键】开始实时翻译,遇到【。!?】结束整句,按【CLEAR】清空所有的输入和输出。
Loading

0 comments on commit a8306f8

Please sign in to comment.