Load model, history, and test files

The following models were created using the imdb dataset. 1. Recurrent Neutral Network (RNN), 2. Long Short-Term Memory (LSTM), 3. Gated Recurrent Units (GRU), 4. Bidirectional LSTM, 5. Bidirectional GRU, 6. 1D Convnet

##Load models

rnn <- load_model_hdf5("rnn_model.h5")
lstm <- load_model_hdf5("lstm_model.h5")
gru <- load_model_hdf5("gru_model.h5")
bi_lstm <- load_model_hdf5("bi_lstm_model.h5")
bi_gru <- load_model_hdf5("bi_gru_model.h5")
conv_1d <- load_model_hdf5("conv_1d_model.h5")

##Load model histories

rnn_history <- read_rds("rnn_history.rds")
lstm_history <- read_rds("lstm_history.rds")
gru_history <- read_rds("gru_history.rds")
bi_lstm_history <- read_rds("bi_lstm_history.rds")
bi_gru_history <- read_rds("bi_gru_history.rds")
conv_1d_history <- read_rds("conv_1d_history.rds")

##Load test data Test data statistics: Number of reviews in the test set,
Number of positive reviews in the test set, and
Number of negative reviews in the test set.

x_test <- read_rds("x_test.rds")
y_test <- read_rds("y_test.rds")
num_reviews <- nrow(x_test)
cat("Number of reviews- test set", num_reviews)
## Number of reviews- test set 5000
pos_neg <- count(tibble(y_test), test_data = (y_test==1))
pos_neg[1,1] <- "Negative"
pos_neg[2,1] <- "Positive"
kable(pos_neg, caption = "Number of Positive and Negative Reviews")
Number of Positive and Negative Reviews
test_data n
Negative 2510
Positive 2490

#Model ##Model summary

summary(rnn)   
## Model: "sequential_15"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_13 (Embedding)         (None, None, 32)              160000      
## ___________________________________________________________________________
## simple_rnn_4 (SimpleRNN)         (None, 32)                    2080        
## ___________________________________________________________________________
## dense_13 (Dense)                 (None, 1)                     33          
## ===========================================================================
## Total params: 162,113
## Trainable params: 162,113
## Non-trainable params: 0
## ___________________________________________________________________________
summary(lstm)  
## Model: "sequential_16"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_14 (Embedding)         (None, None, 32)              160000      
## ___________________________________________________________________________
## lstm_5 (LSTM)                    (None, 32)                    8320        
## ___________________________________________________________________________
## dense_14 (Dense)                 (None, 1)                     33          
## ===========================================================================
## Total params: 168,353
## Trainable params: 168,353
## Non-trainable params: 0
## ___________________________________________________________________________
summary(gru)  
## Model: "sequential_18"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_16 (Embedding)         (None, None, 32)              160000      
## ___________________________________________________________________________
## gru_3 (GRU)                      (None, 32)                    6240        
## ___________________________________________________________________________
## dense_16 (Dense)                 (None, 1)                     33          
## ===========================================================================
## Total params: 166,273
## Trainable params: 166,273
## Non-trainable params: 0
## ___________________________________________________________________________
summary(bi_lstm)
## Model: "sequential_17"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_15 (Embedding)         (None, None, 32)              160000      
## ___________________________________________________________________________
## bidirectional_4 (Bidirectional)  (None, 64)                    16640       
## ___________________________________________________________________________
## dense_15 (Dense)                 (None, 1)                     65          
## ===========================================================================
## Total params: 176,705
## Trainable params: 176,705
## Non-trainable params: 0
## ___________________________________________________________________________
summary(bi_gru)
## Model: "sequential_19"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_17 (Embedding)         (None, None, 32)              160000      
## ___________________________________________________________________________
## bidirectional_5 (Bidirectional)  (None, 64)                    12480       
## ___________________________________________________________________________
## dense_17 (Dense)                 (None, 1)                     65          
## ===========================================================================
## Total params: 172,545
## Trainable params: 172,545
## Non-trainable params: 0
## ___________________________________________________________________________
summary(conv_1d)
## Model: "sequential_20"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## embedding_18 (Embedding)         (None, 300, 128)              640000      
## ___________________________________________________________________________
## conv1d_9 (Conv1D)                (None, 294, 64)               57408       
## ___________________________________________________________________________
## max_pooling1d_5 (MaxPooling1D)   (None, 58, 64)                0           
## ___________________________________________________________________________
## conv1d_10 (Conv1D)               (None, 52, 64)                28736       
## ___________________________________________________________________________
## global_max_pooling1d_4 (GlobalMa (None, 64)                    0           
## ___________________________________________________________________________
## dense_18 (Dense)                 (None, 1)                     65          
## ===========================================================================
## Total params: 726,209
## Trainable params: 726,209
## Non-trainable params: 0
## ___________________________________________________________________________

