You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 14 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ![](https://www.mindspore.cn/static/img/logo.a3e472c9.png)
  2. <!-- TOC -->
  3. - [GNMT v2 For MindSpore](#gnmt-v2-for-mindspore)
  4. - [Model Structure](#model-structure)
  5. - [Dataset](#dataset)
  6. - [Environment Requirements](#environment-requirements)
  7. - [Platform](#platform)
  8. - [Software](#software)
  9. - [Quick Start](#quick-start)
  10. - [Script Description](#script-description)
  11. - [Dataset Preparation](#dataset-preparation)
  12. - [Configuration File](#configuration-file)
  13. - [Training Process](#training-process)
  14. - [Inference Process](#inference-process)
  15. - [Model Description](#model-description)
  16. - [Performance](#performance)
  17. - [Result](#result)
  18. - [Training Performance](#training-performance)
  19. - [Inference Performance](#inference-performance)
  20. - [Random Situation Description](#random-situation-description)
  21. - [Others](#others)
  22. - [ModelZoo](#modelzoo)
  23. <!-- /TOC -->
  24. # GNMT v2 For MindSpore
  25. The GNMT v2 model is similar to the model described in [Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation](https://arxiv.org/abs/1609.08144), which is mainly used for corpus translation.
  26. # Model Structure
  27. The GNMTv2 model mainly consists of an encoder, a decoder, and an attention mechanism, where the encoder and the decoder use a shared word embedding vector.
  28. Encoder: consists of four long short-term memory (LSTM) layers. The first LSTM layer is bidirectional, while the other three layers are unidirectional.
  29. Decoder: consists of four unidirectional LSTM layers and a fully connected classifier. The output embedding dimension of LSTM is 1024.
  30. Attention mechanism: uses the standardized Bahdanau attention mechanism. First, the first layer output of the decoder is used as the input of the attention mechanism. Then, the computing result of the attention mechanism is connected to the input of the decoder LSTM, which is used as the input of the subsequent LSTM layer.
  31. # Dataset
  32. Note that you can run the scripts based on the dataset mentioned in original paper or widely used in relevant domain/network architecture. In the following sections, we will introduce how to run the scripts using the related dataset below.
  33. - *WMT Englis-German* for training.
  34. - *WMT newstest2014* for evaluation.
  35. # Environment Requirements
  36. ## Platform
  37. - Hardware (Ascend)
  38. - Prepare hardware environment with Ascend processor. If you want to try Ascend, please send the [application form](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/file/other/Ascend%20Model%20Zoo%E4%BD%93%E9%AA%8C%E8%B5%84%E6%BA%90%E7%94%B3%E8%AF%B7%E8%A1%A8.docx) to ascend@huawei.com. Once approved, you could get the resources for trial.
  39. - Framework
  40. - Install [MindSpore](https://www.mindspore.cn/install/en).
  41. - For more information, please check the resources below:
  42. - [MindSpore tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  43. - [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  44. ## Software
  45. ```txt
  46. numpy
  47. sacrebleu==1.2.10
  48. sacremoses==0.0.19
  49. subword_nmt==0.3.7
  50. ```
  51. # [Quick Start](#contents)
  52. The process of GNMTv2 performing the text translation task is as follows:
  53. 1. Download the wmt16 data corpus and extract the dataset. For details, see the chapter "_Dataset_" above.
  54. 2. Dataset preparation and configuration.
  55. 3. Training.
  56. 4. Inference.
  57. After dataset preparation, you can start training and evaluation as follows:
  58. ```bash
  59. # run training example
  60. cd ./scripts
  61. sh run_standalone_train_ascend.sh DATASET_SCHEMA_TRAIN PRE_TRAIN_DATASET
  62. # run distributed training example
  63. cd ./scripts
  64. sh run_distributed_train_ascend.sh RANK_TABLE_ADDR DATASET_SCHEMA_TRAIN PRE_TRAIN_DATASET
  65. # run evaluation example
  66. cd ./scripts
  67. sh run_standalone_eval_ascend.sh DATASET_SCHEMA_TEST TEST_DATASET EXISTED_CKPT_PATH \
  68. VOCAB_ADDR BPE_CODE_ADDR TEST_TARGET
  69. ```
  70. # Script Description
  71. The GNMT network script and code result are as follows:
  72. ```text
  73. ├── gnmt
  74. ├── README.md // Introduction of GNMTv2 model.
  75. ├── config
  76. │ ├──__init__.py // User interface.
  77. │ ├──config.py // Configuration instance definition.
  78. │ ├──config.json // Configuration file for pre-train or finetune.
  79. │ ├──config_test.json // Configuration file for test.
  80. ├── src
  81. │ ├──__init__.py // User interface.
  82. │ ├──dataset
  83. │ ├──__init__.py // User interface.
  84. │ ├──base.py // Base class of data loader.
  85. │ ├──bi_data_loader.py // Bilingual data loader.
  86. │ ├──load_dataset.py // Dataset loader to feed into model.
  87. │ ├──schema.py // Define schema of mindrecord.
  88. │ ├──tokenizer.py // Tokenizer class.
  89. │ ├──gnmt_model
  90. │ ├──__init__.py // User interface.
  91. │ ├──attention.py // Bahdanau attention mechanism.
  92. │ ├──beam_search.py // Beam search decoder for inferring.
  93. │ ├──bleu_calculate.py // Calculat the blue accuracy.
  94. │ ├──components.py // Components.
  95. │ ├──create_attention.py // Recurrent attention.
  96. │ ├──create_attn_padding.py // Create attention paddings from input paddings.
  97. │ ├──decoder.py // GNMT decoder component.
  98. │ ├──decoder_beam_infer.py // GNMT decoder component for beam search.
  99. │ ├──dynamic_rnn.py // DynamicRNN.
  100. │ ├──embedding.py // Embedding component.
  101. │ ├──encoder.py // GNMT encoder component.
  102. │ ├──gnmt.py // GNMT model architecture.
  103. │ ├──gnmt_for_infer.py // Use GNMT to infer.
  104. │ ├──gnmt_for_train.py // Use GNMT to train.
  105. │ ├──grad_clip.py // Gradient clip
  106. │ ├──utils
  107. │ ├──__init__.py // User interface.
  108. │ ├──initializer.py // Parameters initializer.
  109. │ ├──load_weights.py // Load weights from a checkpoint or NPZ file.
  110. │ ├──loss_moniter.py // Callback of monitering loss during training step.
  111. │ ├──lr_scheduler.py // Learning rate scheduler.
  112. │ ├──optimizer.py // Optimizer.
  113. ├── scripts
  114. │ ├──run_distributed_train_ascend.sh // shell script for distributed train on ascend.
  115. │ ├──run_standalone_eval_ascend.sh // shell script for standalone eval on ascend.
  116. │ ├──run_standalone_train_ascend.sh // shell script for standalone eval on ascend.
  117. ├── create_dataset.py // dataset preparation.
  118. ├── eval.py // Infer API entry.
  119. ├── requirements.txt // Requirements of third party package.
  120. ├── train.py // Train API entry.
  121. ```
  122. ## Dataset Preparation
  123. You may use this [shell script](https://github.com/NVIDIA/DeepLearningExamples/blob/master/TensorFlow/Translation/GNMT/scripts/wmt16_en_de.sh) to download and preprocess WMT English-German dataset. Assuming you get the following files:
  124. - train.tok.clean.bpe.32000.en
  125. - train.tok.clean.bpe.32000.de
  126. - vocab.bpe.32000
  127. - bpe.32000
  128. - newstest2014.en
  129. - newstest2014.de
  130. - Convert the original data to tfrecord for training and evaluation:
  131. ``` bash
  132. python create_dataset.py --src_folder /home/workspace/wmt16_de_en --output_folder /home/workspace/dataset_menu
  133. ```
  134. ## Configuration File
  135. The JSON file in the `config/` directory is the template configuration file.
  136. Almost all required options and parameters can be easily assigned, including the training platform, model configuration, and optimizer parameters.
  137. - config for GNMTv2
  138. ```python
  139. 'random_seed': 50 # global random seed
  140. 'epochs':6 # total training epochs
  141. 'batch_size': 128 # training batch size
  142. 'dataset_sink_mode': true # whether use dataset sink mode
  143. 'seq_length': 51 # max length of source sentences
  144. 'vocab_size': 32320 # vocabulary size
  145. 'hidden_size': 125 # the output's last dimension of dynamicRNN
  146. 'initializer_range': 0.1 # initializer range
  147. 'max_decode_length': 125 # max length of decoder
  148. 'lr': 0.1 # initial learning rate
  149. 'lr_scheduler': 'WarmupMultiStepLR' # learning rate scheduler
  150. 'existed_ckpt': '' # the absolute full path to save the checkpoint file
  151. ```
  152. For more configuration details, please refer the script `config/config.py` file.
  153. ## Training Process
  154. For a pre-trained model, configure the following options in the `scripts/run_standalone_train_ascend.json` file:
  155. - Select an optimizer ('momentum/adam/lamb' is available).
  156. - Specify `ckpt_prefix` and `ckpt_path` in `checkpoint_path` to save the model file.
  157. - Set other parameters, including dataset configuration and network configuration.
  158. - If a pre-trained model exists, assign `existed_ckpt` to the path of the existing model during fine-tuning.
  159. Start task training on a single device and run the shell script `scripts/run_standalone_train_ascend.sh`:
  160. ```bash
  161. cd ./scripts
  162. sh run_standalone_train_ascend.sh DATASET_SCHEMA_TRAIN PRE_TRAIN_DATASET
  163. ```
  164. In this script, the `DATASET_SCHEMA_TRAIN` and `PRE_TRAIN_DATASET` are the dataset schema and dataset address.
  165. Run `scripts/run_distributed_train_ascend.sh` for distributed training of GNMTv2 model.
  166. Task training on multiple devices and run the following command in bash to be executed in `scripts/`.:
  167. ```bash
  168. cd ./scripts
  169. sh run_distributed_train_ascend.sh RANK_TABLE_ADDR DATASET_SCHEMA_TRAIN PRE_TRAIN_DATASET
  170. ```
  171. Note: the `RANK_TABLE_ADDR` is the hccl_json file assigned when distributed training is running.
  172. Currently, inconsecutive device IDs are not supported in `scripts/run_distributed_train_ascend.sh`. The device ID must start from 0 in the `RANK_TABLE_ADDR` file.
  173. ## Inference Process
  174. For inference using a trained model on multiple hardware platforms, such as Ascend 910.
  175. Set options in `config/config_test.json`.
  176. Run the shell script `scripts/run_standalone_eval_ascend.sh` to process the output token ids to get the BLEU scores.
  177. ```bash
  178. cd ./scripts
  179. sh run_standalone_eval_ascend.sh
  180. sh run_standalone_eval_ascend.sh DATASET_SCHEMA_TEST TEST_DATASET EXISTED_CKPT_PATH \
  181. VOCAB_ADDR BPE_CODE_ADDR TEST_TARGET
  182. ```
  183. The `DATASET_SCHEMA_TEST` and the `TEST_DATASET` are the schema and address of inference dataset respectively, and `EXISTED_CKPT_PATH` is the path of the model file generated during training process.
  184. The `VOCAB_ADDR` is the vocabulary address, `BPE_CODE_ADDR` is the bpe code address and the `TEST_TARGET` are the path of answers.
  185. # Model Description
  186. ## Performance
  187. ### Result
  188. #### Training Performance
  189. | Parameters | Ascend |
  190. | -------------------------- | -------------------------------------------------------------- |
  191. | Resource | Ascend 910 |
  192. | uploaded Date | 11/06/2020 (month/day/year) |
  193. | MindSpore Version | 1.0.0 |
  194. | Dataset | WMT Englis-German |
  195. | Training Parameters | epoch=6, batch_size=128 |
  196. | Optimizer | Adam |
  197. | Loss Function | Softmax Cross Entropy |
  198. | outputs | probability |
  199. | Speed | 344ms/step (8pcs) |
  200. | Total Time | 7800s (8pcs) |
  201. | Loss | 63.35 |
  202. | Params (M) | 613 |
  203. | Checkpoint for inference | 1.8G (.ckpt file) |
  204. | Scripts | [gnmt_v2](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/gnmt_v2) |
  205. #### Inference Performance
  206. | Parameters | Ascend |
  207. | ------------------- | --------------------------- |
  208. | Resource | Ascend 910 |
  209. | Uploaded Date | 11/06/2020 (month/day/year) |
  210. | MindSpore Version | 1.0.0 |
  211. | Dataset | WMT newstest2014 |
  212. | batch_size | 128 |
  213. | Total Time | 1560s |
  214. | outputs | probability |
  215. | Accuracy | BLEU Score= 24.05 |
  216. | Model for inference | 1.8G (.ckpt file) |
  217. # Random Situation Description
  218. There are three random situations:
  219. - Shuffle of the dataset.
  220. - Initialization of some model weights.
  221. - Dropout operations.
  222. Some seeds have already been set in train.py to avoid the randomness of dataset shuffle and weight initialization. If you want to disable dropout, please set the corresponding dropout_prob parameter to 0 in config/config.json.
  223. # Others
  224. This model has been validated in the Ascend environment and is not validated on the CPU and GPU.
  225. # ModelZoo 主页
  226. [链接](https://gitee.com/mindspore/mindspore/tree/master/model_zoo)