Batch size problem

So I confront with another problem :smiley: When I train it and my loops are:

train_x=19712,1,3
train_y=19712,1
and
class Module(nn.Module):
        def __init__(self, D_in, H1, H2, D_out):
                super().__init__()
                self.linear1 = nn.Linear(D_in, H1)
                self.linear2 = nn.Linear(H1, H2)
                self.linear3 = nn.Linear(H2, D_out)
        def forward(self, x):
                x = F.relu(self.linear1(x))  
                x = F.relu(self.linear2(x))
                x = self.linear3(x)
                return x
model=Module(3,32,12,1)

for e in range(epochs):
  
        running_loss = 0.0
        running_corrects = 0.0
        val_running_loss = 0.0
        val_running_corrects = 0.0
  
        for inputs in train_x:
                

              
                
                loss = criterion(model(inputs),train_y)

                #_,preds=torch.max(outputs,1)
                #outputss.append(preds.max().detach().numpy())
                losses.append(loss)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                                                                        
                #outputss.append(outputs.detach().numpy())
                #print(loss.item())

I get this error:

C:\Users\gun\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\loss.py:431: UserWarning: Using a target size (torch.Size([19712, 1])) that is different to the input size (torch.Size([1, 1])).
This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)

Please anyone can help me?

Hi

Could you please output the size of model(inputs) ?
Possible the size of your output from the model is not matching with the target with which you will compare and calculate loss. Seems it should behave like this only. So, please check the size of the output coming from the model

torch.Size([32, 616, 1])=model(inputs)
torch.Size([32, 616])=train_y

do u have any advice about this situation? Because it works perfectly in keras but in pytorch i could not find any solution about this problem

Your output size = [32,616,1]

try this :

output = model(inputs)
new_output = torch.squeeze(output)

Thank you so much for respond. It works fine but on nn side. My nn is:

class Module(nn.Module):
        def __init__(self, D_in, H1, H2, D_out):
                        super().__init__()
                        self.linear1 = nn.Linear(D_in, H1)
                        self.linear2 = nn.Linear(H1, H2)
                        self.linear3 = nn.Linear(H2, D_out)
        def forward(self, x):
                        x = F.relu(self.linear1(x))  
                        x = F.relu(self.linear2(x))
                        x = self.linear3(x)
                        return x

model=Module(3,32,12,1)

and after the training, I pass train_y into the model and i got this error

RuntimeError: size mismatch, m1: [32 x 616], m2: [3 x 32] at C:\w\1\s\tmp_conda_3.7_055457\conda\conda-bld\pytorch_1565416617654\work\aten\src\TH/generic/THTensorMath.cpp:752