|
| 1 | +# A Text Classification Usage Example for pip users |
| 2 | + |
| 3 | +## Intro |
| 4 | + |
| 5 | +In this tutorial, we demonstrate a text classification task with a |
| 6 | +demo mock dataset **for users install by pip**. |
| 7 | + |
| 8 | +A complete process contains following steps: |
| 9 | + |
| 10 | +- Prepare the data set. |
| 11 | +- Develop custom modules (optional). |
| 12 | +- Set the config file. |
| 13 | +- Train a model. |
| 14 | +- Export a model |
| 15 | + |
| 16 | +Please clone our demo repository: |
| 17 | + |
| 18 | +```bash |
| 19 | +git clone --depth 1 https://github.com/applenob/delta_demo.git |
| 20 | +cd ./delta_demo |
| 21 | +``` |
| 22 | + |
| 23 | +## A quick review for installation |
| 24 | + |
| 25 | +If you haven't install `delta-nlp`, please: |
| 26 | + |
| 27 | +```bash |
| 28 | +pip install delta-nlp |
| 29 | +``` |
| 30 | + |
| 31 | +**Requirements**: You need `tensorflow==2.0.0` and `python==3.6` in |
| 32 | +MacOS or Linux. |
| 33 | + |
| 34 | +## Prepare the Data Set |
| 35 | + |
| 36 | +run the script: |
| 37 | + |
| 38 | +``` |
| 39 | +./gen_data.sh |
| 40 | +``` |
| 41 | + |
| 42 | +The generated data are in directory: `data`. |
| 43 | + |
| 44 | +The generated data for text classification should be in the standard format for text classification, which is "label\tdocument". |
| 45 | + |
| 46 | +## Develop custom modules (optional) |
| 47 | + |
| 48 | +Please make sure we don't have modules you need before you decide to |
| 49 | +develop your own modules. |
| 50 | + |
| 51 | +```python |
| 52 | +@registers.model.register |
| 53 | +class TestHierarchicalAttentionModel(HierarchicalModel): |
| 54 | + """Hierarchical text classification model with attention.""" |
| 55 | + |
| 56 | + def __init__(self, config, **kwargs): |
| 57 | + super().__init__(config, **kwargs) |
| 58 | + |
| 59 | + logging.info("Initialize HierarchicalAttentionModel...") |
| 60 | + |
| 61 | + self.vocab_size = config['data']['vocab_size'] |
| 62 | + self.num_classes = config['data']['task']['classes']['num_classes'] |
| 63 | + self.use_true_length = config['model'].get('use_true_length', False) |
| 64 | + if self.use_true_length: |
| 65 | + self.split_token = config['data']['split_token'] |
| 66 | + self.padding_token = utils.PAD_IDX |
| 67 | +``` |
| 68 | + |
| 69 | +You need to register this module file path in the config file |
| 70 | +`config/han-cls.yml` (relative to the current work directory). |
| 71 | + |
| 72 | +```yml |
| 73 | +custom_modules: |
| 74 | + - "test_model.py" |
| 75 | +``` |
| 76 | +
|
| 77 | +## Set the Config File |
| 78 | +
|
| 79 | +The config file of this example is `config/han-cls.yml` |
| 80 | + |
| 81 | +In the config file, we set the task to be `TextClsTask` and the model to be `TestHierarchicalAttentionModel`. |
| 82 | + |
| 83 | +### Config Details |
| 84 | + |
| 85 | +The config is composed by 3 parts: `data`, `model`, `solver`. |
| 86 | + |
| 87 | +Data related configs are under `data`. |
| 88 | +You can set the data path (including training set, dev set and test set). |
| 89 | +The data process configs can also be found here (mainly under `task`). |
| 90 | +For example, we set `use_dense: false` since no dense input was used here. |
| 91 | +We set `language: chinese` since it's a Chinese text. |
| 92 | + |
| 93 | +Model parameters are under `model`. The most important config here is |
| 94 | +`name: TestHierarchicalAttentionModel`, which specifies the model to |
| 95 | +use. Detail structure configs are under `net->structure`. Here, the |
| 96 | +`max_sen_len` is 32 and `max_doc_len` is 32. |
| 97 | + |
| 98 | +The configs under `solver` are used by solver class, including training optimizer, evaluation metrics and checkpoint saver. |
| 99 | +Here the class is `RawSolver`. |
| 100 | + |
| 101 | +## Train a Model |
| 102 | + |
| 103 | +After setting the config file, you are ready to train a model. |
| 104 | + |
| 105 | +``` |
| 106 | +delta --cmd train_and_eval --config config/han-cls.yml |
| 107 | +``` |
| 108 | +
|
| 109 | +The argument `cmd` tells the platform to train a model and also evaluate |
| 110 | +the dev set during the training process. |
| 111 | +
|
| 112 | +After enough steps of training, you would find the model checkpoints have been saved to the directory set by `saver->model_path`, which is `exp/han-cls/ckpt` in this case. |
| 113 | +
|
| 114 | +## Export a Model |
| 115 | +
|
| 116 | +If you would like to export a specific checkpoint to be exported, please set `infer_model_path` in config file. Otherwise, platform will simply find the newest checkpoint under the directory set by `saver->model_path`. |
| 117 | +
|
| 118 | +``` |
| 119 | +delta --cmd export_model --config/han-cls.yml |
| 120 | +``` |
| 121 | +
|
| 122 | +The exported models are in the directory set by config |
| 123 | +`service->model_path`, which is `exp/han-cls/service` here. |
| 124 | +
|
0 commit comments