Trying to understand cross_entropy loss in PyTorch
This is a very newbie question but I'm trying to wrap my head around cross_entropy loss in Torch so I created the following code:

x = torch.FloatTensor([
                        [1.,0.,0.]
                       ,[0.,1.,0.]
                       ,[0.,0.,1.]
                       ])

print(x.argmax(dim=1))

y = torch.LongTensor([0,1,2])
loss = torch.nn.functional.cross_entropy(x, y)

print(loss)


which outputs the following:

tensor([0, 1, 2])
tensor(0.5514)


What I don't understand is given my input matches the expected output why is the loss not 0?

That is because the input you give to your cross entropy function is not the probabilities as you did but the logits to be transformed into probabilities with this formula: 

probas = np.exp(logits)/np.sum(np.exp(logits), axis=1)


So here the matrix of probabilities pytorch will use in your case is:

[0.5761168847658291,  0.21194155761708547,  0.21194155761708547]
[0.21194155761708547, 0.5761168847658291, 0.21194155761708547]
[0.21194155761708547,  0.21194155761708547, 0.5761168847658291]

