使用 Torchmetrics 評估模型準確率
評估模型表現有多好是關鍵,特別是在準備部署之前!讓我們把使用 Torchmetrics 計算準確率順暢地整合進 validation_step()。別忘了把結果記錄下來,這樣你就能輕鬆追蹤模型的進展。
本練習屬於課程
Scalable AI Models with PyTorch Lightning
練習說明
- 從
torchmetrics匯入Accuracy。 - 在
__init__()中實例化準確率評估指標。 - 在
validation_step()中計算準確率,並以'val_acc'名稱記錄(log)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import relevant metric
from torchmetrics import ____
import lightning.pytorch as pl
class ClassifierModel(pl.LightningModule):
def __init__(self):
super().__init__()
# Instantiate accuracy metric
self.accuracy = ____()
def validation_step(self, batch, batch_idx):
x, y = batch
preds = self(x)
# Calculate accuracy and log it as val_acc
acc = self.____(preds, y)
self.log(____, acc)