Hi Mr. Bing!



 bing:

Classes are imbalanced.
weights = torch.tensor([0.90,0.40,0.40,0.20])
criterion = torch.nn.CrossEntropyLoss(weights)

On calculating number of different classes in a single image, following it the result -
(tensor([0, 1, 2, 3]), tensor([305903,  12655,  10342,   1060]))
…
How to choose weights values for it?


There is no single right answer for choosing weights to reweight an
unbalanced data set.  But a good starting point is:
weight_for_class = 1 / number_of_samples_in_class.
The idea is that you might want each class to be making about the
same overall contribution to the loss function – so, more samples
in a class means you use a lower weight.
So, you might start with:
counts = torch.FloatTensor ([305903, 12655, 10342, 1060])
weights = 1.0 / counts
weights_normalized = (1.0 / counts) / (1.0 / counts).sum()

(I’m using the old pytorch version 0.3.0.)
You don’t need this, but if you want your weights to be normalized (sum
to one), the last line illustrates this.
This yields:
>>> counts

 3.0590e+05
 1.2655e+04
 1.0342e+04
 1.0600e+03
[torch.FloatTensor of size 4]

>>> weights

1.00000e-04 *
  0.0327
  0.7902
  0.9669
  9.4340
[torch.FloatTensor of size 4]

>>> weights_normalized

 0.0029
 0.0704
 0.0862
 0.8405
[torch.FloatTensor of size 4]

Good luck!
K. Frank