Passing weights to cross entropy loss
I am trying to assign different weights to different classes, so I have modified my loss criterion as such:

I had to convert the weight tensor to double torch.DoubleTensor(weight) since my model is already moved to double(). Am I doing it correctly ?

weights = [0.4,0.8,1.0]

class_weights = torch.DoubleTensor(weights).cuda()

criterion = nn.CrossEntropyLoss(weight=class_weights)


According to the documentation, the weight parameter to CrossEntropyLoss should be:


  weight (Tensor, optional) – a manual rescaling weight given to each class. If given, has to be a Tensor of size C.


I assume you have 3 classes (C=3). By the way, are you sure your model is moved to double()? Otherwise, you should prefer using FloatTensor. For example,

nn.CrossEntropyLoss(weight=torch.FloatTensor([0.4, 0.8, 1.0]))


Otherwise, your example looks ok!
