title: Siamese Neural Network , RuntimeError: size mismatch, m1: [20 x 5], m2: [10 x 5] at ..\aten\src\TH/generic/THTensorMath.cpp:41
body: <p>I am trying to build a Siamese Neural Network with following code</p>
  <pre><code>class MySiameseNet(nn.Module):
      def __init__(self):
          super(MySiameseNet,self).__init__()
          self.nn = nn.Sequential(
                  nn.Linear(30,30),
                  nn.ReLU(),
                  nn.Linear(30,10),
                  nn.ReLU(),
                  nn.Linear(10,5),
                  nn.ReLU())
          self.fc = nn.Sequential(
                  nn.Linear(10,5),
                  nn.ReLU(),
                  nn.Linear(5,1),
                  nn.Sigmoid())
          
      def forward_once(self,x):
          output = self.nn(x)
          return output
      def forward(self,input1,input2):
          output1 = self.forward_once(input1)
          output2 = self.forward_once(input2)
          combinedOutput = torch.cat((output1,output2))
          output = self.fc(combinedOutput)
          return output
  </code></pre>
  <p>The code works pretty fine when I give it to some random input like following</p>
  <pre><code>size=100
  features=30
  trainData1 = torch.tensor(np.random.randn(size,features)).type(torch.float)
  trainData2 = torch.tensor(np.random.randn(size,features)).type(torch.float)
  labelData = [np.random.randint(0,2) for i in range(size)] 
  
  myNet = MySiameseNet()
  result = myNet(trainData1[0],trainData2[0])
  result
  </code></pre>
  <p>But the problem occurs when I try to give it all the data at once , because in case of batch wise input I would be feeding it not just one by one input but a batch of input values. So I tried first the following way</p>
  <pre><code>result = myNet(trainData1,trainData2)
  </code></pre>
  <p>I got the following scary error</p>
  <pre><code>RuntimeError: size mismatch, m1: [200 x 5], m2: [10 x 5] at ..\aten\src\TH/generic/THTensorMath.cpp:41
  </code></pre>
  <p>Then I tried to use the Pytorch Dataloader method to give the data batchwise using the following way</p>
  <pre><code>class MySiameseDataset(torch.utils.data.Dataset):
  def __init__(self,input1,input2,label):
      super(MySiameseDataset,self).__init__()
      self.input1 = input1
      self.input2 = input2
      self.label = label
  def __len__(self):
      return self.input1.shape[0]
  def __getitem__(self,index):
      return self.input1[index],self.input2[index],self.label[index]
  
  myDataset = MySiameseDataset(trainData1,trainData2,labelData)
  myDatasetLoader = DataLoader(myDataset,batch_size=10,shuffle=False)
  binaryLossFunction = nn.BCELoss()
  adamOptimizer = torch.optim.Adam(myNet.parameters(),lr=0.001)
  </code></pre>
  <p>So now the Dataloader is working fine and I am being able to access data and label batch wise</p>
  <pre><code>for firstInput,secondInput,labelValue in myDatasetLoader:
      print(i.shape,j.shape,labelValue.shape,labelValue.shape)
  </code></pre>
  <p>Output:</p>
  <pre><code>torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  torch.Size([10, 30]) torch.Size([10, 30]) torch.Size([10]) torch.Size([10])
  </code></pre>
  <p>But Now when I try to use the model for training using the following code</p>
  <pre><code>for firstInput,secondInput,labelValue in myDatasetLoader:
      adamOptimizer.zero_grad()
      output = myNet(firstInput,secondInput)
      binaryLossFunction(output,labelValue)
      binaryLossFunction.backward()
      adamOptimizer.step()
  </code></pre>
  <p>I get the following error</p>
  <pre><code>RuntimeError: size mismatch, m1: [20 x 5], m2: [10 x 5] at ..\aten\src\TH/generic/THTensorMath.cpp:41
  </code></pre>
  <p>Please guide me how to reslove the issue and how to pass the input batch wise to the models in pytorch ?</p>