Text Classification through LSTMs
The aim of this repository is to show a baseline model for text classification by implementing a LSTM-based model coded in PyTorch. In order to provide a better understanding of the model, it will be used a Tweets dataset provided by Kaggle
If you want to delve into the details regarding how the text was pre-processed, how the sequences were generated, how the neural network was built from the LSTMCells and how the model was trained, I highly recommend reading the blog:
Text Classification with PyTorch
1. Data
As it was mentioned above, the implemented dataset is about Tweets regarding fake news. The rawdataset contains some unnecessary columns which are going to be removed in the preprocessing step, in the end, we will be working with a dataset with a head such as this:
| id | text | target |
|---|
| 1 | Our Deeds are the Reason of this #earthquake May ALLAH Forgive us all | 1 |
| 2 | SOOOO PUMPED FOR ABLAZE ???? @southridgelife | 0 |
| 3 | INEC Office in Abia Set Ablaze - http://t.co/3ImaomknnA | 1 |
| 4 | Building the perfect tracklist to life leave the streets ablaze | 0 |
This raw dataset can be found in data/tweets.csv.
2. The model
As it was already commented, the aim of this repository is to provide a base line model for text classfication. In this sense, the model is based on a two-stacked LSTM layers followed by two linear layers. The dataset is preprocessed through a tokens-based technique, then tokens are associated to an embedding layer. The following image describes the pipeline of the model.
3. Dependencies
This model was developed under these specified versions:
torch==1.0.1.post2
torchtext==0.6.0
tensorflow==1.12.0
Keras==2.0.0
numpy==1.15.4
pandas==1.0.3
4. How to use it
The model can be executed easily by typing:
python main.py
You can define some hyperparameters manually, such as:
main.py [-h] [--epochs EPOCHS] [--learning_rate LEARNING_RATE]
[--hidden_dim HIDDEN_DIM] [--lstm_layers LSTM_LAYERS]
[--batch_size BATCH_SIZE] [--test_size TEST_SIZE]
[--max_len MAX_LEN] [--max_words MAX_WORDS]
5. Demo