Fitting the model

We need to use the following code to fit the model:

model_1 <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 128,
validation_split = 0.2)
plot(model_1)

As shown in the preceding code, we're using train_x and train_y to fit the model, as well as 10 epochs and a batch size of 128. We are using 20% of the training data to assess the model's performance in terms of loss and accuracy values. After fitting the model, we obtain a plot for loss and accuracy, as shown in the following plot:

From the preceding plot, we can observe the following:

  • The plot for loss and accuracy shows divergence between the training and validation data after about four epochs.
  • Divergence between the training and validation data is observed for both the loss and accuracy values.
  • We won't be using this model since there is clear evidence that there's an overfitting problem.

To overcome this overfitting problem, we need to modify the preceding code so that it appears as follows:

model <- keras_model_sequential()
model %>% layer_embedding(input_dim = 500,
output_dim = 16,
input_length = 100) %>%
layer_flatten() %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dense(units = 1, activation = "sigmoid")
model %>% compile(optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("acc"))
model_2 <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 512,
validation_split = 0.2)
plot(model_2)

Looking at the preceding code, we can observe the following:

  • We're re-running the model and making only one change; that is, we're increasing the batch size to 512
  • We keep everything else the same and then fit the model using the training data

After fitting the model, the loss and accuracy values that are stored in model_2 are plotted, as shown in the following plot:

From the preceding plot, we can observe the following:

  • The loss and accuracy values show better results this time.
  • The curves for training and validation are closer to each other for both loss and accuracy.
  • In addition, the loss and accuracy values that are based on validation data don't show the severe deterioration that we had observed for the previous model, where the values for the last three epochs are flat here.
  • We were able to overcome the problem of overfitting by making minor changes to our code.

We will use this model for evaluation and prediction.

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

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