|
| 1 | +# [PYTORCH] Hierarchical Attention Networks for Document Classification |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +Here is my pytorch implementation of the model described in the paper **Hierarchical Attention Networks for Document Classification** [paper](https://www.cs.cmu.edu/%7Ediyiy/docs/naacl16.pdf). |
| 6 | + |
| 7 | +<p align="center"> |
| 8 | + <img src="demo/video.gif"><br/> |
| 9 | + <i>An example of app demo for my model's output for Dbpedia dataset.</i> |
| 10 | +</p> |
| 11 | + |
| 12 | +<p align="center"> |
| 13 | + <img src="demo/output.gif"><br/> |
| 14 | + <i>An example of my model's performance for Dbpedia dataset.</i> |
| 15 | +</p> |
| 16 | + |
| 17 | +## How to use my code |
| 18 | + |
| 19 | +With my code, you can: |
| 20 | +* **Train your model with any dataset** |
| 21 | +* **Given either my trained model or yours, you could evaluate any test dataset whose have the same set of classes** |
| 22 | +* **Run a simple web app for testing purpose** |
| 23 | + |
| 24 | +## Requirements: |
| 25 | + |
| 26 | +* **python 3.6** |
| 27 | +* **pytorch 0.4** |
| 28 | +* **tensorboard** |
| 29 | +* **tensorboardX** (This library could be skipped if you do not use SummaryWriter) |
| 30 | +* **numpy** |
| 31 | + |
| 32 | +## Datasets: |
| 33 | + |
| 34 | +Statistics of datasets I used for experiments. These datasets could be download from [link](https://drive.google.com/drive/u/0/folders/0Bz8a_Dbh9Qhbfll6bVpmNUtUcFdjYmF2SEpmZUZUcVNiMUw1TWN6RDV3a0JHT3kxLVhVR2M) |
| 35 | + |
| 36 | +| Dataset | Classes | Train samples | Test samples | |
| 37 | +|------------------------|:---------:|:---------------:|:--------------:| |
| 38 | +| AG’s News | 4 | 120 000 | 7 600 | |
| 39 | +| Sogou News | 5 | 450 000 | 60 000 | |
| 40 | +| DBPedia | 14 | 560 000 | 70 000 | |
| 41 | +| Yelp Review Polarity | 2 | 560 000 | 38 000 | |
| 42 | +| Yelp Review Full | 5 | 650 000 | 50 000 | |
| 43 | +| Yahoo! Answers | 10 | 1 400 000 | 60 000 | |
| 44 | +| Amazon Review Full | 5 | 3 000 000 | 650 000 | |
| 45 | +| Amazon Review Polarity | 2 | 3 600 000 | 400 000 | |
| 46 | + |
| 47 | +Additionally, I also use word2vec pre-trained models, taken from GLOVE, which you could download from [link](https://nlp.stanford.edu/projects/glove/). I run experiments with all 4 word2vec files (50d, 100d, 200d and 300d). You could easily switch to other common word2vec models, like the one provided in FastText [link](https://fasttext.cc/docs/en/crawl-vectors.html) |
| 48 | +In the paper, it is said that a pre-trained word2vec is used. However, to the best of my knowledge, at least in pytorch, there is no implementation on github using it. In all HAN github repositories I have seen so far, a default embedding layer |
| 49 | +was used, without loading pre-trained word2vec model. I admit that we could still train HAN model without any pre-trained word2vec model. However, to serve the purpose of re-implementing origin model, in all experiments, as mentioned above, I used 1 out of 4 pre-trained word2vec models as initilization for embedding layer. |
| 50 | + |
| 51 | +## Setting: |
| 52 | + |
| 53 | +During my experiments, I found out that given different datasets and different embedding layer's dimension, some combinations of batch size and learning rate yield better performance (faster convergence and higher accuracy) than others. Particularly in some cases, if you set wrong values for these 2 very important parameters, your model will never converge. Detail setting for each experiments will be shown in **Experiments** part. |
| 54 | +I have not set a fixed number of epoches for each experiment. Instead, I apply early stopping technique, to stop training phase after test loss has not been improved for **n** epoches. |
| 55 | + |
| 56 | +## Training |
| 57 | + |
| 58 | +If you want to train a model with default parameters, you could run: |
| 59 | +- **python train.py** |
| 60 | + |
| 61 | +If you want to train a model with your preference parameters, like optimizer and learning rate, you could run: |
| 62 | +- **python train.py --batch_size batch_size --lr learning_rate**: For example, python train.py --batch_size 512 --lr 0.01 |
| 63 | + |
| 64 | +If you want to train a model with your preference word2vec model, you could run: |
| 65 | +- **python train.py --word2vec_path path/to/your/word2vec** |
| 66 | + |
| 67 | +## Test |
| 68 | + |
| 69 | +For testing a trained model with your test file, please run the following command: |
| 70 | +- **python test.py --word2vec_path path/to/your/word2vec**, with the word2vec file is the same as the one you use in training phase. |
| 71 | + |
| 72 | +You could find some trained models I have trained in [link](https://drive.google.com/open?id=1A50PDQMm0THnU6QDxOEsvKqH-ZTxmGpT) |
| 73 | + |
| 74 | +## Experiments: |
| 75 | + |
| 76 | +Results for test set are presented as follows: A(B/C): |
| 77 | +- **A** is accuracy. |
| 78 | +- **B** is learning rate used. |
| 79 | +- **C** is batch size. |
| 80 | + |
| 81 | +Each experiment is run over 10 epochs. |
| 82 | + |
| 83 | +| GLOVE word2vec| 50 | 100 | 200 | 300 | |
| 84 | +|:---------------:|:------------------:|:------------------:|:------------------:|:------------------:| |
| 85 | +| ag_news | updated soon | updated soon | updated soon | updated soon | |
| 86 | +| sogu_news | updated soon | updated soon | updated soon | updated soon | |
| 87 | +| db_pedia | updated soon | updated soon | updated soon | updated soon | |
| 88 | +| yelp_polarity | updated soon | updated soon | updated soon | updated soon | |
| 89 | +| yelp_review | updated soon | updated soon | updated soon | updated soon | |
| 90 | +| yahoo_answer | updated soon | updated soon | updated soon | updated soon | |
| 91 | +| amazon_review | updated soon | updated soon | updated soon | updated soon | |
| 92 | +|amazon_polarity| updated soon | updated soon | updated soon | updated soon | |
| 93 | + |
| 94 | +The training/test loss/accuracy curves for each dataset's experiments (with the order from left to right, top to bottom is 50d, 100d, 200d and 300d word2vec) are shown below: |
| 95 | + |
| 96 | +- **ag_news** |
| 97 | + |
| 98 | +<img src="demo/agnews_50.png" width="420"> <img src="demo/agnews_100.png" width="420"> |
| 99 | +<img src="demo/agnews_200.png" width="420"> <img src="demo/agnews_300.png" width="420"> |
| 100 | + |
| 101 | +- **db_pedia** |
| 102 | + |
| 103 | +<img src="demo/dbpedia_50.png" width="420"> <img src="demo/dbpedia_100.png" width="420"> |
| 104 | +<img src="demo/dbpedia_200.png" width="420"> <img src="demo/dbpedia_300.png" width="420"> |
| 105 | + |
| 106 | +- **yelp_polarity** |
| 107 | + |
| 108 | +<img src="demo/yelpreviewpolarity_50.png" width="420"> <img src="demo/yelpreviewpolarity_100.png" width="420"> |
| 109 | +<img src="demo/yelpreviewpolarity_200.png" width="420"> <img src="demo/empty.png" width="420"> |
| 110 | + |
| 111 | +- **yelp_review** |
| 112 | + |
| 113 | +<img src="demo/yelpreviewfull_50.png" width="420"> <img src="demo/empty.png" width="420"> |
| 114 | +<img src="demo/empty.png" width="420"> <img src="demo/yelpreviewfull_300.png" width="420"> |
| 115 | + |
| 116 | +- **Yahoo! Answers** |
| 117 | + |
| 118 | +<img src="demo/yahoo_50.png" width="420"> <img src="demo/yahoo_100.png" width="420"> |
| 119 | +<img src="demo/yahoo_200.png" width="420"> <img src="demo/yahoo_300.png" width="420"> |
| 120 | + |
| 121 | +- **amazon_review** |
| 122 | + |
| 123 | +<img src="demo/amazonreviewfull_50.png" width="420"> <img src="demo/empty.png" width="420"> |
| 124 | +<img src="demo/amazonreviewfull_200.png" width="420"> <img src="demo/empty.png" width="420"> |
| 125 | + |
| 126 | +- **amazon_polarity** |
| 127 | + |
| 128 | +<img src="demo/amazonreviewpolarity_50.png" width="420"> <img src="demo/amazonreviewpolarity_100.png" width="420"> |
| 129 | +<img src="demo/empty.png" width="420"> <img src="demo/amazonreviewpolarity_50.png" width="420"> |
| 130 | + |
| 131 | +There are some experiments I have not had time to train. For such experiments, statistics as well as loss/accuracy visualization are empty. Additionally, there are some other experiments, I can not wait until they are finished, hence I stopped training phase before it should be . You could see whether a model was stopped by early stopping technique or by me by looking at the test loss curve, if the loss is not improved for 5 consecutive epoches, it is the former case. Othewise, if the loss is still going down, it is the latter case. When I have time, I will complete the incomplete experiments, and update results here. |
| 132 | + |
| 133 | +After completing training phase, you could see model's parameters you have set, accuracy, loss and confusion matrix for test set at the end of each epoch at **root_folder/trained_models/logs.txt**. One example is shown below: |
| 134 | + |
| 135 | +<p align="center"> |
| 136 | + <img src="demo/output.png"><br/> |
| 137 | + <i>An example of logs.txt for Dbpedia dataset.</i> |
| 138 | +</p> |
| 139 | + |
| 140 | +## Demo: |
| 141 | + |
| 142 | +I wrote a simple web which is suitable for quick test with any input text. In order to use the app, you could follow the following steps: |
| 143 | + |
| 144 | +- **Step 1**: Run the script app.py |
| 145 | +<img src="demo/1.png" width="800"> |
| 146 | + |
| 147 | +- **Step 2**: Web interface |
| 148 | +<img src="demo/2.png" width="800"> |
| 149 | + |
| 150 | +- **Step 3**: Select trained model |
| 151 | +<img src="demo/3.png" width="800"> |
| 152 | + |
| 153 | +- **Step 4**: Select word2vec model |
| 154 | +<img src="demo/4.png" width="800"> |
| 155 | + |
| 156 | +- **Step 5 (Optional)**: Select file containing classes (one class per line) |
| 157 | +<img src="demo/5.png" width="800"> |
| 158 | + |
| 159 | +- **Step 6**: After all necessary files are selected, press submit button |
| 160 | +<img src="demo/6.png" width="800"> |
| 161 | + |
| 162 | +- **Step 7**: You could paste any text to the textbox |
| 163 | +<img src="demo/7.png" width="800"> |
| 164 | + |
| 165 | +- **Step 8**: A sample text |
| 166 | +<img src="demo/8.png" width="800"> |
| 167 | + |
| 168 | +- **Step 9**: After submit button pressed, predicted category and probability are shown |
| 169 | +<img src="demo/9.png" width="800"> |
0 commit comments