| 209 | |
| 210 | |
| 211 | def test(model, device, test_loader): |
| 212 | model.eval() |
| 213 | test_loss = 0 |
| 214 | correct = 0 |
| 215 | |
| 216 | # we aren't using `TripletLoss` as the MNIST dataset is simple, so `BCELoss` can do the trick. |
| 217 | criterion = nn.BCELoss() |
| 218 | |
| 219 | with torch.no_grad(): |
| 220 | for (images_1, images_2, targets) in test_loader: |
| 221 | images_1, images_2, targets = images_1.to(device), images_2.to(device), targets.to(device) |
| 222 | outputs = model(images_1, images_2).squeeze() |
| 223 | test_loss += criterion(outputs, targets).sum().item() # sum up batch loss |
| 224 | pred = torch.where(outputs > 0.5, 1, 0) # get the index of the max log-probability |
| 225 | correct += pred.eq(targets.view_as(pred)).sum().item() |
| 226 | |
| 227 | test_loss /= len(test_loader.dataset) |
| 228 | |
| 229 | # for the 1st epoch, the average loss is 0.0001 and the accuracy 97-98% |
| 230 | # using default settings. After completing the 10th epoch, the average |
| 231 | # loss is 0.0000 and the accuracy 99.5-100% using default settings. |
| 232 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( |
| 233 | test_loss, correct, len(test_loader.dataset), |
| 234 | 100. * correct / len(test_loader.dataset))) |
| 235 | |
| 236 | |
| 237 | def main(): |