Get startedGet started for free

Model evaluation

With the training loop sorted out, you have trained the model for 1000 epochs, and it is available to you as net. You have also set up a test_dataloader in exactly the same way as you did with train_dataloader before—just reading the data from the test rather than the train directory.

You can now evaluate the model on test data. To do this, you will need to write the evaluation loop to iterate over the batches of test data, get the model's predictions for each batch, and calculate the accuracy score for it. Let's do it!

This exercise is part of the course

Intermediate Deep Learning with PyTorch

View Course

Exercise instructions

  • Set up the evaluation metric as Accuracy for binary classification and assign it to acc.
  • For each batch of test data, get the model's outputs and assign them to outputs.
  • After the loop, compute the total test accuracy and assign it to test_accuracy.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

import torch
from torchmetrics import Accuracy

# Set up binary accuracy metric
acc = ____

net.eval()
with torch.no_grad():
    for features, labels in dataloader_test:
        # Get predicted probabilities for test data batch
        outputs = ____
        preds = (outputs >= 0.5).float()
        acc(preds, labels.view(-1, 1))

# Compute total test accuracy
test_accuracy = ____
print(f"Test accuracy: {test_accuracy}")
Edit and Run Code