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

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # fastNLP
  2. [![Build Status](https://travis-ci.org/fastnlp/fastNLP.svg?branch=master)](https://travis-ci.org/fastnlp/fastNLP)
  3. [![codecov](https://codecov.io/gh/fastnlp/fastNLP/branch/master/graph/badge.svg)](https://codecov.io/gh/fastnlp/fastNLP)
  4. fastNLP is a modular Natural Language Processing system based on PyTorch, for fast development of NLP tools. It divides the NLP model based on deep learning into different modules. These modules fall into 4 categories: encoder, interaction, aggregation and decoder, while each category contains different implemented modules. Encoder modules encode the input into some abstract representation, interaction modules make the information in the representation interact with each other, aggregation modules aggregate and reduce information, and decoder modules decode the representation into the output. Most current NLP models could be built on these modules, which vastly simplifies the process of developing NLP models. The architecture of fastNLP is as the figure below:
  5. ![](https://github.com/fastnlp/fastNLP/raw/master/fastnlp-architecture.pdf)
  6. ## Requirements
  7. - numpy>=1.14.2
  8. - torch==0.4.0
  9. - torchvision>=0.1.8
  10. ## Resources
  11. - [Documentation](https://github.com/fastnlp/fastNLP)
  12. - [Source Code](https://github.com/fastnlp/fastNLP)
  13. ## Example
  14. ### Basic Usage
  15. A typical fastNLP routine is composed of four phases: loading dataset, pre-processing data, constructing model and training model.
  16. ```python
  17. from fastNLP.models.base_model import BaseModel
  18. from fastNLP.modules import encoder
  19. from fastNLP.modules import aggregation
  20. from fastNLP.loader.dataset_loader import ClassDatasetLoader
  21. from fastNLP.loader.preprocess import ClassPreprocess
  22. from fastNLP.core.trainer import ClassificationTrainer
  23. from fastNLP.core.inference import ClassificationInfer
  24. class ClassificationModel(BaseModel):
  25. """
  26. Simple text classification model based on CNN.
  27. """
  28. def __init__(self, class_num, vocab_size):
  29. super(ClassificationModel, self).__init__()
  30. self.embed = encoder.Embedding(nums=vocab_size, dims=300)
  31. self.conv = encoder.Conv(
  32. in_channels=300, out_channels=100, kernel_size=3)
  33. self.pool = aggregation.MaxPool()
  34. self.output = encoder.Linear(input_size=100, output_size=class_num)
  35. def forward(self, x):
  36. x = self.embed(x) # [N,L] -> [N,L,C]
  37. x = self.conv(x) # [N,L,C_in] -> [N,L,C_out]
  38. x = self.pool(x) # [N,L,C] -> [N,C]
  39. x = self.output(x) # [N,C] -> [N, N_class]
  40. return x
  41. data_dir = 'data' # directory to save data and model
  42. train_path = 'test/data_for_tests/text_classify.txt' # training set file
  43. # load dataset
  44. ds_loader = ClassDatasetLoader("train", train_path)
  45. data = ds_loader.load()
  46. # pre-process dataset
  47. pre = ClassPreprocess(data_dir)
  48. vocab_size, n_classes = pre.process(data, "data_train.pkl")
  49. # construct model
  50. model_args = {
  51. 'num_classes': n_classes,
  52. 'vocab_size': vocab_size
  53. }
  54. model = ClassificationModel(class_num=n_classes, vocab_size=vocab_size)
  55. # train model
  56. train_args = {
  57. "epochs": 20,
  58. "batch_size": 50,
  59. "pickle_path": data_dir,
  60. "validate": False,
  61. "save_best_dev": False,
  62. "model_saved_path": None,
  63. "use_cuda": True,
  64. "learn_rate": 1e-3,
  65. "momentum": 0.9}
  66. trainer = ClassificationTrainer(train_args)
  67. trainer.train(model)
  68. # predict using model
  69. seqs = [x[0] for x in data]
  70. infer = ClassificationInfer(data_dir)
  71. labels_pred = infer.predict(model, seqs)
  72. ```
  73. ## Installation
  74. ### Cloning From GitHub
  75. If you just want to use fastNLP, use:
  76. ```shell
  77. git clone https://github.com/fastnlp/fastNLP
  78. cd fastNLP
  79. ```
  80. ### PyTorch Installation
  81. Visit the [PyTorch official website] for installation instructions based on your system. In general, you could use:
  82. ```shell
  83. # using conda
  84. conda install pytorch torchvision -c pytorch
  85. # or using pip
  86. pip3 install torch torchvision
  87. ```
  88. ## Project Structure
  89. ```
  90. FastNLP
  91. ├── docs
  92. │   └── quick_tutorial.md
  93. ├── fastNLP
  94. │   ├── action
  95. │   │   ├── action.py
  96. │   │   ├── inference.py
  97. │   │   ├── __init__.py
  98. │   │   ├── metrics.py
  99. │   │   ├── optimizer.py
  100. │   │   ├── README.md
  101. │   │   ├── tester.py
  102. │   │   └── trainer.py
  103. │   ├── fastnlp.py
  104. │   ├── __init__.py
  105. │   ├── loader
  106. │   │   ├── base_loader.py
  107. │   │   ├── config_loader.py
  108. │   │   ├── dataset_loader.py
  109. │   │   ├── embed_loader.py
  110. │   │   ├── __init__.py
  111. │   │   ├── model_loader.py
  112. │   │   └── preprocess.py
  113. │   ├── models
  114. │   │   ├── base_model.py
  115. │   │   ├── char_language_model.py
  116. │   │   ├── cnn_text_classification.py
  117. │   │   ├── __init__.py
  118. │   │   └── sequence_modeling.py
  119. │   ├── modules
  120. │   │   ├── aggregation
  121. │   │   │   ├── attention.py
  122. │   │   │   ├── avg_pool.py
  123. │   │   │   ├── __init__.py
  124. │   │   │   ├── kmax_pool.py
  125. │   │   │   ├── max_pool.py
  126. │   │   │   └── self_attention.py
  127. │   │   ├── decoder
  128. │   │   │   ├── CRF.py
  129. │   │   │   └── __init__.py
  130. │   │   ├── encoder
  131. │   │   │   ├── char_embedding.py
  132. │   │   │   ├── conv_maxpool.py
  133. │   │   │   ├── conv.py
  134. │   │   │   ├── embedding.py
  135. │   │   │   ├── __init__.py
  136. │   │   │   ├── linear.py
  137. │   │   │   ├── lstm.py
  138. │   │   │   ├── masked_rnn.py
  139. │   │   │   └── variational_rnn.py
  140. │   │   ├── __init__.py
  141. │   │   ├── interaction
  142. │   │   │   └── __init__.py
  143. │   │   ├── other_modules.py
  144. │   │   └── utils.py
  145. │   └── saver
  146. │   ├── base_saver.py
  147. │   ├── __init__.py
  148. │   ├── logger.py
  149. │   └── model_saver.py
  150. ├── LICENSE
  151. ├── README.md
  152. ├── reproduction
  153. │   ├── Char-aware_NLM
  154. │   │  
  155. │   ├── CNN-sentence_classification
  156. │   │  
  157. │   ├── HAN-document_classification
  158. │   │  
  159. │   └── LSTM+self_attention_sentiment_analysis
  160. |
  161. ├── requirements.txt
  162. ├── setup.py
  163. └── test
  164. ├── data_for_tests
  165. │   ├── charlm.txt
  166. │   ├── config
  167. │   ├── cws_test
  168. │   ├── cws_train
  169. │   ├── people_infer.txt
  170. │   └── people.txt
  171. ├── test_charlm.py
  172. ├── test_cws.py
  173. ├── test_fastNLP.py
  174. ├── test_loader.py
  175. ├── test_seq_labeling.py
  176. ├── test_tester.py
  177. └── test_trainer.py
  178. ```

一款轻量级的自然语言处理(NLP)工具包,目标是减少用户项目中的工程型代码,例如数据处理循环、训练循环、多卡运行等