| 80 | |
| 81 | |
| 82 | def test(model, device, test_loader): |
| 83 | model.eval() |
| 84 | test_loss = 0 |
| 85 | correct = 0 |
| 86 | with torch.no_grad(): |
| 87 | for data, target in test_loader: |
| 88 | data, target = data.to(device), target.to(device) |
| 89 | output = model(data) |
| 90 | test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss |
| 91 | pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability |
| 92 | correct += pred.eq(target.view_as(pred)).sum().item() |
| 93 | |
| 94 | test_loss /= len(test_loader.dataset) |
| 95 | |
| 96 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( |
| 97 | test_loss, correct, len(test_loader.dataset), |
| 98 | 100. * correct / len(test_loader.dataset))) |
| 99 | |
| 100 | |
| 101 | def main(): |