Skip to content

Instantly share code, notes, and snippets.

@napoler
Created September 29, 2021 02:17
Show Gist options
  • Save napoler/4992697fce159c90aa0af28afe01d066 to your computer and use it in GitHub Desktop.
Save napoler/4992697fce159c90aa0af28afe01d066 to your computer and use it in GitHub Desktop.
Created with Copy to Gist
# CASE 1: A single test dataset
def test_step(self, batch, batch_idx):
x, y = batch
# implement your own
out = self(x)
loss = self.loss(out, y)
# log 6 example images
# or generated text... or whatever
sample_imgs = x[:6]
grid = torchvision.utils.make_grid(sample_imgs)
self.logger.experiment.add_image('example_images', grid, 0)
# calculate acc
labels_hat = torch.argmax(out, dim=1)
test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
# log the outputs!
self.log_dict({'test_loss': loss, 'test_acc': test_acc})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment