What is the weight values mean in torch.nn.CrossEntropyLoss?

minhoha: <p>In official docs, weight is used for unbalanced training set. In CrossEntropyLoss, what is the weight values mean??</p>

jdhao: <p>It just means the weight that you give to different classes. Basically, for classes with small number of training images, you give it more weight so that the network will be punished more if it makes mistakes predicting the label of these classes. For classes with large numbers of images, you give it small weight.</p>

minhoha: <p>So, how can i decide that value?? It is proportional to size of classes??</p>

tom: <p>If you have a roughly balanced problem, you don’t need to use weights. If you have imbalance, <code>1/class_size</code> is a typical choice.</p>
<p>Best regards</p>
<p>Thomas</p>

minhoha: <p>what if class 0 has 9000 samples and class 1 has 41000 samples, in this case, are weight of class0 1/9000 and weight of class1 1/41000 ??</p>

jdhao: <p>It is not definitely. You can try multiple weights and see which works best.</p>

minhoha: <p>ok thank you for your advice</p>

kkorovesis: <p>What should the weights be to get better accuracy ? In a 5 classes data I used something like that and the results got much worst. Can you give an intuition ?</p>
<pre><code class="lang-auto">weights = [1/1016, 1/12852, 1/12888, 1/3380, 1/296] #[ 1 / number of instances for each class]
class_weights = torch.FloatTensor(weights).cuda()

criterion = torch.nn.CrossEntropyLoss(weight=class_weights)
parameters = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters)
</code></pre>

done1892: <p>I used this formula, and it led me to very good results:</p>
<pre><code class="lang-auto">Max(Number of occurrences in most common class) / (Number of occurrences in rare classes)
</code></pre>
<ul>
<li>In multiclass problems, if we have five classes with this number of occurrences<br>
0     2741<br>
1    37919<br>
2    22858<br>
3    31235<br>
4     5499<br>
the weight value for each class is 0:(37919/2741), 1:(37919/37919), 2:(37919/22858), 3:(37919/31235), 4:(37919/5499), so:</li>
</ul>
<pre><code class="lang-auto">weights = [13.83, 1.0, 1.66, 1.21, 6.9]
class_weights = torch.FloatTensor(weights).to(device)
criterion = nn.CrossEntropyLoss(weight=class_weights)
</code></pre>
<ul>
<li>In binary classification problems, if we have two classes respectively<br>
0     900<br>
1     100<br>
you can use <a href="https://pytorch.org/docs/stable/nn.html#bcewithlogitsloss" rel="nofollow noopener">nn.BCEWithLogitsLoss</a> <code>pos_weight</code> parameter, which takes as input the positive class weight (in this case 900/100 = 9), so:</li>
</ul>
<pre><code class="lang-auto">weight = [9.0]
class_weight = torch.FloatTensor(weight).to(device)
criterion = nn.BCEWithLogitsLoss(pos_weight= class_weight)
</code></pre>

gabrieldernbach: <p>Keep in mind that in a bigger than 90/10 unbalance setting, you will be presenting your network with more than 90 % cases of fairly small losses (weighted by 1/n_samples). While once in a while the other class of less than 10 % will pop up with a huge loss resulting in a relatively huge update step, forcing you to nevertheless stay at a moderate learning rate.</p>
<p>Now if you combine this with some sophisticated augmentation pipeline as is often necessary, this can become a real waste of resources (electricity bill).</p>
<p>I think for anything bigger than tiny datasets it is can be recommended to upsample the minority classes. This can be achieved with light memory footprint by using the weighted samplers provided with pytorch.</p>
<p>As a side note: an additional quite natural case where class weights come up is, when on deployment we know that the economic cost of a misclassification of class (a) is much higher than the economic cost of class (b). Then we can just take that cost (2 to 1) and put it into our CrossEntropyLoss [2.0, 1.0].</p>

sharkdeng: <p>Can this be applied in F.binary_cross_entropy_loss (unbalanced binary classification problem?)</p>

raquelhortab: <p>You mean the weighted loss option will take more training time than an oversampling technique? I was actually considering it thinking it’s the opposite, since oversampling would mean having more samples and therefore more steps each epoch. However, the few tests I’ve done, weighted loss is performing horribly (I got around a 1:14 ratio)</p>