Before you start
Write the evaluation loop that runs at the end of every epoch.
Given a classifier and a DataLoader that yields (x, y) batches, return
the accuracy, the macro F1 score and the confusion matrix over the whole
dataset. Three details make this easy to get wrong:
model.eval() before the loop. Afterwards, put the model back in the
mode it was in, because the training loop that called you expects that.torch.no_grad(). Without it, every
forward pass records an autograd graph that you will never use.The confusion matrix C has shape (n_classes, n_classes) and dtype
int64. C[i, j] counts the examples whose true class is i and whose
predicted class is j. For each class:
tp = C[c, c]; fp = C[:, c].sum() - tp; fn = C[c, :].sum() - tp
F1_c = 2 * tp / (2 * tp + fp + fn) # 0 when the denominator is 0
macro_f1 = mean over all n_classes of F1_c
def evaluate(model, loader, n_classes):
Input
# true labels [0, 0, 1, 2, 2]
# predicted labels [0, 1, 1, 2, 0] (argmax of the model's logits)
evaluate(model, loader, n_classes=3)
Output
{"accuracy": 0.6,
"macro_f1": 0.6111,
"confusion": tensor([[1, 1, 0],
[0, 1, 0],
[1, 0, 1]])}
Reasoning
Three of five predictions are right, so the accuracy is 0.6. Class 0 has
tp = 1, fp = 1, fn = 1, so its F1 is 2 / 4 = 0.5. Class 1 has
tp = 1, fp = 1, fn = 0, so its F1 is 2 / 3. Class 2 has
tp = 1, fp = 0, fn = 1, so its F1 is 2 / 3. Their mean is 0.6111.
(batch, n_classes). The prediction is
their argmax.accuracy and macro_f1 are Python floats. confusion is a tensor.0.
It still counts in the macro mean.Run it in Colab
Google Colab runs real PyTorch without a local installation. Get a code, open the notebook, and paste the code into its first cell. Grading happens there and the result comes back here.
If no runtime is available, save a copy in Drive and retry later, or choose Your machine. These exercises work on CPU; a GPU is optional. Connection help ↗
Run PyTorch on your machine?
In Colab, the grading cell is your Submit. It runs the hidden cases there, and the result shows here on its own.
⇧↵run cell