Model evaluation with test data

We will use the model to obtain the loss and accuracy values from the test data using the following code:

# Loss and accuracy
model %>% evaluate(testx, testy)
$loss
[1] 2.460835
$acc
[1] 0.2508

From the preceding code, we can see that the loss and accuracy values based on the test data are 2.461 and 0.251, respectively. Both of these results are inferior to the ones we obtained based on the training data, which is usually expected. Predicting the classes and calculating the accuracy of classification for each author, as shown in the following code, would help provide further insights:

# Prediction and confusion matrix
pred1 <- model %>% predict_classes(testx)
tab1 <- table(Predicted=pred1, Actual=testy_org)
(accuracy <- 100*diag(tab1)/colSums(tab1))
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
22 28 2 2 28 14 14 20 6 28 24 8 28 8 46 84 14 36 10 50 40 12 4 22 4
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
18 54 38 12 34 46 0 52 26 48 40 26 84 46 18 24 26 10 0 46 0 4 38 0 10

The information in the confusion matrix is stored in tab1, which is used for arriving at the accuracy of correctly classifying articles from each author. The results are as follows:

An overall accuracy of about 25% for the test data already suggested significantly inferior performance based on the test data. This can also be seen in the preceding bar chart. Let's take a look at some of the observations we can make from this:

  • For the authors labeled 31, 43, 45, and 48, none of the 50 articles written by each author were correctly classified.
  • More than 80% of the articles from the authors labeled 15 and 38 were correctly classified.

From this initial example, we can see that our model classification performance needs further improvement. The differences in performance that we have observed between the training and test data also indicate the presence of an overfitting problem. Thus, we need to make changes to the model architecture to obtain a model that not only provides higher accuracy in classification performance but also shows consistent performance between the training and test data. We will explore this in the next section.

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

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