| 370 | # test set to estimate the generalization error: |
| 371 | |
| 372 | def test_accuracy(net, device="cpu", data_dir=None): |
| 373 | _trainset, testset = load_data(data_dir) |
| 374 | |
| 375 | testloader = torch.utils.data.DataLoader( |
| 376 | testset, batch_size=4, shuffle=False, num_workers=2 |
| 377 | ) |
| 378 | |
| 379 | correct = 0 |
| 380 | total = 0 |
| 381 | with torch.no_grad(): |
| 382 | for data in testloader: |
| 383 | image_batch, labels = data |
| 384 | image_batch, labels = image_batch.to(device), labels.to(device) |
| 385 | outputs = net(image_batch) |
| 386 | _, predicted = torch.max(outputs.data, 1) |
| 387 | total += labels.size(0) |
| 388 | correct += (predicted == labels).sum().item() |
| 389 | |
| 390 | return correct / total |
| 391 | |
| 392 | ###################################################################### |
| 393 | # Configure and run Ray Tune |