'Calculate Model Accuracy for one of the Kaggle Notebook

How to calculate accuracy for PyTorch model used inhttps://www.kaggle.com/code/andradaolteanu/whales-dolphins-effnet-train-rapids-clusters

I tried using some methods like -

  1. Tried this → correct += (out == targets).float().sum() accuracy = 100 * correct / len(test) This is giving weird errors ,i.e,

    RuntimeError Traceback (most recent call last) /tmp/ipykernel_33/896230284.py in ----> 1 train_pipeline(train) /tmp/ipykernel_33/2939766947.py in train_pipeline(train) 62 # print(targets) 63 ---> 64 correct += (out == targets).float().sum() 65 # num_corrects = num_corrects + torch.sum(out.argmax(1) == targets) 66 RuntimeError: The size of tensor a (15587) must match the size of tensor b (32) at non-singleton dimension 1

    1. Tried this → num_corrects = num_corrects + torch.sum(out.argmax(1) == targets) print(“Accuracy = {}”.format(train_accuracy)) This is giving 0 as accuracy and printing out and targets giving me this

    out tensor([[-13.0885, -15.4269, -15.7011, ..., -18.2123, -21.8597, -19.1524], [-13.3293, -12.2672, -14.0315, ..., -17.7681, -22.0472, -18.1889], [-14.6508, -13.7065, -16.5062, ..., -19.2380, -21.8163, -19.5815], ..., [-15.3698, -16.0767, -14.8986, ..., -18.8057, -22.7389, -19.4303], [-13.5040, -14.4153, -15.7141, ..., -19.5422, -22.9803, -19.5026], [-13.8497, -15.1122, -14.3879, ..., -19.5625, -22.6934, -18.4038]], device='cuda:0', grad_fn=) targets tensor([ 2842, 1353, 118, 108, 425, 168, 2123, 9758, 6036, 748, 443, 1, 102, 12102, 2115, 450, 96, 5147, 998, 1227, 220, 177, 8876, 32, 79, 9920, 65, 43, 1468, 84, 8002, 13], device='cuda:0') out tensor([[-14.9549, -14.1026, -15.7504, ..., -19.3176, -22.6721, -18.4659], [-11.7263, -12.5097, -15.5440, ..., -19.5593, -22.9155, -20.5102], [ -4.3821, -9.7733, -3.9627, ..., -11.9340, -11.7187, -12.0451], [ -6.1771, -7.6977, -5.3471, ..., -11.8034, -11.9683, -13.8230], [-15.3451, -16.2378, -16.4004, ..., -20.1262, -22.6535, -20.3700], [-14.6429, -15.8015, -15.3850, ..., -19.8169, -22.2832, -18.9964]], device='cuda:0', grad_fn=) targets tensor([ 8923, 2130, 4607, 12931, 341, 47], device='cuda:0')

I am adding this accuracy thing in train_pipeline(train)

Please could anyone help me with it,



Solution 1:[1]

Since you haven't provided details about the exact shapes and datatypes of out and targets tensors, I can only provide the approach, you have to figure out the exact code.

If you are using softmax at the end of your model, then out should be a tensor of size (n_classes, 1) containing the probabilities of the datapoint belonging to each class. You have to convert that vector of probabilities to the corresponding integer class label, and store it in (say) out_labels.

Also ensure that targets is an integer tensor, and contains the correct integer class labels.

After above steps, you can compute accuracy like this -

accuracy = (out_labels == targets).sum() / len(targets)

(Make sure both out_labels and targets are integer tensors of the same shapes to avoid running into any errors).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aravind G.