Hi Dhorka!



 Dhorka:

I observed that I’m achieving the same results


The short answer is that your batch size is 1, so you won’t see a
difference.


>>> input = torch.randn(1, 5, requires_grad=True)
tensor([[ 1.2900,  0.4519, -0.2622, -0.5862, -0.5304]], requires_grad=True)

>>> target = torch.empty(1, dtype=torch.long).random_(5)
tensor([0])

Am I doing the weighting correctly? How is it supposed to be used the weight parameter of cross-entropy loss?


Yes, you’re doing the weighting correctly and the result is correct.
But CrossEntropyLoss calculates a weighted average when using
class weights (and the default reduction = 'mean').  So when you
take the weighted average over a single sample, the weight drops out.
Try this again with a batch with a size of more than one (and make
sure that the batch contains samples with classes with different
weights).  Then the values of weighted and unweighted loss functions
will differ.
Best.
K. Frank