MCPcopy Create free account
hub / github.com/pytorch/tutorials / test

Function test

intermediate_source/custom_function_conv_bn_tutorial.py:306–325  ·  view source on GitHub ↗
(model, device, test_loader)

Source from the content-addressed store, hash-verified

304 100. * batch_idx / len(train_loader), loss.item()))
305
306def test(model, device, test_loader):
307 model.eval()
308 test_loss = 0
309 correct = 0
310 # Use inference mode instead of no_grad, for free improved test-time performance
311 with torch.inference_mode():
312 for data, target in test_loader:
313 data, target = data.to(device), target.to(device)
314 output = model(data)
315 # sum up batch loss
316 test_loss += F.nll_loss(output, target, reduction='sum').item()
317 # get the index of the max log-probability
318 pred = output.argmax(dim=1, keepdim=True)
319 correct += pred.eq(target.view_as(pred)).sum().item()
320
321 test_loss /= len(test_loader.dataset)
322
323 print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
324 test_loss, correct, len(test_loader.dataset),
325 100. * correct / len(test_loader.dataset)))
326
327use_cuda = torch.cuda.is_available()
328device = torch.device("cuda" if use_cuda else "cpu")

Calls 1

modelFunction · 0.50

Tested by

no test coverage detected