LSTM Architecture

LSTM for Sentiment Analysis

Machine Learning
PyTorch
Python

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

Network Architecture

The architecture for this network is shown below.

Network-architecture

The layers are as follows:

  1. An embedding layer that converts our word tokens (integers) into embeddings of a specific size.

  2. An LSTM layer defined by a hidden_state size and number of layers

  3. A fully-connected output layer that maps the LSTM layer outputs to a desired output_size

  4. 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 the model's state_dict here due to its size. If you need it, feel free to contact me.

Dataset

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