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 13 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. [查看中文](./README_CN.md)
  2. # Contents
  3. - [LSTM Description](#lstm-description)
  4. - [Model Architecture](#model-architecture)
  5. - [Dataset](#dataset)
  6. - [Environment Requirements](#environment-requirements)
  7. - [Quick Start](#quick-start)
  8. - [Script Description](#script-description)
  9. - [Script and Sample Code](#script-and-sample-code)
  10. - [Script Parameters](#script-parameters)
  11. - [Dataset Preparation](#dataset-preparation)
  12. - [Training Process](#training-process)
  13. - [Evaluation Process](#evaluation-process)
  14. - [Model Description](#model-description)
  15. - [Performance](#performance)
  16. - [Training Performance](#training-performance)
  17. - [Evaluation Performance](#evaluation-performance)
  18. - [Description of Random Situation](#description-of-random-situation)
  19. - [ModelZoo Homepage](#modelzoo-homepage)
  20. # [LSTM Description](#contents)
  21. This example is for LSTM model training and evaluation.
  22. [Paper](https://www.aclweb.org/anthology/P11-1015/): Andrew L. Maas, Raymond E. Daly, Peter T. Pham, Dan Huang, Andrew Y. Ng, Christopher Potts. [Learning Word Vectors for Sentiment Analysis](https://www.aclweb.org/anthology/P11-1015/). Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies. 2011
  23. # [Model Architecture](#contents)
  24. LSTM contains embeding, encoder and decoder modules. Encoder module consists of LSTM layer. Decoder module consists of fully-connection layer.
  25. # [Dataset](#contents)
  26. 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.
  27. - aclImdb_v1 for training evaluation.[Large Movie Review Dataset](http://ai.stanford.edu/~amaas/data/sentiment/)
  28. - GloVe: Vector representations for words.[GloVe: Global Vectors for Word Representation](https://nlp.stanford.edu/projects/glove/)
  29. # [Environment Requirements](#contents)
  30. - Hardware(GPU/CPU/Ascend)
  31. - Prepare hardware environment with Ascend, GPU or CPU processor.
  32. - Framework
  33. - [MindSpore](https://gitee.com/mindspore/mindspore)
  34. - For more information, please check the resources below:
  35. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  36. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  37. # [Quick Start](#contents)
  38. - running on Ascend
  39. ```bash
  40. # run training example
  41. bash run_train_ascend.sh 0 ./aclimdb ./glove_dir
  42. # run evaluation example
  43. bash run_eval_ascend.sh 0 ./preprocess lstm-20_390.ckpt
  44. ```
  45. - running on GPU
  46. ```bash
  47. # run training example
  48. bash run_train_gpu.sh 0 ./aclimdb ./glove_dir
  49. # run evaluation example
  50. bash run_eval_gpu.sh 0 ./aclimdb ./glove_dir lstm-20_390.ckpt
  51. ```
  52. - running on CPU
  53. ```bash
  54. # run training example
  55. bash run_train_cpu.sh ./aclimdb ./glove_dir
  56. # run evaluation example
  57. bash run_eval_cpu.sh ./aclimdb ./glove_dir lstm-20_390.ckpt
  58. ```
  59. # [Script Description](#contents)
  60. ## [Script and Sample Code](#contents)
  61. ```shell
  62. .
  63. ├── lstm
  64.    ├── README.md # descriptions about LSTM
  65.    ├── script
  66.    │   ├── run_eval_gpu.sh # shell script for evaluation on GPU
  67.    │   ├── run_eval_ascend.sh # shell script for evaluation on Ascend
  68.    │   ├── run_eval_cpu.sh # shell script for evaluation on CPU
  69.    │   ├── run_train_gpu.sh # shell script for training on GPU
  70.    │   ├── run_train_ascend.sh # shell script for training on Ascend
  71.    │   └── run_train_cpu.sh # shell script for training on CPU
  72.    ├── src
  73.    │   ├── config.py # parameter configuration
  74.    │   ├── dataset.py # dataset preprocess
  75.    │   ├── imdb.py # imdb dataset read script
  76.    │   ├── lr_schedule.py # dynamic_lr script
  77.    │   └── lstm.py # Sentiment model
  78.    ├── eval.py # evaluation script on GPU, CPU and Ascend
  79.    └── train.py # training script on GPU, CPU and Ascend
  80. ```
  81. ## [Script Parameters](#contents)
  82. ### Training Script Parameters
  83. ```python
  84. usage: train.py [-h] [--preprocess {true, false}] [--aclimdb_path ACLIMDB_PATH]
  85. [--glove_path GLOVE_PATH] [--preprocess_path PREPROCESS_PATH]
  86. [--ckpt_path CKPT_PATH] [--pre_trained PRE_TRAINING]
  87. [--device_target {GPU, CPU, Ascend}]
  88. Mindspore LSTM Example
  89. options:
  90. -h, --help # show this help message and exit
  91. --preprocess {true, false} # whether to preprocess data.
  92. --aclimdb_path ACLIMDB_PATH # path where the dataset is stored.
  93. --glove_path GLOVE_PATH # path where the GloVe is stored.
  94. --preprocess_path PREPROCESS_PATH # path where the pre-process data is stored.
  95. --ckpt_path CKPT_PATH # the path to save the checkpoint file.
  96. --pre_trained # the pretrained checkpoint file path.
  97. --device_target # the target device to run, support "GPU", "CPU", "Ascend". Default: "Ascend".
  98. ```
  99. ### Running Options
  100. ```python
  101. config.py:
  102. GPU/CPU:
  103. num_classes # classes num
  104. dynamic_lr # if use dynamic learning rate
  105. learning_rate # value of learning rate
  106. momentum # value of momentum
  107. num_epochs # epoch size
  108. batch_size # batch size of input dataset
  109. embed_size # the size of each embedding vector
  110. num_hiddens # number of features of hidden layer
  111. num_layers # number of layers of stacked LSTM
  112. bidirectional # specifies whether it is a bidirectional LSTM
  113. save_checkpoint_steps # steps for saving checkpoint files
  114. Ascend:
  115. num_classes # classes num
  116. momentum # value of momentum
  117. num_epochs # epoch size
  118. batch_size # batch size of input dataset
  119. embed_size # the size of each embedding vector
  120. num_hiddens # number of features of hidden layer
  121. num_layers # number of layers of stacked LSTM
  122. bidirectional # specifies whether it is a bidirectional LSTM
  123. save_checkpoint_steps # steps for saving checkpoint files
  124. keep_checkpoint_max # max num of checkpoint files
  125. dynamic_lr # if use dynamic learning rate
  126. lr_init # init learning rate of Dynamic learning rate
  127. lr_end # end learning rate of Dynamic learning rate
  128. lr_max # max learning rate of Dynamic learning rate
  129. lr_adjust_epoch # Dynamic learning rate adjust epoch
  130. warmup_epochs # warmup epochs
  131. global_step # global step
  132. ```
  133. ### Network Parameters
  134. ## [Dataset Preparation](#contents)
  135. - Download the dataset aclImdb_v1.
  136. Unzip the aclImdb_v1 dataset to any path you want and the folder structure should be as follows:
  137. ```bash
  138. .
  139. ├── train # train dataset
  140. └── test # infer dataset
  141. ```
  142. - Download the GloVe file.
  143. Unzip the glove.6B.zip to any path you want and the folder structure should be as follows:
  144. ```bash
  145. .
  146. ├── glove.6B.100d.txt
  147. ├── glove.6B.200d.txt
  148. ├── glove.6B.300d.txt # we will use this one later.
  149. └── glove.6B.50d.txt
  150. ```
  151. Adding a new line at the beginning of the file which named `glove.6B.300d.txt`.
  152. It means reading a total of 400,000 words, each represented by a 300-latitude word vector.
  153. ```bash
  154. 400000 300
  155. ```
  156. ## [Training Process](#contents)
  157. - Set options in `config.py`, including learning rate and network hyperparameters.
  158. - running on Ascend
  159. Run `sh run_train_ascend.sh` for training.
  160. ``` bash
  161. bash run_train_ascend.sh 0 ./aclimdb ./glove_dir
  162. ```
  163. The above shell script will train in the background. You will get the loss value as following:
  164. ```shell
  165. # grep "loss is " log.txt
  166. epoch: 1 step: 390, loss is 0.6003723
  167. epcoh: 2 step: 390, loss is 0.35312173
  168. ...
  169. ```
  170. - running on GPU
  171. Run `sh run_train_gpu.sh` for training.
  172. ``` bash
  173. bash run_train_gpu.sh 0 ./aclimdb ./glove_dir
  174. ```
  175. The above shell script will run distribute training in the background. You will get the loss value as following:
  176. ```shell
  177. # grep "loss is " log.txt
  178. epoch: 1 step: 390, loss is 0.6003723
  179. epcoh: 2 step: 390, loss is 0.35312173
  180. ...
  181. ```
  182. - running on CPU
  183. Run `sh run_train_cpu.sh` for training.
  184. ``` bash
  185. bash run_train_cpu.sh ./aclimdb ./glove_dir
  186. ```
  187. The above shell script will train in the background. You will get the loss value as following:
  188. ```shell
  189. # grep "loss is " log.txt
  190. epoch: 1 step: 390, loss is 0.6003723
  191. epcoh: 2 step: 390, loss is 0.35312173
  192. ...
  193. ```
  194. ## [Evaluation Process](#contents)
  195. - evaluation on Ascend
  196. Run `bash run_eval_ascend.sh` for evaluation.
  197. ``` bash
  198. bash run_eval_ascend.sh 0 ./preprocess lstm-20_390.ckpt
  199. ```
  200. - evaluation on GPU
  201. Run `bash run_eval_gpu.sh` for evaluation.
  202. ``` bash
  203. bash run_eval_gpu.sh 0 ./aclimdb ./glove_dir lstm-20_390.ckpt
  204. ```
  205. - evaluation on CPU
  206. Run `bash run_eval_cpu.sh` for evaluation.
  207. ``` bash
  208. bash run_eval_cpu.sh ./aclimdb ./glove_dir lstm-20_390.ckpt
  209. ```
  210. # [Model Description](#contents)
  211. ## [Performance](#contents)
  212. ### Training Performance
  213. | Parameters | LSTM (Ascend) | LSTM (GPU) | LSTM (CPU) |
  214. | -------------------------- | -------------------------- | -------------------------------------------------------------- | -------------------------- |
  215. | Resource | Ascend 910 | Tesla V100-SMX2-16GB | Ubuntu X86-i7-8565U-16GB |
  216. | uploaded Date | 12/21/2020 (month/day/year)| 10/28/2020 (month/day/year) | 10/28/2020 (month/day/year)|
  217. | MindSpore Version | 1.1.0 | 1.0.0 | 1.0.0 |
  218. | Dataset | aclimdb_v1 | aclimdb_v1 | aclimdb_v1 |
  219. | Training Parameters | epoch=20, batch_size=64 | epoch=20, batch_size=64 | epoch=20, batch_size=64 |
  220. | Optimizer | Momentum | Momentum | Momentum |
  221. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy | Softmax Cross Entropy |
  222. | Speed | 1049 | 1022 (1pcs) | 20 |
  223. | Loss | 0.12 | 0.12 | 0.12 |
  224. | Params (M) | 6.45 | 6.45 | 6.45 |
  225. | Checkpoint for inference | 292.9M (.ckpt file) | 292.9M (.ckpt file) | 292.9M (.ckpt file) |
  226. | Scripts | [lstm script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/lstm) | [lstm script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/lstm) | [lstm script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/lstm) |
  227. ### Evaluation Performance
  228. | Parameters | LSTM (Ascend) | LSTM (GPU) | LSTM (CPU) |
  229. | ------------------- | ---------------------------- | --------------------------- | ---------------------------- |
  230. | Resource | Ascend 910 | Tesla V100-SMX2-16GB | Ubuntu X86-i7-8565U-16GB |
  231. | uploaded Date | 12/21/2020 (month/day/year) | 10/28/2020 (month/day/year) | 10/28/2020 (month/day/year) |
  232. | MindSpore Version | 1.1.0 | 1.0.0 | 1.0.0 |
  233. | Dataset | aclimdb_v1 | aclimdb_v1 | aclimdb_v1 |
  234. | batch_size | 64 | 64 | 64 |
  235. | Accuracy | 85% | 84% | 83% |
  236. # [Description of Random Situation](#contents)
  237. There are three random situations:
  238. - Shuffle of the dataset.
  239. - Initialization of some model weights.
  240. # [ModelZoo Homepage](#contents)
  241. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).