I was trying to understand how weight is in CrossEntropyLoss works by a practical example. So I first run as standard PyTorch code and then manually both. But the losses are not the same.
from torch import nn
import torch
softmax=nn.Softmax()
sc=torch.tensor([0.4,0.36])
loss = nn.CrossEntropyLoss(weight=sc)
input = torch.tensor([[3.0,4.0],[6.0,9.0]])
target = torch.tensor([1,0])
output = loss(input, target)
print(output)
>>1.7529
Now for manual Calculation, first softmax the input:
print(softmax(input))
>>
tensor([[0.2689, 0.7311],
[0.0474, 0.9526]])
and then negetive log of the correct class probality and multiply with the respective weight:
((-math.log(0.7311)*0.36) - (math.log(0.0474)*0.4))/2
>>
0.6662
What I am missing here?
np.unique(y)
be ordered ascendingly? – Pickmeup