A low training loss only says the model fits the images it trained on. What counts is how well it reads digits it has never seen. The starting code trains the digits network from the last lesson on the 1,347 images of digits_train.pt and keeps the 450 test images of digits_test.pt aside in X_test and y_test. Both files hold pixels already divided by 16.
Two switches for a test: model.eval() puts the model in evaluation mode: layers that act differently while training, like dropout in the next lesson, switch to their test behavior. with torch.no_grad(): opens a block where PyTorch keeps no record for gradients. A test never calls backward(), so that record would only cost memory and time.
From scores to digits: the model gives back one row of 10 scores per image, one score per digit. scores.argmax(1) gives back, for each row, the position of its biggest score: the digit the model picks. == between two tensors gives True or False for each position, .sum() counts the True ones, .item() turns a one-number tensor into a plain Python number, and .tolist() turns a tensor into a Python list.
s = torch.tensor([[0.1, 2.5, -1.0], [3.0, 0.2, 0.4]])
picks = s.argmax(1) # tensor([1, 0])
same = (picks == torch.tensor([1, 2])).sum() # tensor(1)
print(same.item(), picks.tolist()) # 1 [1, 0]
When you're done, the program prints this. The last two lines come from the lesson's check:
epoch 5 loss: 0.221 epoch 10 loss: 0.096 epoch 15 loss: 0.064 epoch 20 loss: 0.048 Test accuracy: 0.967 Predicted: [2, 8, 2, 6, 6] Truth: [2, 8, 2, 6, 6] model.training: False scores.requires_grad: False
Your task: below the training loop, switch the model to evaluation mode, get scores for X_test inside a torch.no_grad() block, pick the digits with argmax(1), compute the share the model reads right, and print it, rounded to 3 decimals, with the first five predictions next to the truth.
Assignment
Follow the Coding Tutorial to test the trained model on the 450 images it never saw.
Hint
Only the line that runs the model goes inside the with block. The accuracy is the number of matches divided by len(y_test).
Free plan: 30 tutor messages. PRO is unlimited.
