開始使用免費開始

模型評估

訓練迴圈已經就緒,你已將模型訓練 1000 個 epoch,並以 net 提供給你。你也用與先前 train_dataloader 完全相同的方式建立了 test_dataloader——只是改為從測試目錄讀取資料,而不是訓練目錄。

現在你可以在測試資料上評估模型。為此,你需要撰寫評估迴圈,遍歷測試資料的每個批次,取得每個批次的模型預測,並計算其準確率分數。一起來完成吧!

本練習屬於課程

Intermediate Deep Learning with PyTorch

檢視課程

練習說明

  • 將評估指標設為二元分類的 Accuracy,並指定給 acc
  • 對於每個測試批次,取得模型的輸出並指定給 outputs
  • 迴圈結束後,計算整體測試準確率並指定給 test_accuracy

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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}")
編輯並執行程式碼