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 19 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # FastText
  2. ![mindspore](https://www.mindspore.cn/static/img/logo_black.6a5c850d.png)
  3. <!-- TOC -->
  4. - [FastText](#fasttext)
  5. - [Model Structure](#model-structure)
  6. - [Dataset](#dataset)
  7. - [Environment Requirements](#environment-requirements)
  8. - [Quick Start](#quick-start)
  9. - [Script Description](#script-description)
  10. - [Dataset Preparation](#dataset-preparation)
  11. - [Configuration File](#configuration-file)
  12. - [Training Process](#training-process)
  13. - [Inference Process](#inference-process)
  14. - [Model Description](#model-description)
  15. - [Performance](#performance)
  16. - [Training Performance](#training-performance)
  17. - [Inference Performance](#inference-performance)
  18. - [Random Situation Description](#random-situation-description)
  19. - [Others](#others)
  20. - [ModelZoo HomePage](#modelzoo-homepage)
  21. <!-- /TOC -->
  22. ## [FastText](#contents)
  23. FastText is a fast text classification algorithm, which is simple and efficient. It was proposed by Armand
  24. Joulin, Tomas Mikolov etc. in the article "Bag of Tricks for Efficient Text Classification" in 2016. It is similar to
  25. CBOW in model architecture, where the middle word is replace by a label. FastText adopts ngram feature as addition feature
  26. to get some information about words. It speeds up training and testing while maintaining high precision, and widly used
  27. in various tasks of text classification.
  28. [Paper](https://arxiv.org/pdf/1607.01759.pdf): "Bag of Tricks for Efficient Text Classification", 2016, A. Joulin, E. Grave, P. Bojanowski, and T. Mikolov
  29. ## [Model Structure](#contents)
  30. The FastText model mainly consists of an input layer, hidden layer and output layer, where the input is a sequence of words (text or sentence).
  31. The output layer is probability that the words sequence belongs to different categories. The hidden layer is formed by average of multiple word vector.
  32. The feature is mapped to the hidden layer through linear transformation, and then mapped to the label from the hidden layer.
  33. ## [Dataset](#contents)
  34. Note that you can run the scripts based on the dataset mentioned in original paper or widely used in relevant domain/network
  35. architecture. In the following sections, we will introduce how to run the scripts using the related dataset below.
  36. - AG's news topic classification dataset
  37. - DBPedia Ontology Classification Dataset
  38. - Yelp Review Polarity Dataset
  39. ## [Environment Requirements](#content)
  40. - Hardware(Ascend/GPU)
  41. - Prepare hardware environment with Ascend or GPU processor.
  42. - Framework
  43. - [MindSpore](https://gitee.com/mindspore/mindspore)
  44. - For more information, please check the resources below:
  45. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  46. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  47. ## [Quick Start](#content)
  48. After dataset preparation, you can start training and evaluation as follows:
  49. ```bash
  50. # run training example
  51. cd ./scripts
  52. sh run_standalone_train.sh [TRAIN_DATASET] [DEVICEID]
  53. # run distributed training example
  54. sh run_distribute_train.sh [TRAIN_DATASET] [RANK_TABLE_PATH]
  55. # run evaluation example
  56. sh run_eval.sh [EVAL_DATASET_PATH] [DATASET_NAME] [MODEL_CKPT] [DEVICEID]
  57. ```
  58. ## [Script Description](#content)
  59. The FastText network script and code result are as follows:
  60. ```text
  61. ├── fasttext
  62. ├── README.md // Introduction of FastText model.
  63. ├── src
  64. │ ├──config.py // Configuration instance definition.
  65. │ ├──create_dataset.py // Dataset preparation.
  66. │ ├──fasttext_model.py // FastText model architecture.
  67. │ ├──fasttext_train.py // Use FastText model architecture.
  68. │ ├──load_dataset.py // Dataset loader to feed into model.
  69. │ ├──lr_scheduler.py // Learning rate scheduler.
  70. ├── scripts
  71. │ ├──run_distributed_train.sh // shell script for distributed train on ascend.
  72. │ ├──run_eval.sh // shell script for standalone eval on ascend.
  73. │ ├──run_standalone_train.sh // shell script for standalone eval on ascend.
  74. │ ├──run_distributed_train_gpu.sh // shell script for distributed train on GPU.
  75. │ ├──run_eval_gpu.sh // shell script for standalone eval on GPU.
  76. │ ├──run_standalone_train_gpu.sh // shell script for standalone train on GPU.
  77. ├── eval.py // Infer API entry.
  78. ├── requirements.txt // Requirements of third party package.
  79. ├── train.py // Train API entry.
  80. ```
  81. ### [Dataset Preparation](#content)
  82. - Download the AG's News Topic Classification Dataset, DBPedia Ontology Classification Dataset and Yelp Review Polarity Dataset. Unzip datasets to any path you want.
  83. - Run the following scripts to do data preprocess and convert the original data to mindrecord for training and evaluation.
  84. ``` bash
  85. cd scripts
  86. sh creat_dataset.sh [SOURCE_DATASET_PATH] [DATASET_NAME]
  87. ```
  88. ### [Configuration File](#content)
  89. Parameters for both training and evaluation can be set in config.py. All the datasets are using same parameter name, parameters value could be changed according the needs.
  90. - Network Parameters
  91. ```text
  92. vocab_size # vocabulary size.
  93. buckets # bucket sequence length.
  94. test_buckets # test dataset bucket sequence length
  95. batch_size # batch size of input dataset.
  96. embedding_dims # The size of each embedding vector.
  97. num_class # number of labels.
  98. epoch # total training epochs.
  99. lr # initial learning rate.
  100. min_lr # minimum learning rate.
  101. warmup_steps # warm up steps.
  102. poly_lr_scheduler_power # a value used to calculate decayed learning rate.
  103. pretrain_ckpt_dir # pretrain checkpoint direction.
  104. keep_ckpt_max # Max ckpt files number.
  105. ```
  106. ### [Training Process](#content)
  107. - Running on Ascend
  108. - Start task training on a single device and run the shell script
  109. ```bash
  110. cd ./scripts
  111. sh run_standalone_train.sh [DATASET_PATH]
  112. ```
  113. - Running scripts for distributed training of FastText. Task training on multiple device and run the following command in bash to be executed in `scripts/`:
  114. ```bash
  115. cd ./scripts
  116. sh run_distributed_train.sh [DATASET_PATH] [RANK_TABLE_PATH]
  117. ```
  118. - Running on GPU
  119. - Start task training on a single device and run the shell script
  120. ```bash
  121. cd ./scripts
  122. sh run_standalone_train_gpu.sh [DATASET_PATH]
  123. ```
  124. - Running scripts for distributed training of FastText. Task training on multiple device and run the following command in bash to be executed in `scripts/`:
  125. ```bash
  126. cd ./scripts
  127. sh run_distributed_train_gpu.sh [DATASET_PATH] [NUM_OF_DEVICES]
  128. ```
  129. ### [Inference Process](#content)
  130. - Running on Ascend
  131. - Running scripts for evaluation of FastText. The commdan as below.
  132. ```bash
  133. cd ./scripts
  134. sh run_eval.sh [DATASET_PATH] [DATASET_NAME] [MODEL_CKPT]
  135. ```
  136. Note: The `DATASET_PATH` is path to mindrecord. eg. `/dataset_path/*.mindrecord`
  137. - Running on GPU
  138. - Running scripts for evaluation of FastText. The commdan as below.
  139. ```bash
  140. cd ./scripts
  141. sh run_eval_gpu.sh [DATASET_PATH] [DATASET_NAME] [MODEL_CKPT]
  142. ```
  143. Note: The `DATASET_PATH` is path to mindrecord. eg. `/dataset_path/*.mindrecord`
  144. ## [Model Description](#content)
  145. ### [Performance](#content)
  146. #### Training Performance
  147. | Parameters | Ascend | GPU |
  148. | ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
  149. | Resource | Ascend 910; OS Euler2.8 | NV SMX3 V100-32G |
  150. | uploaded Date | 12/21/2020 (month/day/year) | 1/29/2021 (month/day/year) |
  151. | MindSpore Version | 1.1.0 | 1.1.0 |
  152. | Dataset | AG's News Topic Classification Dataset | AG's News Topic Classification Dataset |
  153. | Training Parameters | epoch=5, batch_size=512 | epoch=5, batch_size=512 |
  154. | Optimizer | Adam | Adam |
  155. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  156. | outputs | probability | probability |
  157. | Speed | 10ms/step (1pcs) | 11.91ms/step(1pcs) |
  158. | Epoch Time | 2.36s (1pcs) | 2.815s(1pcs) |
  159. | Loss | 0.0067 | 0.0085 |
  160. | Params (M) | 22 | 22 |
  161. | Checkpoint for inference | 254M (.ckpt file) | 254M (.ckpt file) |
  162. | Scripts | [fasttext](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/fasttext) | [fasttext](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/fasttext) |
  163. | Parameters | Ascend | GPU |
  164. | ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
  165. | Resource | Ascend 910; OS Euler2.8 | NV SMX3 V100-32G |
  166. | uploaded Date | 11/21/2020 (month/day/year) | 1/29/2020 (month/day/year) |
  167. | MindSpore Version | 1.1.0 | 1.1.0 |
  168. | Dataset | DBPedia Ontology Classification Dataset | DBPedia Ontology Classification Dataset |
  169. | Training Parameters | epoch=5, batch_size=4096 | epoch=5, batch_size=4096 |
  170. | Optimizer | Adam | Adam |
  171. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  172. | outputs | probability | probability |
  173. | Speed | 58ms/step (1pcs) | 34.82ms/step(1pcs) |
  174. | Epoch Time | 8.15s (1pcs) | 4.87s(1pcs) |
  175. | Loss | 2.6e-4 | 0.0004 |
  176. | Params (M) | 106 | 106 |
  177. | Checkpoint for inference | 1.2G (.ckpt file) | 1.2G (.ckpt file) |
  178. | Scripts | [fasttext](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/fasttext) | [fasttext](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/fasttext) |
  179. | Parameters | Ascend | GPU |
  180. | ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
  181. | Resource | Ascend 910; OS Euler2.8 | NV SMX3 V100-32G |
  182. | uploaded Date | 11/21/2020 (month/day/year) | 1/29/2020 (month/day/year) |
  183. | MindSpore Version | 1.1.0 | 1.1.0 |
  184. | Dataset | Yelp Review Polarity Dataset | Yelp Review Polarity Dataset |
  185. | Training Parameters | epoch=5, batch_size=2048 | epoch=5, batch_size=2048 |
  186. | Optimizer | Adam | Adam |
  187. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  188. | outputs | probability | probability |
  189. | Speed | 101ms/step (1pcs) | 30.54ms/step(1pcs) |
  190. | Epoch Time | 28s (1pcs) | 8.46s(1pcs) |
  191. | Loss | 0.062 | 0.002 |
  192. | Params (M) | 103 | 103 |
  193. | Checkpoint for inference | 1.2G (.ckpt file) | 1.2G (.ckpt file) |
  194. | Scripts | [fasttext](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/fasttext) | [fasttext](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/nlp/fasttext) |
  195. #### Inference Performance
  196. | Parameters | Ascend | GPU |
  197. | ------------------- | --------------------------- | ------------------- |
  198. | Resource | Ascend 910; OS Euler2.8 | NV SMX3 V100-32G |
  199. | Uploaded Date | 12/21/2020 (month/day/year) | 1/29/2020 (month/day/year) |
  200. | MindSpore Version | 1.1.0 | 1.1.0 |
  201. | Dataset | AG's News Topic Classification Dataset | AG's News Topic Classification Dataset |
  202. | batch_size | 512 | 128 |
  203. | Epoch Time | 2.36s | 2.815s(1pcs) |
  204. | outputs | label index | label index |
  205. | Accuracy | 92.53 | 92.58 |
  206. | Model for inference | 254M (.ckpt file) | 254M (.ckpt file) |
  207. | Parameters | Ascend | GPU |
  208. | ------------------- | --------------------------- | ------------------- |
  209. | Resource | Ascend 910; OS Euler2.8 | NV SMX3 V100-32G |
  210. | Uploaded Date | 12/21/2020 (month/day/year) | 1/29/2020 (month/day/year) |
  211. | MindSpore Version | 1.1.0 | 1.1.0 |
  212. | Dataset | DBPedia Ontology Classification Dataset | DBPedia Ontology Classification Dataset |
  213. | batch_size | 4096 | 4096 |
  214. | Epoch Time | 8.15s | 4.87s |
  215. | outputs | label index | label index |
  216. | Accuracy | 98.6 | 98.49 |
  217. | Model for inference | 1.2G (.ckpt file) | 1.2G (.ckpt file) |
  218. | Parameters | Ascend | GPU |
  219. | ------------------- | --------------------------- | ------------------- |
  220. | Resource | Ascend 910; OS Euler2.8 | NV SMX3 V100-32G |
  221. | Uploaded Date | 12/21/2020 (month/day/year) | 12/29/2020 (month/day/year) |
  222. | MindSpore Version | 1.1.0 | 1.1.0 |
  223. | Dataset | Yelp Review Polarity Dataset | Yelp Review Polarity Dataset |
  224. | batch_size | 2048 | 2048 |
  225. | Epoch Time | 28s | 8.46s |
  226. | outputs | label index | label index |
  227. | Accuracy | 95.7 | 95.7 |
  228. | Model for inference | 1.2G (.ckpt file) | 1.2G (.ckpt file) |
  229. ## [Random Situation Description](#content)
  230. There only one random situation.
  231. - Initialization of some model weights.
  232. Some seeds have already been set in train.py to avoid the randomness of weight initialization.
  233. ## [Others](#others)
  234. This model has been validated in the Ascend environment and is not validated on the CPU and GPU.
  235. ## [ModelZoo HomePage](#contents)
  236. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo)