Training RNNs for sentiment analysis
In this section, we will train an RNN model using PyTorch for a text classification task – sentiment analysis. In this task, the model takes in a piece of text – a sequence of words – as input and outputs either 1
(meaning positive sentiment) or 0
(negative sentiment). For this binary classification task involving sequential data, we will use a unidirectional single-layer RNN.
Before training the model, we will manually process the textual data and convert it into a usable numeric form. Upon training the model, we will test it on some sample texts. We will demonstrate the use of various PyTorch functionalities to efficiently perform this task. The code for this exercise can be found at https://github.com/PacktPublishing/Mastering-PyTorch/blob/master/Chapter04/rnn.ipynb.
Loading and preprocessing the text dataset
For this exercise, we will need to import a few dependencies:
- First, execute the following
import...