Why am i getting this error when both the labels and the output of the model have same dimensions?

username: aayush
cooked: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/1/1b55844ff51d023d5cc26eae3bed384502522d4d.png" alt="Capture" data-base62-sha1="3TO6RwlLf9h5ZGnekFqUNyhlzOZ" width="441" height="178"></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/290c80cd443b818e0e60c8cd0991edcb5696c744.png" data-download-href="https://discuss.pytorch.org/uploads/default/290c80cd443b818e0e60c8cd0991edcb5696c744" title="Capturea"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/2/290c80cd443b818e0e60c8cd0991edcb5696c744_2_690x432.png" alt="Capturea" data-base62-sha1="5R8mzHhIDDgSGIAiJcTrRfsnoNu" width="690" height="432" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/2/290c80cd443b818e0e60c8cd0991edcb5696c744_2_690x432.png, https://discuss.pytorch.org/uploads/default/optimized/2X/2/290c80cd443b818e0e60c8cd0991edcb5696c744_2_1035x648.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/2/290c80cd443b818e0e60c8cd0991edcb5696c744.png 2x" data-dominant-color="2C3035"><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">Capturea</span><span class="informations">1379×864 98.8 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>class MyNet1(nn.Module):</p>
<pre><code>def __init__(self):
    super(MyNet1, self).__init__()

    self.common = nn.Sequential(
        nn.Linear(64*64*3,32*32*3),
        nn.ReLU(),
        nn.Dropout(0.2),
        nn.Linear(32*32*3, 16*16*3),
        nn.ReLU(),
        nn.Dropout(0.2),
        nn.Linear(16*16*3,320),
        nn.Dropout(0.2) 
    )


    self.branch1 = nn.Sequential(
       nn.Linear(320,320),
       nn.ReLU(),
       nn.Dropout(0.2),
       nn.Linear(320,160),
       nn.ReLU(),
       nn.Dropout(0.2),
       nn.Linear(160,10)
       
    )

    self.branch2 =nn.Sequential(
       nn.Linear(320,320),
       nn.ReLU(),
       nn.Dropout(0.2),
       nn.Linear(320,160),
       nn.ReLU(),
       nn.Dropout(0.2),
       nn.Linear(160,10)          
    )


def forward(self, X):
        
        X = X.view(X.size(0),-1)
        
        X= self.common(X)
         

        op1= self.branch1(X)
        op2= self.branch2(X)
     
        
        return torch.cat([op1, op2], 1)
</code></pre>

username: simon_thom
cooked: <p>It expects the target to have dimensions (60,10), but you provide them as (60,2,10).</p>
<p>I think you have to give your targets as 2D map where each pixel has the value of the appropriate class (or just a vector with the correct classes, depending on what you’re doing)</p>

username: aayush
cooked: <p>But the outputs and labels have the same dimesnsions,i have printed both the dimensions. Please have a look</p>

username: simon_thom
cooked: <p>Yes and that is the problem <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>They shouldn’t have the same dimensions for that loss function. The error tells you that the targets must be of size (60,10) and not (60,2,10).</p>
<p>Also look at: <a href="https://pytorch.org/docs/stable/nn.html#nllloss" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html#nllloss</a> <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: sai_tharun
cooked: <p>can you just post your full model? and  y r u reshaping  the final output?</p>

username: aayush
cooked: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/1/1fd450145f23779ba55c6668f648454d44f1a256.png" alt="Capturef" data-base62-sha1="4xzG8HyzMriKDe3lfQwmPmkkluu" width="403" height="201"></p>
<p>I have printed the labels size. So don’t you expect my model output and the labels must have the same dimensions? Then only they can be compared?</p>

username: aayush
cooked: <p>i have posted my whole model in code.<br>
Actually  every image belongs to one of the ten classes for two categories.(i.e. the labels size is [2,10], one class in each of the ten classes.)<br>
And my model gives and output of (batch_size,20),Therefore i am reshaping it to (batch_size,2,10) so that is matches with the dimension of the label.<br>
(the batch size is 60).</p>

username: sai_tharun
cooked: <p>in forward function, change the return code</p>
<pre><code class="lang-auto"> op1= self.branch1(X)
 op2= self.branch2(X)
 return op1,op2
</code></pre>
<p>and while training:</p>
<pre><code class="lang-auto">outputs = model(inputs)
loss1 = loss_fn(outputs[0],labels[:,0,:])
loss2 = loss_fn(outputs[1],labels[:,1,:]
loss = loss1+loss2
loss.backward()
opt.step()
</code></pre>
<p>Hope this helps!</p>