calculating accuracy (in lstm model)
I am trying to get my accuracy, and I have this code:
            num_correct = 0.0
            for inputs, labels in dataloader(
            valid_features, valid_labels, batch_size=batch_size, sequence_length=20):

                top_val, top_class = torch.exp(output).topk(1)
                num_correct += torch.sum(top_class.squeeze() == labels)
#...
            print(#...,
                  &quot;Accuracy: {:.3f}&quot;.format(num_correct*1.0 / len(valid_labels) *1.0)

It always prints 0.000, so I decided to print the raw values going into num_correct:
print(top_class.squeeze(), labels)
tensor([ 1,  3,  3,  ...,  3,  4,  3], device='cuda:0') tensor([ 1,  1,  3,  ...,  3,  3,  3], device='cuda:0')
tensor([ 4,  3,  1,  ...,  4,  4,  3], device='cuda:0') tensor([ 4,  3,  1,  ...,  4,  4,  3], device='cuda:0')
tensor([ 2,  4,  2,  ...,  4,  4,  4], device='cuda:0') tensor([ 3,  4,  1,  ...,  4,  4,  4], device='cuda:0')
tensor([ 0,  1,  3,  ...,  2,  3,  0], device='cuda:0') tensor([ 0,  1,  3,  ...,  2,  2,  0], device='cuda:0')


these appear pretty highly accurate. So .. I could extract this out to numpy and be done with it, but there is a pytorch way.

This works:
num_correct += torch.sum(torch.eq(top_class.squeeze(), labels)).item()

