Developing a recurrent neural network model

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.

Note that the default activation function for the layer_simple_rnn layer is a hyperbolic tangent (tanh), which is an S-shaped curve where the output ranges from -1 to +1.

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.

Note that the sigmoid activation function is an S-shaped curve where the output ranges between 0 and 1.

Now, let's look at the model summary and understand how we can calculate on the number of parameters that are required.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset