In this section, we will develop the architecture for the recurrent neural network and compile it. Let's look at the following code:
# Model architecture
model <- keras_model_sequential()
model %>%
layer_embedding(input_dim = 500, output_dim = 32) %>%
layer_simple_rnn(units = 8) %>%
layer_dense(units = 1, activation = "sigmoid")
We start by initializing the model using the keras_model_sequential function. Then, we add embedding and simple recurrent neural network (RNN) layers. For the embedding layer, we specify input_dim to be 500, which is the same as the number of most frequent words that we had specified earlier. The next layer is a simple RNN layer, with the number of hidden units specified as 8.
The last dense layer has one unit to capture movie review sentiment (positive or negative) with the activation function sigmoid. When an output lies between 0 and 1, as in this case, it is convenient for interpretation as it can be thought of as a probability.
Now, let's look at the model summary and understand how we can calculate on the number of parameters that are required.