ValueError: Target size (torch.Size([1, 1])) must be the same as input size (torch.Size([1, 2]))
While trying to train my model with 2 targets, I get the error…“ValueError: Target size (torch.Size([1, 1])) must be the same as input size (torch.Size([1, 2]))”. I have a data set with 2 targets. I tried a lot even by resizing the tensors but no use. Also if I make the output_dim = 1, it always predicts the same class out of two.
** Loading Training data
class SwelltrainDataset(T.utils.data.Dataset):

  def __init__(self, Swelltrain):
    
    sc = StandardScaler()
    
    X_tr = sc.fit_transform(X_train)
    Y_tr = y_train
      
    self.X_tr = torch.tensor(X_tr, dtype = torch.float32)
    self.Y_tr = torch.tensor(Y_tr, dtype = torch.float32)

  def __len__(self):
    return len(self.Y_tr)

  def __getitem__(self, idx):
              
        return self.X_tr[idx], self.Y_tr[idx]
train_ds = SwelltrainDataset(Swelltrain)

bat_size = 1
idx = np.append(np.where(train_ds.Y_tr == 0)[0], 
                np.where(train_ds.Y_tr == 1)[0],
                )

train_ds.X_tr = train_ds.X_tr[idx]
train_ds.Y_tr = train_ds.Y_tr[idx]

train_ldr = T.utils.data.DataLoader(train_ds,
    batch_size=bat_size, shuffle=True)
batch = next(iter(train_ldr))

I am using LSTM Model with dimensions: input_dim = 16, hidden_dim = 100, layer_dim = 1, output_dim = 2
class LSTMModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
        super(LSTMModel, self).__init__()
        self.hidden_dim = hidden_dim
        
        self.layer_dim = layer_dim

        self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, dropout=1, batch_first=True, )
      
        self.fc = nn.Linear(hidden_dim, output_dim)
       
    def forward(self, x):
        h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()

        c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
        
        x, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
       
        x = self.fc(x[:, -1, :]) 
        return (x)   

**Model Training
optimizer = optim.Adam(model.parameters(), lr=0.001)

loss_func = nn.BCEWithLogitsLoss()

epochs = 2
loss_list = []

model.train()

for epoch in range(epochs):
    total_loss = []
    
    for X_tr, Y_tr in train_ldr:
  
      X_tr = X_tr.unsqueeze(1)
      
     
      Y_tr = Y_tr.type(torch.LongTensor)
      Y_tr = Y_tr.unsqueeze(1)
      
      optimizer.zero_grad()
      output = model(X_tr.float())

      pred = output.argmax(dim=1, keepdim=True)

     loss = loss_func(output, Y_tr.float())
         
      loss.backward()
       
      optimizer.step()
        
      total_loss.append(loss.item())

    loss_list.append(sum(total_loss)/len(total_loss))
    print('Training [{:.0f}%]\tLoss: {:.4f}'.format(
         100. * (epoch + 1) / epochs, loss_list[-1]))

ValueError                                Traceback (most recent call last)
&lt;ipython-input-30-1ab26e6f45d7&gt; in &lt;module&gt;
     31 
     32 #      print(Y_tr.size())
---&gt; 33       loss = loss_func(output, Y_tr.float())
     34 
     35         # Backward pass

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~\anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
    712         assert self.weight is None or isinstance(self.weight, Tensor)
    713         assert self.pos_weight is None or isinstance(self.pos_weight, Tensor)
--&gt; 714         return F.binary_cross_entropy_with_logits(input, target,
    715                                                   self.weight,
    716                                                   pos_weight=self.pos_weight,

~\anaconda3\lib\site-packages\torch\nn\functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
   2825 
   2826     if not (target.size() == input.size()):
-&gt; 2827         raise ValueError(&quot;Target size ({}) must be the same as input size ({})&quot;.format(target.size(), input.size()))
   2828 
   2829     return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

ValueError: Target size (torch.Size([1, 1])) must be the same as input size (torch.Size([1, 2]))


Try converting your Y_tr variable into a 2 class one hot label:
one_hot_label = torch.nn.functional.one_hot(Y_tr.to(torch.int64), 2)

one_hot_label = one_hot_label.float()

This should have the shape of target torch.Size([1, 2])
