Is there any way to completely ignore samples with a particular label when calculating loss?

Cloud_Snow: <p>Hi there,<br>
my problem is as follows:<br>
I have a mixture of  <strong>Labelled</strong>  Sample Group and  <strong>Unlabeled</strong>  Sample Group. For some special reason, I have to mix those samples in a same mini-batch to train a classification network, with  <strong>CrossEntropyLoss</strong> . So I need to ignore these unlabeled samples when computing the CrossEntropyLoss.</p>
<p>I tried to assign a pseudo label to these unlabeled samples and set the option  <em>weight</em>  in <a href="http://pytorch.org/docs/master/nn.html?highlight=crossentropy#torch.nn.CrossEntropyLoss" rel="nofollow noopener"> <strong>CrossEntropyLoss</strong>  </a> to be  <em>( <strong>zero</strong> )</em>  corresponding to the pseudo label , but found that it wouldn’t satisfy my demand. Although the loss on the pseudo class is ignored, if the prediction of an unlabeled data have large values on other indexes, it still generate large loss. I need those samples contribute exactly  <strong>0</strong>  to the CrossEntropyLoss.</p>
<p>So will someone kindly help me with this issue?<br>
Millions of thanks in advance.</p>

SoucheChapich: <p>Hello,</p>
<p>Indeed setting the weight in CrossEntropyLoss set weights with respects to the classes which seems not to be what you need.<br>
Not sure my solution could help you but in my opinion you could create 3 datasets, one with labelled, one with unlabelelled and one that return the 2. You can create them via module Dataset of Pytorch, you can make a call only on the labelled data to compute the crossentropyloss, something like:</p>
<pre><code class="lang-auto">mixed_dataset = MixedDataset() #the __getitem__() function return ( labeleddata, unlabeleddata)
labeled_dataset = LabeledDataset()
unlabelled_dataset = UnlabeledDataset()

for data in mixed_dataset:
     whatever you need
     for labeled in labeled_dataset:
             compute loss
    whatever you need
</code></pre>
<p>just be sure to not mix the index and always return the corresponding data</p>

Cloud_Snow: <p>Thanks for your reply.</p>
<p>I’m a little confused that there are two for loops to get 2 batch of data. Can you explain more about that?</p>
<p>In my case, both the labeled data and unlabeled data are mixed and shuffled during training, forming a minibatch to do forward propagation.</p>

Cloud_Snow: <p>Hi SoucheChapich, thanks for your reply.</p>
<p>I’m a little confused that there are two for loops to get two batches of data. Can you explain more about that?</p>
<p>In my case, both the labeled data and unlabeled data are mixed and shuffled during training</p>

Cloud_Snow: <p>I notice that there exists a parameter called ignore_index in nn.CrossEntropyLoss(), but it seems that ignore_index works similarly to zero weight and cannot solve the problem that misclassified<br>
unlabeled data still contribute to the final loss</p>

SoucheChapich: <p>Hello,</p>
<p>I was just thinking about a solution for your case, I think that something like that is possible.</p>
<p>How did you define your dataset ? Are you using data.Dataset class from pytorch ? If yes, you should be able to specify whatever you want. I mean, you can do somehing like:</p>
<pre><code class="lang-auto">class mixedDataset(data.Dataset):
     def __getitem__(self, index):
           return your combined_data

    def __getlabeleddata_(self,index):
           return your labeled data 
</code></pre>
<p>and after you can iterate through your mixedDataset and calling your <strong>getlabeldata</strong> function to get the labeled data.</p>

Cloud_Snow: <p>Yes, I use Dataset and dataloader class from pytorch. You can viewed the model comprising a feature extractor and a classifier which works in an E2E manner. For both labeled and unlabeled data mixed together to do forward propagation,  however, you can view it as we use the whole data for feature extraction, but only labeled data was used for classificaion.</p>