My prediction is y_hat = [ 0.57,0.05,0.14,0.10,0.14]
and target is
target =[ 1, 0, 0, 0, 0 ]
.
I need to calculate Cross Entropy loss by NumPy and Pytorch loss function.
Using NumPy my formula is -np.sum(target*np.log(y_hat))
, and I got 0.5621189181535413
However, using Pytorch:
loss = nn.CrossEntropyLoss()
output = torch.FloatTensor([0.57,0.05,0.14,0.10,0.14])
label = torch.FloatTensor([1,0,0,0,0])
loss_value = loss(output, label)
print(loss_value)
Gives tensor(1.2586)
, which is different.