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 8.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. # [Script Description](#contents)
  43. ## [Script and Sample Code](#contents)
  44. ```shell
  45. .
  46. ├── lstm
  47.    ├── README.md # descriptions about LSTM
  48.    ├── script
  49.    │   ├── run_eval_gpu.sh # shell script for evaluation on GPU
  50.    │   └── run_train_gpu.sh # shell script for training on GPU
  51.    ├── src
  52.    │   ├── config.py # parameter configuration
  53.    │   ├── dataset.py # dataset preprocess
  54.    │   ├── imdb.py # imdb dataset read script
  55.    │   └── lstm.py # Sentiment model
  56.    ├── eval.py # evaluation script
  57.    └── train.py # training script
  58. ```
  59. ## [Script Parameters](#contents)
  60. ### Training Script Parameters
  61. ```python
  62. usage: train.py [-h] [--preprocess {true, false}] [--aclimdb_path ACLIMDB_PATH]
  63. [--glove_path GLOVE_PATH] [--preprocess_path PREPROCESS_PATH]
  64. [--ckpt_path CKPT_PATH] [--pre_trained PRE_TRAINING]
  65. [--device_target {GPU, CPU}]
  66. Mindspore LSTM Example
  67. options:
  68. -h, --help # show this help message and exit
  69. --preprocess {true, false} # whether to preprocess data.
  70. --aclimdb_path ACLIMDB_PATH # path where the dataset is stored.
  71. --glove_path GLOVE_PATH # path where the GloVe is stored.
  72. --preprocess_path PREPROCESS_PATH # path where the pre-process data is stored.
  73. --ckpt_path CKPT_PATH # the path to save the checkpoint file.
  74. --pre_trained # the pretrained checkpoint file path.
  75. --device_target # the target device to run, support "GPU", "CPU". Default: "GPU".
  76. ```
  77. ### Running Options
  78. ```python
  79. config.py:
  80. num_classes # classes num
  81. learning_rate # value of learning rate
  82. momentum # value of momentum
  83. num_epochs # epoch size
  84. batch_size # batch size of input dataset
  85. embed_size # the size of each embedding vector
  86. num_hiddens # number of features of hidden layer
  87. num_layers # number of layers of stacked LSTM
  88. bidirectional # specifies whether it is a bidirectional LSTM
  89. save_checkpoint_steps # steps for saving checkpoint files
  90. ```
  91. ### Network Parameters
  92. ## [Dataset Preparation](#contents)
  93. - Download the dataset aclImdb_v1.
  94. > Unzip the aclImdb_v1 dataset to any path you want and the folder structure should be as follows:
  95. > ```
  96. > .
  97. > ├── train # train dataset
  98. > └── test # infer dataset
  99. > ```
  100. - Download the GloVe file.
  101. > Unzip the glove.6B.zip to any path you want and the folder structure should be as follows:
  102. > ```
  103. > .
  104. > ├── glove.6B.100d.txt
  105. > ├── glove.6B.200d.txt
  106. > ├── glove.6B.300d.txt # we will use this one later.
  107. > └── glove.6B.50d.txt
  108. > ```
  109. > Adding a new line at the beginning of the file which named `glove.6B.300d.txt`.
  110. > It means reading a total of 400,000 words, each represented by a 300-latitude word vector.
  111. > ```
  112. > 400000 300
  113. > ```
  114. ## [Training Process](#contents)
  115. - Set options in `config.py`, including learning rate and network hyperparameters.
  116. - Run `sh run_train_gpu.sh` for training.
  117. ``` bash
  118. bash run_train_gpu.sh 0 ./aclimdb ./glove_dir
  119. ```
  120. The above shell script will run distribute training in the background. You will get the loss value as following:
  121. ```shell
  122. # grep "loss is " log.txt
  123. epoch: 1 step: 390, loss is 0.6003723
  124. epcoh: 2 step: 390, loss is 0.35312173
  125. ...
  126. ```
  127. ## [Evaluation Process](#contents)
  128. - Run `bash run_eval_gpu.sh` for evaluation.
  129. ``` bash
  130. bash run_eval_gpu.sh 0 ./aclimdb ./glove_dir lstm-20_390.ckpt
  131. ```
  132. # [Model Description](#contents)
  133. ## [Performance](#contents)
  134. ### Training Performance
  135. | Parameters | LSTM |
  136. | -------------------------- | -------------------------------------------------------------- |
  137. | Resource | Tesla V100-SMX2-16GB |
  138. | uploaded Date | 08/06/2020 (month/day/year) |
  139. | MindSpore Version | 0.6.0-beta |
  140. | Dataset | aclimdb_v1 |
  141. | Training Parameters | epoch=20, batch_size=64 |
  142. | Optimizer | Momentum |
  143. | Loss Function | Softmax Cross Entropy |
  144. | Speed | 1022 (1pcs) |
  145. | Loss | 0.12 |
  146. | Params (M) | 6.45 |
  147. | Checkpoint for inference | 292.9M (.ckpt file) |
  148. | Scripts | https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/lstm |
  149. ### Evaluation Performance
  150. | Parameters | LSTM |
  151. | ------------------- | --------------------------- |
  152. | Resource | Tesla V100-SMX2-16GB |
  153. | uploaded Date | 08/06/2020 (month/day/year) |
  154. | MindSpore Version | 0.6.0-beta |
  155. | Dataset | aclimdb_v1 |
  156. | batch_size | 64 |
  157. | Accuracy | 84% |
  158. # [Description of Random Situation](#contents)
  159. There are three random situations:
  160. - Shuffle of the dataset.
  161. - Initialization of some model weights.
  162. # [ModelZoo Homepage](#contents)
  163. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).