In this notebook, I implemented a recurrent neural network (Long short-term memory) using PyTorch that performs sentiment analysis.
Here I used a dataset of Amazon baby products reviews, accompanied by product names and rates. You can find it here
The architecture for this network is shown below.
The layers are as follows:
-
An embedding layer that converts our word tokens (integers) into embeddings of a specific size.
-
An LSTM layer defined by a hidden_state size and number of layers
-
A fully-connected output layer that maps the LSTM layer outputs to a desired output_size
-
A sigmoid activation layer which turns all outputs into a value 0-1; return only the last sigmoid output as the output of this network.
It is not possible to push model's state_dict
here due to its size. If you need it, feel free to contact me.
It's a CSV
file consisting of reviews of Amazon baby products. You can download it from Kaggle
. It consists of product names, reviews, and ratings associated with each. Bellow, you can see dataframe info:
Data columns (total 3 columns): | ||
---|---|---|
name | 183213 non-null | object |
review | 182702 non-null | object |
rating | 183531 non-null | int64 |
Head of the data:
name | review | rating |
---|---|---|
Planetwise Flannel | Wipes These flannel wipes are OK, but in my opinion ... | 3 |
Planetwise Wipe Pouch | it came early and was not disappointed. i love... | 5 |
Annas Dream Full Quilt with 2 Shams | Very soft and comfortable and warmer than it l... | 5 |
Stop Pacifier Sucking without tears with Thumb... | This is a product well worth the purchase. I ... | 5 |
Stop Pacifier Sucking without tears with Thumb... | All of my kids have cried non-stop when I trie... | 5 |
-
Clone the repo
git clone https://github.com/salehsargolzaee/LSTM-for-Sentiment-Analysis
-
Make sure you've installed following packages:
- PyTorch
- Pandas
- NumPy
-
Change directory to repo folder
cd path/to/repo/folder
-
Download the
amazon_baby.csv
dataset, and place it in this project's home directory, at the locationdata/amazon_baby.csv
. -
Run
jupyter notebook
jupyter notebook
-
Open
Sentiment_RNN_product_review.ipynb
Saleh Sargolzaee - LinkedIn - [email protected]
Project Link: https://github.com/salehsargolzaee/LSTM-for-Sentiment-Analysis
Give a ⭐️ if you liked the project!