Balancing a multilabel dataset

Boriss_Sidjakins: <p>Hello,</p>
<p>I have a dataset that contains email texts and their corresponding labels. Each email can have multiple labels, making it a multilabel problem. I used multi-hot encoding for labels, so they look like [1, 0, 1, 0, 0], where 1 indicates that an email belongs to that class. However, my data is imbalanced, and some label combinations barely occur in the dataset, which makes my LSTM biased towards the majority classes. I tried specifying pos_weight for BCEWithLogitsLoss, but that doesn’t seem to help. What are some balancing techniques that I could use?</p>

ptrblck: <p>Balancing the data in a multi-label use case is not trivial and <a href="https://discuss.pytorch.org/t/unbalanced-data-in-multiple-object-detection/88104/2">this post</a> describes some methods for a balanced sampling.</p>

vdw: <p>How dependent are the labels? If they are (kind of) independent, would training 5 binary classifiers be an option?</p>

Boriss_Sidjakins: <p>I’m not sure how to measure their dependency, but for context, I’m classifying emails based on what architectural (software) design decisions they contain. Here is the distribution of labels in my training dataset (not-ak refers to emails that don’t contain any architectural decisions):</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/3/e326075a14ebdf9b281b30a608af3da40c1bc6d5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e326075a14ebdf9b281b30a608af3da40c1bc6d5" title="test"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/3/e326075a14ebdf9b281b30a608af3da40c1bc6d5_2_413x500.jpeg" alt="test" data-base62-sha1="wprRrjEslcQFLfq00x9Jxm1FPCd" width="413" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/3/e326075a14ebdf9b281b30a608af3da40c1bc6d5_2_413x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/e/3/e326075a14ebdf9b281b30a608af3da40c1bc6d5.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/3/e326075a14ebdf9b281b30a608af3da40c1bc6d5.jpeg 2x" data-dominant-color="F1F4F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">test</span><span class="informations">580×701 33.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I’m new to machine/deep learning, so I’m not sure if training 5 binary classifiers would be helpful in this case.</p>

vdw: <p>Well, I would indeed consider 5 binary classifiers here, as Yes/No classifier for each of for 5 class labels.</p>
<p>For example if a item item1 is labeled “process, property, technology”, and an item item2 is labeled as “process, technology” it can be views as multiclass data as follow:<br>
(item1, process)<br>
(item1, property)<br>
(item1, technology)<br>
(item2, process)<br>
(item3, technology)</p>
<p>If you now train the, say, “property” classifier, you put item1 in the positive class and item2 in the negative class. For training the “process” and “technology” classifiers, of course both item1 and item2 are in the positive class</p>
<p>For inferencing, you pump a new item through all 5 classifiers. If the “property” classifier and the “technology” classifier say but not the others, you label the new item with “property, technology”.</p>

J_Johnson: <p>Suppose we have all of your targets(all_targets) in a tensor of size [num_targets, num_classes].</p>
<p>First, sum along the num_targets:</p>
<pre><code class="lang-auto">total_by_class = all_targets.sum(0)
</code></pre>
<p>Second, use this to generate a weights vector:</p>
<pre><code class="lang-auto">weights = total_by_class.mean()/total_by_class
</code></pre>
<p>Third, set empty classes back to 0.</p>
<pre><code class="lang-auto">cond = torch.isinf(weights)
weights[cond] = 0
</code></pre>
<p>Finally, pass that into your loss function:</p>
<pre><code class="lang-auto">criterion = nn.CrossEntropyLoss(weight=weights)
</code></pre>
<p>What that will do is cause the loss function to scale each class by a multiplier, by the inverse of their representation. I.e. classes with less examples get scaled more.</p>