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 10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # Contents
  2. - [LSTM Description](#lstm-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Environment Requirements](#environment-requirements)
  6. - [Quick Start](#quick-start)
  7. - [Script Description](#script-description)
  8. - [Script and Sample Code](#script-and-sample-code)
  9. - [Script Parameters](#script-parameters)
  10. - [Dataset Preparation](#dataset-preparation)
  11. - [Training Process](#training-process)
  12. - [Evaluation Process](#evaluation-process)
  13. - [Model Description](#model-description)
  14. - [Performance](#performance)
  15. - [Training Performance](#training-performance)
  16. - [Evaluation Performance](#evaluation-performance)
  17. - [Description of Random Situation](#description-of-random-situation)
  18. - [ModelZoo Homepage](#modelzoo-homepage)
  19. # [LSTM Description](#contents)
  20. This example is for LSTM model training and evaluation.
  21. [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
  22. # [Model Architecture](#contents)
  23. LSTM contains embeding, encoder and decoder modules. Encoder module consists of LSTM layer. Decoder module consists of fully-connection layer.
  24. # [Dataset](#contents)
  25. - aclImdb_v1 for training evaluation.[Large Movie Review Dataset](http://ai.stanford.edu/~amaas/data/sentiment/)
  26. - GloVe: Vector representations for words.[GloVe: Global Vectors for Word Representation](https://nlp.stanford.edu/projects/glove/)
  27. # [Environment Requirements](#contents)
  28. - Hardware(GPU/CPU)
  29. - Framework
  30. - [MindSpore](https://gitee.com/mindspore/mindspore)
  31. - For more information, please check the resources below:
  32. - [MindSpore tutorials](https://www.mindspore.cn/tutorial/en/master/index.html)
  33. - [MindSpore API](https://www.mindspore.cn/api/en/master/index.html)
  34. # [Quick Start](#contents)
  35. - runing on GPU
  36. ```bash
  37. # run training example
  38. bash run_train_gpu.sh 0 ./aclimdb ./glove_dir
  39. # run evaluation example
  40. bash run_eval_gpu.sh 0 ./aclimdb ./glove_dir lstm-20_390.ckpt
  41. ```
  42. - runing on CPU
  43. ```bash
  44. # run training example
  45. bash run_train_cpu.sh ./aclimdb ./glove_dir
  46. # run evaluation example
  47. bash run_eval_cpu.sh ./aclimdb ./glove_dir lstm-20_390.ckpt
  48. ```
  49. # [Script Description](#contents)
  50. ## [Script and Sample Code](#contents)
  51. ```shell
  52. .
  53. ├── lstm
  54.    ├── README.md # descriptions about LSTM
  55.    ├── script
  56.    │   ├── run_eval_gpu.sh # shell script for evaluation on GPU
  57.    │   ├── run_eval_cpu.sh # shell script for evaluation on CPU
  58.    │   ├── run_train_gpu.sh # shell script for training on GPU
  59.    │   └── run_train_cpu.sh # shell script for training on CPU
  60.    ├── src
  61.    │   ├── config.py # parameter configuration
  62.    │   ├── dataset.py # dataset preprocess
  63.    │   ├── imdb.py # imdb dataset read script
  64.    │   └── lstm.py # Sentiment model
  65.    ├── eval.py # evaluation script on both GPU and CPU
  66.    └── train.py # training script on both GPU and CPU
  67. ```
  68. ## [Script Parameters](#contents)
  69. ### Training Script Parameters
  70. ```python
  71. usage: train.py [-h] [--preprocess {true, false}] [--aclimdb_path ACLIMDB_PATH]
  72. [--glove_path GLOVE_PATH] [--preprocess_path PREPROCESS_PATH]
  73. [--ckpt_path CKPT_PATH] [--pre_trained PRE_TRAINING]
  74. [--device_target {GPU, CPU}]
  75. Mindspore LSTM Example
  76. options:
  77. -h, --help # show this help message and exit
  78. --preprocess {true, false} # whether to preprocess data.
  79. --aclimdb_path ACLIMDB_PATH # path where the dataset is stored.
  80. --glove_path GLOVE_PATH # path where the GloVe is stored.
  81. --preprocess_path PREPROCESS_PATH # path where the pre-process data is stored.
  82. --ckpt_path CKPT_PATH # the path to save the checkpoint file.
  83. --pre_trained # the pretrained checkpoint file path.
  84. --device_target # the target device to run, support "GPU", "CPU". Default: "GPU".
  85. ```
  86. ### Running Options
  87. ```python
  88. config.py:
  89. num_classes # classes num
  90. learning_rate # value of learning rate
  91. momentum # value of momentum
  92. num_epochs # epoch size
  93. batch_size # batch size of input dataset
  94. embed_size # the size of each embedding vector
  95. num_hiddens # number of features of hidden layer
  96. num_layers # number of layers of stacked LSTM
  97. bidirectional # specifies whether it is a bidirectional LSTM
  98. save_checkpoint_steps # steps for saving checkpoint files
  99. ```
  100. ### Network Parameters
  101. ## [Dataset Preparation](#contents)
  102. - Download the dataset aclImdb_v1.
  103. > Unzip the aclImdb_v1 dataset to any path you want and the folder structure should be as follows:
  104. > ```
  105. > .
  106. > ├── train # train dataset
  107. > └── test # infer dataset
  108. > ```
  109. - Download the GloVe file.
  110. > Unzip the glove.6B.zip to any path you want and the folder structure should be as follows:
  111. > ```
  112. > .
  113. > ├── glove.6B.100d.txt
  114. > ├── glove.6B.200d.txt
  115. > ├── glove.6B.300d.txt # we will use this one later.
  116. > └── glove.6B.50d.txt
  117. > ```
  118. > Adding a new line at the beginning of the file which named `glove.6B.300d.txt`.
  119. > It means reading a total of 400,000 words, each represented by a 300-latitude word vector.
  120. > ```
  121. > 400000 300
  122. > ```
  123. ## [Training Process](#contents)
  124. - Set options in `config.py`, including learning rate and network hyperparameters.
  125. - runing on GPU
  126. Run `sh run_train_gpu.sh` for training.
  127. ``` bash
  128. bash run_train_gpu.sh 0 ./aclimdb ./glove_dir
  129. ```
  130. The above shell script will run distribute training in the background. You will get the loss value as following:
  131. ```shell
  132. # grep "loss is " log.txt
  133. epoch: 1 step: 390, loss is 0.6003723
  134. epcoh: 2 step: 390, loss is 0.35312173
  135. ...
  136. ```
  137. - runing on CPU
  138. Run `sh run_train_cpu.sh` for training.
  139. ``` bash
  140. bash run_train_cpu.sh ./aclimdb ./glove_dir
  141. ```
  142. The above shell script will train in the background. You will get the loss value as following:
  143. ```shell
  144. # grep "loss is " log.txt
  145. epoch: 1 step: 390, loss is 0.6003723
  146. epcoh: 2 step: 390, loss is 0.35312173
  147. ...
  148. ```
  149. ## [Evaluation Process](#contents)
  150. - evaluation on GPU
  151. Run `bash run_eval_gpu.sh` for evaluation.
  152. ``` bash
  153. bash run_eval_gpu.sh 0 ./aclimdb ./glove_dir lstm-20_390.ckpt
  154. ```
  155. - evaluation on CPU
  156. Run `bash run_eval_cpu.sh` for evaluation.
  157. ``` bash
  158. bash run_eval_cpu.sh ./aclimdb ./glove_dir lstm-20_390.ckpt
  159. ```
  160. # [Model Description](#contents)
  161. ## [Performance](#contents)
  162. ### Training Performance
  163. | Parameters | LSTM (GPU) | LSTM (CPU) |
  164. | -------------------------- | -------------------------------------------------------------- | -------------------------- |
  165. | Resource | Tesla V100-SMX2-16GB | Ubuntu X86-i7-8565U-16GB |
  166. | uploaded Date | 08/06/2020 (month/day/year) | 08/06/2020 (month/day/year)|
  167. | MindSpore Version | 0.6.0-beta | 0.6.0-beta |
  168. | Dataset | aclimdb_v1 | aclimdb_v1 |
  169. | Training Parameters | epoch=20, batch_size=64 | epoch=20, batch_size=64 |
  170. | Optimizer | Momentum | Momentum |
  171. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  172. | Speed | 1022 (1pcs) | 20 |
  173. | Loss | 0.12 | 0.12 |
  174. | Params (M) | 6.45 | 6.45 |
  175. | Checkpoint for inference | 292.9M (.ckpt file) | 292.9M (.ckpt file) |
  176. | 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) |
  177. ### Evaluation Performance
  178. | Parameters | LSTM (GPU) | LSTM (CPU) |
  179. | ------------------- | --------------------------- | ---------------------------- |
  180. | Resource | Tesla V100-SMX2-16GB | Ubuntu X86-i7-8565U-16GB |
  181. | uploaded Date | 08/06/2020 (month/day/year) | 08/06/2020 (month/day/year) |
  182. | MindSpore Version | 0.6.0-beta | 0.6.0-beta |
  183. | Dataset | aclimdb_v1 | aclimdb_v1 |
  184. | batch_size | 64 | 64 |
  185. | Accuracy | 84% | 83% |
  186. # [Description of Random Situation](#contents)
  187. There are three random situations:
  188. - Shuffle of the dataset.
  189. - Initialization of some model weights.
  190. # [ModelZoo Homepage](#contents)
  191. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).