[1]
for batch_idx, (inputs, labels) in enumerate(test_loader):
     inputs, labels = inputs.cuda(), labels.cuda()
     with torch.no_grad():
         outputs = model(inputs)
     _, predicted = outputs.max(1)
     total += labels.size(0)
     correct += predicted.eq(labels).sum().item()
 print(correct / total)

[2]

nb_classes = 10
net.eval()
confusion_matrix = torch.zeros(nb_classes, nb_classes)
for batch_idx, (inputs, labels) in enumerate(test_loader):
    inputs = inputs.cuda()
    labels = labels.cuda()
    outputs = net(inputs)
    _, preds = torch.max(outputs, 1)
    for t, p in zip(labels.view(-1), preds.view(-1)):
        confusion_matrix[t.long(), p.long()] += 1
ele_wise_acc = confusion_matrix.diag() / confusion_matrix.sum(1) # Class-wise acc
print(ele_wise_acc.mean() * 100) # Total acc



[1]: Calculate accuracy code
[2]: Calculate class-wise accuracy from How to find individual class Accuracy 11.
[1] and [2] have different accuracy.
I think both of them are looks fine, Anyone can find problems?