I would most likely guess you are using the variable total in the wrong way. It seems total is to represent the total shape of the label tensor, whereas labels.size(0) it would most likely get you the batch size. Try  total+=labels.nelement() as it would return the total number of elements in the tensor. Does that solve it?