Now that we have trained our model, it's time to evaluate it to see how well it performs.
This is the function that we'll use for evaluation.
On the first line, we have what's called a decorator. torch.no_grad()
disables gradient calculation, which reduces memory consumption. We take the model and the validation set as parameters to the function, then we call the .eval()
method, which tells the model that we'll start evaluating as the model behaves differently during training and evaluating (for example, it deactivates dropout layers).
We then go through the validation set batch by batch using a for loop. We make the predictions, then we call torch.max()
function. We call this function on the output and store the indexes in a variable called preds
. We set dim
to 1 because we want the maximum value in each prediction made for an image. We now have the actual predictions. We then call torch.sum()
on preds == labels
which is a tensor that contains Trues and Falses. True when the prediction is equal to the label and False otherwise. When we call torch.sum()
on that, we get a tensor with the number of Trues found in the tensor. We call the .item()
method to return a single number rather than a tensor, then divide that by the number of predictions, giving us the accuracy.
Now we've evaluated our model, it's time to test the model on single pictures and do with it something useful.