In this article I’m going to build an app that can automatically detect the sentiment of an English text.
I actually tried this already in this article where I used a 1-dimensional convolutional neural network to analyze the movie review text. That approach worked quite well with a final accuracy of 86%, but unfortunately my solution started overfitting right away.
A much better way to analyze English text is by using a specialized type of recurrent neural network called an LSTM network.
All recurrent neural networks have an internal state (a type of memory) that helps them make sense of written language. But an LSTM network actually has two types of memory: long-term and short-term memory. That makes the network well-suited to process language.
So how will an LSTM network do? Will it perform better than the 1-dimensional convolutional network?
Let’s find out!
I will use the same IMDB Movie Dataset again, this is a dataset with 25,000 positive movie reviews and 25,000 negative movie reviews. The reviews look like this:
The datafile is not a text file but a binary file, this is because the movie reviews have already been preprocessed. Each word in the reviews has been converted to an index number in a dictionary, and the words have been sorted in reverse order and padded with zeroes so each review is exactly 500 numbers long.
I will build an LSTM network that reads in these 500-word sequences and then makes a prediction for each review if it is positive or negative.
Let’s get started.
I will need to build a new application from scratch:
$ dotnet new console -o LstmDemo
$ cd LstmDemo
I will also copy the dataset file imdb_data.zip into this folder because the code I’m going to type next will expect it here.
Now I’ll install the following packages:
$ dotnet add package CNTK.GPU
$ dotnet add package XPlot.Plotly
$ dotnet add package…