title: RuntimeError: The size of tensor a (4000) must match the size of tensor b (512) at non-singleton dimension 1
body: <p>I'm trying to build a model for document classification. I'm using <code>BERT</code> with <code>PyTorch</code>.</p>
  <p>I got the bert model with below code.</p>
  <pre><code>bert = AutoModel.from_pretrained('bert-base-uncased')
  </code></pre>
  <p>This is the code for training.</p>
  <pre><code>for epoch in range(epochs):
   
      print('\n Epoch {:} / {:}'.format(epoch + 1, epochs))
  
      #train model
      train_loss, _ = modhelper.train(proc.train_dataloader)
  
      #evaluate model
      valid_loss, _ = modhelper.evaluate()
  
      #save the best model
      if valid_loss &lt; best_valid_loss:
          best_valid_loss = valid_loss
          torch.save(modhelper.model.state_dict(), 'saved_weights.pt')
  
      # append training and validation loss
      train_losses.append(train_loss)
      valid_losses.append(valid_loss)
  
      print(f'\nTraining Loss: {train_loss:.3f}')
      print(f'Validation Loss: {valid_loss:.3f}')
  </code></pre>
  <p>this is my train method, accessible with the object <code>modhelper</code>.</p>
  <pre><code>def train(self, train_dataloader):
      self.model.train()
      total_loss, total_accuracy = 0, 0
      
      # empty list to save model predictions
      total_preds=[]
      
          # iterate over batches
      for step, batch in enumerate(train_dataloader):
          
          # progress update after every 50 batches.
          if step % 50 == 0 and not step == 0:
              print('  Batch {:&gt;5,}  of  {:&gt;5,}.'.format(step, len(train_dataloader)))
          
          # push the batch to gpu
          #batch = [r.to(device) for r in batch]
          
          sent_id, mask, labels = batch
          
          # clear previously calculated gradients 
          self.model.zero_grad()        
  
          print(sent_id.size(), mask.size())
          # get model predictions for the current batch
          preds = self.model(sent_id, mask) #This line throws the error
          
          # compute the loss between actual and predicted values
          self.loss = self.cross_entropy(preds, labels)
          
          # add on to the total loss
          total_loss = total_loss + self.loss.item()
          
          # backward pass to calculate the gradients
          self.loss.backward()
          
          # clip the the gradients to 1.0. It helps in preventing the exploding gradient problem
          torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
          
          # update parameters
          self.optimizer.step()
          
          # model predictions are stored on GPU. So, push it to CPU
          #preds=preds.detach().cpu().numpy()
          
          # append the model predictions
          total_preds.append(preds)
        
      # compute the training loss of the epoch
      avg_loss = total_loss / len(train_dataloader)
      
      # predictions are in the form of (no. of batches, size of batch, no. of classes).
      # reshape the predictions in form of (number of samples, no. of classes)
      total_preds  = np.concatenate(total_preds, axis=0)
        
      #returns the loss and predictions
      return avg_loss, total_preds
  </code></pre>
  <p><code>preds = self.model(sent_id, mask)</code> this line throws the following error(including full traceback).</p>
  <pre><code> Epoch 1 / 1
  torch.Size([32, 4000]) torch.Size([32, 4000])
  Traceback (most recent call last):
  
  File &quot;&lt;ipython-input-39-17211d5a107c&gt;&quot;, line 8, in &lt;module&gt;
  train_loss, _ = modhelper.train(proc.train_dataloader)
  
  File &quot;E:\BertTorch\model.py&quot;, line 71, in train
  preds = self.model(sent_id, mask)
  
  File &quot;E:\BertTorch\venv\lib\site-packages\torch\nn\modules\module.py&quot;, line 727, in _call_impl
  result = self.forward(*input, **kwargs)
  
  File &quot;E:\BertTorch\model.py&quot;, line 181, in forward
  #pass the inputs to the model
  
  File &quot;E:\BertTorch\venv\lib\site-packages\torch\nn\modules\module.py&quot;, line 727, in _call_impl
  result = self.forward(*input, **kwargs)
  
  File &quot;E:\BertTorch\venv\lib\site-packages\transformers\modeling_bert.py&quot;, line 837, in forward
  embedding_output = self.embeddings(
  
  File &quot;E:\BertTorch\venv\lib\site-packages\torch\nn\modules\module.py&quot;, line 727, in _call_impl
  result = self.forward(*input, **kwargs)
  
  File &quot;E:\BertTorch\venv\lib\site-packages\transformers\modeling_bert.py&quot;, line 201, in forward
  embeddings = inputs_embeds + position_embeddings + token_type_embeddings
  
  RuntimeError: The size of tensor a (4000) must match the size of tensor b (512) at non-singleton dimension 1
  </code></pre>
  <p>If you observe I've printed the torch size in the code.
  <code>print(sent_id.size(), mask.size())</code></p>
  <p>The output of that line of code is <code>torch.Size([32, 4000]) torch.Size([32, 4000])</code>.</p>
  <p>as we can see that size is the same but it throws the error. Please put your thoughts. Really appreciate it.</p>
  <p>please comment if you need further information. I'll be quick to add whatever is required.</p>