Solved by KFrank in post #2 
              
              
                Hello Yash! 

As an aside, you can use the “K-dimensional case” feature of 
CrossEntropyLoss (if you’re using a recent enough version of 
pytorch) to eliminate your for loop. 
output.size (1) * torch.nn.CrossEntropyLoss() (output.transpose (1, 2), target)

should yield the same result as your calcul…