| 51 | |
| 52 | |
| 53 | def test(model, device, test_loader): |
| 54 | model.eval() |
| 55 | test_loss = 0 |
| 56 | correct = 0 |
| 57 | with torch.no_grad(): |
| 58 | for data, target in test_loader: |
| 59 | data, target = data.to(device), target.to(device) |
| 60 | output = model(data) |
| 61 | test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss |
| 62 | pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability |
| 63 | correct += pred.eq(target.view_as(pred)).sum().item() |
| 64 | |
| 65 | test_loss /= len(test_loader.dataset) |
| 66 | |
| 67 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( |
| 68 | test_loss, correct, len(test_loader.dataset), |
| 69 | 100. * correct / len(test_loader.dataset))) |
| 70 | |
| 71 | |
| 72 | def main(): |