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