Recurrent Neural Networks (RNNs)

A RNN process sequences by iterating through the sequence elements and maintaining a state containing information relative to what it has seen so far.

Transition equation for a simple RNN:

output_t <- tanh(as.numeric((W %*% input_t) + (U %*% state_t) + b))

Following is R pseudo-code for a simple RNN layer:

Embedding layer

Previously, we have encoded text data using integers.

Another approach to feed text to our models is to use an embedding layer:

The IMDB dataset

The objective here is to classify a movie review as either positive or negative.

Recurring neural networks with an embedding layer

  • Data preparation parameters
  • Downloading the data
## 25000 train sequences
## 25000 test sequences
  • Padding the sequences
## input_train shape: 25000 500
## input_test shape: 25000 500
  • Defining the model with embedding and simple RNN layers:
## Model
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_1 (Embedding)          (None, None, 32)              320000      
## ___________________________________________________________________________
## simple_rnn_1 (SimpleRNN)         (None, 32)                    2080        
## ___________________________________________________________________________
## dense_1 (Dense)                  (None, 1)                     33          
## ===========================================================================
## Total params: 322,113
## Trainable params: 322,113
## Non-trainable params: 0
## ___________________________________________________________________________
  • Compiling the model
  • Training and validation

Stacking recurrent layers