How to do it...

In this section, we will work with the IMDb reviews dataset. The data preparation steps for this are the same as they were for the Sentiment classification using RNNs section. This means we can move straight onto the model-building part:

  1. Let's start by instantiating our sequential model:
model <- keras_model_sequential()
  1. Now, we add some layers to our model and print its summary:
model %>%
  layer_embedding(input_dim = 2000, output_dim = 128) %>% 
  bidirectional(layer_simple_rnn(units = 32),merge_mode = "concat") %>% 
  layer_dense(units = 1, activation = 'sigmoid')

summary(model)

The following screenshot shows the summary of the model:

  1. Let's compile and train our model:
# compile model
model %>% compile(
  loss = "binary_crossentropy",
  optimizer = "adam",
  metrics = c("accuracy")
)

# train model
model %>% fit(
  train_x,train_y,
  batch_size = 32,
  epochs = 10,
  validation_split = .2
)
  1. Let's evaluate the model and print the metrics:
scores <- model %>% evaluate(
  test_x, test_y,
  batch_size = 32
)

cat('Test score:', scores[[1]],'
')
cat('Test accuracy', scores[[2]])

The following screenshot shows the performance metric on the test data:

We get an accuracy of 75% on the test data.

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

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