title: Expected 3D (unbatched) or 4D (batched) input to [...], but got input of size [...]
body: <p>i tried to program an image-detection, if it's a dog or cat. I use about 25000 images from <a href="https://www.kaggle.com/competitions/dogs-vs-cats-redux-kernels-edition/data?select=train.zip" rel="nofollow noreferrer">kaggle</a>.
  But I get the following error:</p>
  <pre><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  ~\AppData\Local\Temp/ipykernel_18276/2093974780.py in &lt;module&gt;
        1 for epoch in range(1,30):
  ----&gt; 2     train(epoch)
        3     break
  
  ~\AppData\Local\Temp/ipykernel_18276/1566085253.py in train(epoch)
       35         target = Variable(data)
       36         optimizer.zero_grad()
  ---&gt; 37         out = model(data)
       38         criterion = F.nll_loss
       39         loss = criterion(out,target)
  
  ~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
     1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
     1109                 or _global_forward_hooks or _global_forward_pre_hooks):
  -&gt; 1110             return forward_call(*input, **kwargs)
     1111         # Do not call functions when jit is used
     1112         full_backward_hooks, non_full_backward_hooks = [], []
  
  ~\AppData\Local\Temp/ipykernel_18276/1566085253.py in forward(self, x)
        8 
        9     def forward(self,x):
  ---&gt; 10         x = self.conv1(x)
       11         x =F.max_pool2d(x,2)
       12         x =F.relu(x)
  
  ~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
     1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
     1109                 or _global_forward_hooks or _global_forward_pre_hooks):
  -&gt; 1110             return forward_call(*input, **kwargs)
     1111         # Do not call functions when jit is used
     1112         full_backward_hooks, non_full_backward_hooks = [], []
  
  ~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
      445 
      446     def forward(self, input: Tensor) -&gt; Tensor:
  --&gt; 447         return self._conv_forward(input, self.weight, self.bias)
      448 
      449 class Conv3d(_ConvNd):
  
  ~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in _conv_forward(self, input, weight, bias)
      441                             weight, bias, self.stride,
      442                             _pair(0), self.dilation, self.groups)
  --&gt; 443         return F.conv2d(input, weight, bias, self.stride,
      444                         self.padding, self.dilation, self.groups)
      445 
  
  RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [64, 1, 3, 256, 256]
  </code></pre>
  <p>I'dont know what is wrong and hope you can help me.
  Thank you in forward.</p>
  <p>My Code looks like this.
  In the first step a get the images out of my folder and append the label. Additionally, I bring the images to the same size of 256x256 centercrop.</p>
  <pre class="lang-python prettyprint-override"><code>import torch
  import torchvision 
  from torchvision import transforms
  from PIL import Image
  from os import listdir
  import random
  import torch.optim as optim
  from torch.autograd import Variable
  import torch.nn.functional
  import torch.nn as nn
  
  normalize = transforms.Normalize( # Normalisierung, da die Bilder auch überbelichtet sein
  mean = [0.485, 0.456, 0.406],
  std = [0.229, 0.224, 0.225]
  )
  transform = transforms.Compose([
      transforms.Resize(256),
      transforms.CenterCrop(256),
      transforms.ToTensor(),
      normalize])
  
  #TARGET [isCat, isDog]
  
  target_list = []
  train_data_list = []
  train_data = []
  files = listdir('data/train/')
  for i in range(len(listdir('data/train/'))):
      
      f = random.choice(files)
      files.remove(f)
      img = Image.open(&quot;data/train/&quot; + f)
      img_tensor = transform(img)
      train_data_list.append(img_tensor)
      img_tensor.unsqueeze_(0) 
      isCat = 1 if 'cat' in f else 0
      isDog = 1 if 'dog' in f else 0
      target = [isCat, isDog]
      target_list.append(target)
      if len(train_data_list) &gt;= 64:
          train_data.append((torch.stack(train_data_list), target_list)) 
          train_data_list = []
          break
  print(train_data)
  
  </code></pre>
  <p>In this code block I create the &quot;Netz&quot; and train the model.</p>
  <pre class="lang-python prettyprint-override"><code>
  #MAIN TRAIN LOOP
  class Netz(nn.Module):
      def __init__(self):
          super(Netz,self).__init__()
          self.conv1 = nn.Conv2d(3,6, kernel_size = 5)
          self.conv2 = nn.Conv2d(6,12, kernel_size =5)
          self.conv3 = nn.Conv2d(12,18, kernel_size =5)
      
      def forward(self,x):
          x = self.conv1(x)
          x =F.max_pool2d(x,2)
          x =F.relu(x)
          x = self.conv2(x)
          x =F.max_pool2d(x,2)
          x =F.relu(x)
          x = self.conv3(x)
          x = F.max_pool2d(x,2)
          x =F.relu(x)
          print(x.size())      
          exit()
          
  model = Netz()
  #model.cuda()
  optimizer = optim.Adam(model.parameters(), lr = 0.01)
  
  
  def train(epoch):
      model.train()
      batch_id = 0
      for data, target in train_data:
          #data = data.cuda()
          target = torch.Tensor(target)
          #target = target.cuda()
          data = Variable(data)
          target = Variable(data)
          optimizer.zero_grad()
          out = model(data)
          criterion = F.nll_loss
          loss = criterion(out,target)
          lass.backward()
          optimizer.step()
          batch_id += 1
          print('Tain Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(epoch,batch_id*len(data), len(train_data), 100*batch_id/len(train_data), loss.item()))
          break
  
  for epoch in range(1,30):
      train(epoch)
      break        
  
  </code></pre>