(dataloader, model, loss_fn)
| 158 | # We also check the model's performance against the test dataset to ensure it is learning. |
| 159 | |
| 160 | def test(dataloader, model, loss_fn): |
| 161 | size = len(dataloader.dataset) |
| 162 | num_batches = len(dataloader) |
| 163 | model.eval() |
| 164 | test_loss, correct = 0, 0 |
| 165 | with torch.no_grad(): |
| 166 | for X, y in dataloader: |
| 167 | X, y = X.to(device), y.to(device) |
| 168 | pred = model(X) |
| 169 | test_loss += loss_fn(pred, y).item() |
| 170 | correct += (pred.argmax(1) == y).type(torch.float).sum().item() |
| 171 | test_loss /= num_batches |
| 172 | correct /= size |
| 173 | print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n") |
| 174 | |
| 175 | ############################################################################## |
| 176 | # The training process is conducted over several iterations (*epochs*). During each epoch, the model learns |
no test coverage detected