Evaluating RNN classification models
The team at PyBooks now wants you to evaluate the RNN model you created and ran using the Newsgroup dataset. Recall, the goal was to classify the articles into one of three categories:
rec.autos
, sci.med
, and comp.graphics
.
The model was trained and you printed epoch and loss for each model.
Use torchmetrics
to evaluate various metrics for your model. The following has been loaded for : Accuracy
, Precision
, Recall
, F1Score
.
An instance of rnn_model
trained in the previous exercise in preloaded for you, too.
This exercise is part of the course
Deep Learning for Text with PyTorch
Exercise instructions
- Create an instance of each metric for multi-class classification with
num_classes
equal to the number of categories. - Generate the predictions for the
rnn_model
using the test dataX_test_seq
. - Calculate the metrics using the predicted classes and the true labels.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create an instance of the metrics
accuracy = Accuracy(task="multiclass", ____)
precision = Precision(____, num_classes=____)
recall = Recall(task=____, num_classes=____)
f1 = F1Score(____, ____)
# Generate the predictions
outputs = ____(X_test_seq)
_, predicted = ____.____(outputs, 1)
# Calculate the metrics
accuracy_score = accuracy(____, y_test_seq)
precision_score = precision(____, y_test_seq)
recall_score = recall(____, y_test_seq)
f1_score = f1(____, y_test_seq)
print("RNN Model - Accuracy: {}, Precision: {}, Recall: {}, F1 Score: {}".format(accuracy_score, precision_score, recall_score, f1_score))