Multi-target not_supported Error with multiple outputs

AmardeepGanguly: <p>I am facing some issues with training a network with multiple outputs .It is an image classification problem.Every image belongs to one of ten subclasses from two classes.Thus the<br>
labels vector is a tensor of size [60,2,10](Note Here 60 corresponds to the Batch_size)<br>
.For this model i am implementing the LeNet Architecture with two output  tensors and backpropagating the combined loss.<br>
Here is my model</p>
<pre><code class="lang-auto">class LeNet(nn.Module):
    def __init__(self):
            super(LeNet,self).__init__()
            self.cnn_model=nn.Sequential(nn.Conv2d(1,6,5),
            nn.ReLU(),
            nn.AvgPool2d(2,stride=2),
            nn.Conv2d(6,16,5),
            nn.ReLU(),
            nn.AvgPool2d(2,stride=2))
            self.fc_model=nn.Sequential(
            nn.Linear(400,120),
            nn.LeakyReLU(),
            nn.Linear(120,84),
            nn.LeakyReLU(),
            nn.Linear(84,10))
    def forward(self,x):
        x1=self.cnn_model(x)
        x1=x1.view(x1.size(0),-1)
        x1=self.fc_model(x1)
        x2=self.cnn_model(x)
        x2=x2.view(x2.size(0),-1)
        x2=self.fc_model(x2)
        return x1,x2
</code></pre>
<pre><code class="lang-auto">net=LeNet().to(device)
import torch.optim as optim
loss_fn=nn.CrossEntropyLoss()
opt=optim.Adam(net.parameters())
</code></pre>
<p>This is my training loop</p>
<pre><code class="lang-auto">%%time
loss_arr=[]
loss_epoch_arr=[]
epochs =25
for epoch in range(epochs):
    for i,data in enumerate(train_loader,0):
        inputs,labels=data
        inputs,labels=inputs.to(device),labels.to(device)
        opt.zero_grad()
        out1,out2=net(inputs)
        loss1=loss_fn(out1,labels[:,0,:].long())
        loss2=loss_fn(out2,labels[:,1,:].long())
        loss=loss1+loss2
        loss.backward()
        opt.step()
        loss_arr.append(loss.item())
    loss_epoch_arr.append(loss.item())
    print("Epoch:%d/%d,Test_Acc: %0.2f,Train_Acc: %0.2f"%(epoch,epochs,evaluation(testloader),evaluation(trainloader)))
plt.plot(loss_epoch_arr)
plt.show()
</code></pre>
<p>But i am getting the following error<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/ed647f2b87f22f10b838e0dd3ba82708090aa1c1.png" data-download-href="https://discuss.pytorch.org/uploads/default/ed647f2b87f22f10b838e0dd3ba82708090aa1c1" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/2X/e/ed647f2b87f22f10b838e0dd3ba82708090aa1c1.png" alt="error" data-base62-sha1="xS4tnjz7MlSO3sw5ykVGiZIqQxz" width="690" height="265" data-dominant-color="2C3036"><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">error</span><span class="informations">882×340 35.7 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><br>
Thanks i in advance if you have any suggestions please provide some code as i am a beginner</p>

sai_tharun: <p>This is because of the loss function.<br>
cross entropy doesnt accept the one hot encoding of the target variables.<br>
Place the below code before loss =loss1+loss2</p>
<pre><code class="lang-auto">labels1,labels2 = labels[:, 0], labels[:, 1] 
targetnp=labels1.cpu().numpy()
targetnp1 = labels2.cpu().numpy()
l1=np.where(targetnp&gt;0)[1]
l2=np.where(targetnp1&gt;0)[1]
new_targets=torch.LongTensor(idxs)
new_targets1=torch.LongTensor(idxs1)
new_targets = new_targets.to(device)
new_targets1 = new_targets1.to(device)          
loss1 = loss_fn(out1, new_targets)
loss2 = loss_fn(out2,new_targets1)
</code></pre>

AmardeepGanguly: <p>Thank you Sai very much I was stuck on this for a long time one last thing can you suggest me any codes for the Evaluation part   .Can you help me with this?this is my code I am passing a<br>
dataloader to test the accuracy of the model .This was my code for a simpler models where there was a single output</p>
<pre><code class="lang-auto">def evaluation(dataloader):
  total,correct=0,0
  for data in dataloader:
    inputs,labels=data
    inputs,labels=inputs.to(device),labels.to(device)
    out=net(inputs)
    _,pred=torch.max(out.data,1)
    total+=labels.size(0)
    correct+=(pred==labels).sum().item()
  return 100*correct/total
</code></pre>
<p>Should i apply the same transformations to the targets as above ?If possible provide code snippets.</p>

sai_tharun: <p>i dont know what your labels are,i really cant tell you without knowing what you trying to do</p>