开始使用免费开始使用

Implementing the validation step

Once we trained a neural network model we need to monitor its performance during training. Using PyTorch Lightning, implement the validation_step() method to calculate and log the validation loss at each epoch.

本练习是课程的一部分

Scalable AI Models with PyTorch Lightning

查看课程

练习说明

  • Compute predictions using the model on input batch.
  • Calculate validation loss using F.cross_entropy().
  • Log the validation loss with self.log() as val_loss.

交互式实操练习

通过完成这段示例代码来试试这个练习。

import torch.nn.functional as F

def validation_step(self, batch, batch_idx):
    x, y = batch
    # Compute predictions using the model
    preds = ____(x)
    # Calculate validation loss
    loss = F.____(preds, y)
    # Log the validation loss
    self.____('val_loss', loss)
编辑并运行代码