LSTM Recurrent Neural Network

20 Dec 2017

Oftentimes we have text data that we want to classify. While it is possible to use a type of convolutional network, we are going to focus on a more popular option: the recurrent neural network. The key feature of recurrent neural networks is that information loops back in the network. This gives recurrent neural networks a type of memory it can use to better understand sequential data. A popular choice type of recurrent neural network is the long short-term memory (LSTM) network which allows for information to loop backwards in the network.

Preliminaries

Load Dataset On Movie Review Text

# Set the number of features we wantnumber_of_features=1000# Load data and target vector from movie review data(train_data,train_target),(test_data,test_target)=imdb.load_data(num_words=number_of_features)# Use padding or truncation to make each observation have 400 featurestrain_features=sequence.pad_sequences(train_data,maxlen=400)test_features=sequence.pad_sequences(test_data,maxlen=400)

Compule LSTM Neural Network Architecture

Train LSTM Neural Network Architecture

# Train neural networkhistory=network.fit(train_features,# Featurestrain_target,# Targetepochs=3,# Number of epochsverbose=0,# Do not print description after each epochbatch_size=1000,# Number of observations per batchvalidation_data=(test_features,test_target))# Data for evaluation