I was skimming the book, and found a teeny tiny error in the accuracy calculation in 04_mnist_basics.ipynb
.
The error is in the accuracy calculation for the baseline 3 or 7 classifier. The code below calculates the accuracy by taking the mean accuracy of 3s, the mean accuracy of 7s, and dividing it by 2. However, there are different numbers of samples in the validation set for 3s and 7s.
accuracy_3s = is_3(valid_3_tens).float() .mean()
accuracy_7s = (1 - is_3(valid_7_tens).float()).mean()
accuracy_3s,accuracy_7s,(accuracy_3s+accuracy_7s)/2
#Output: (tensor(0.9168), tensor(0.9854), tensor(0.9511))
In another cell, I ran:
valid_3_tens.shape[0] == valid_7_tens.shape[0]
#Output: False
When calculating accuracy, I used the following code:
# Create vectors for the predictions and actuals for the 3s
valid_3s_preds = is_3(valid_3_tens).float()
valid_3s_actual = torch.ones_like(valid_3s_preds)
# Create vectors for the predictions and actuals for the 7s
valid_7s_preds = is_3(valid_7_tens).float()
valid_7s_actual = torch.zeros_like(valid_7s_preds)
# Concatenate everything together
preds = torch.cat([valid_3s_preds, valid_7s_preds]).float()
actuals = torch.cat([valid_3s_actual, valid_7s_actual]).float()
# Calculate accuracy
(preds == actuals).float().mean()
# Output: tensor(0.9514)
It’s only a small difference in accuracy, but I’m worried this calculation may mislead readers how to properly calculate accuracy by taking the mean of the accuracy of each class.