##Plot training history

plot(rnn_history)

plot(lstm_history)

plot(bi_lstm_history)

plot(gru_history)

plot(bi_gru_history)

plot(conv_1d_history)

##Performance evaluation

rnn_scores <- rnn  %>% evaluate(x_test, y_test)

lstm_scores <- lstm %>% evaluate(x_test, y_test)

gru_scores <- gru %>% evaluate(x_test, y_test)

bi_lstm_scores <- bi_lstm %>% evaluate(x_test, y_test)

bi_gru_scores <- bi_gru %>% evaluate(x_test, y_test)

conv_1d_scores <- conv_1d %>% evaluate(x_test, y_test)
rnn_pred <- rnn %>% predict_classes(x_test)
lstm_pred <- lstm %>% predict_classes(x_test)
gru_pred <-  gru %>% predict_classes(x_test)
bi_lstm_pred  <-bi_lstm %>% predict_classes(x_test)
bi_gru_pred <-bi_gru %>% predict_classes(x_test)
conv_1d_pred <-conv_1d %>% predict_classes(x_test)
rnn_table <- table(y_test, rnn_pred)
lstm_table <- table(y_test, lstm_pred)
gru_table <- table(y_test, gru_pred)
bi_lstm_table  <-table(y_test, bi_lstm_pred)
bi_gru_table <-table(y_test, bi_gru_pred)
conv_1d_table <-table(y_test, conv_1d_pred)

#Performance ##Model Summaries of acc, n_tp, n_tn, n_fp, n_fn.

model_name <- c("RNN", "LSTM", "GRU", "Bidirectional GRU", "Bidirectional LSTM", "1D Convnet")
scores <- c(rnn_scores$acc, lstm_scores$acc, gru_scores$acc, bi_lstm_scores$acc, bi_gru_scores$acc, conv_1d_scores$acc)

n_tp <- c(rnn_table[2,2], lstm_table[2,2], gru_table[2,2], bi_lstm_table[2,2], bi_gru_table[2,2], conv_1d_table[2,2])

n_tn <- c(rnn_table[1,1], lstm_table[1,1], gru_table[1,1], bi_lstm_table[1,1], bi_gru_table[1,1], conv_1d_table[1,1])

n_fp <- c(rnn_table[1,2], lstm_table[1,2], gru_table[1,2], bi_lstm_table[1,2], bi_gru_table[1,2], conv_1d_table[1,2])

n_fn <-  c(rnn_table[2,1], lstm_table[2,1], gru_table[2,1], bi_lstm_table[2,1], bi_gru_table[2,1], conv_1d_table[2,1])

summary <- tibble(model_name = model_name, acc= scores, n_tp=n_tp, n_tn=n_tn, n_fp=n_fp, n_fn=n_fn)

kable(summary, caption = "Performance Summary")
Performance Summary
model_name acc n_tp n_tn n_fp n_fn
RNN 0.7054 1759 1768 742 731
LSTM 0.8248 2188 1936 574 302
GRU 0.8164 1880 2202 308 610
Bidirectional GRU 0.8492 2201 2045 465 289
Bidirectional LSTM 0.8234 2193 1924 586 297
1D Convnet 0.8186 1935 2158 352 555

#Discussion The Bidrectional GRU model provided the greatest accuracy (84.9%) followed by the bidirectional LSTM model(82.3%) and the LSTM model (82%). The accuracy of the 1D Convnet model improved sligthly after adjusting the parameters for epoch, batch_size, and validation_split. The RNN model perfomed the worst of all six models.