Performing sentiment prediction using LSTM network
In this section, we will use LSTM networks to perform sentiment analysis. Along with the word itself, the LSTM network also accounts for the sequence using recurrent connections, which makes it more accurate than a traditional feed-forward neural network.
Here, we shall use the movie reviews
dataset text2vec
from the cran
package. This dataset consists of 5,000 IMDb movie reviews, where each review is tagged with a binary sentiment flag (positive or negative).
How to do it...
Here is how you can proceed with sentiment prediction using LSTM:
- Load the required packages and movie reviews dataset:
load_packages=c("text2vec","tidytext","tensorflow") lapply(load_packages, require, character.only = TRUE) data("movie_review")
- Extract the movie reviews and labels as a dataframe and matrix respectively. In movie reviews, add an additional attribute
"Sno"
denoting the review number. In the labels matrix, add an additional attribute related tonegative...