IniziaInizia gratis

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!

Questo esercizio fa parte del corso

Intermediate Deep Learning with PyTorch

Visualizza il corso

Istruzioni dell'esercizio

  • 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.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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}")
Modifica ed esegui il codice