Solved by hhaoao in post #3 
              
              
                I found the problem: 

I misunderstood the three sentences of Shape in https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss. (doc too crude). 
The shape of output in my code is 2. So the following code should be changed like th…