I’d like to use the cross-entropy loss function that can take one-hot encoded values as the target.
# Fake NN output
out = torch.FloatTensor([[0.05, 0.9, 0.05], [0.05, 0.05, 0.9], [0.9, 0.05, 0.05]])
out = torch.autograd.Variable(out)

# Categorical targets
y = torch.LongTensor([1, 2, 0])
y = torch.autograd.Variable(y)

# One-hot encoded targets
y1 = torch.FloatTensor([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
y1 = torch.autograd.Variable(y1)

# Calculating the loss
loss_val = nn.CrossEntropyLoss()(out, y)
loss_val1 = nn.BCEWithLogitsLoss()(out, y1)

print(loss_val)
print(loss_val1)


Variable containing:
0.6178
[torch.FloatTensor of size 1]
Variable containing:
0.5927
[torch.FloatTensor of size 1]

The output of the two functions is not the same.
How to implement the cross-entropy to get to same output passing one-hot encoded targets?