Class Net module Layers size missmatch Error

username: Kiki
cooked: <p>Hello, I’m new at pytroch and I’m currently working on my first cnn for image recognition. My images have the size 28*28 and are gray, so they have a channel of 1. I get a message every time that i have a size mismatch. I can’t figure out what is wrong. Can anyone help me? I<br>
<strong>Error Code:</strong><br>
Traceback (most recent call last):</p>
<p>File “C:\Users\jessi\Downloads\Code (1)\BA\main1.py”, line 238, in <br>
train(model, device, train_loader, optimizer, epoch)</p>
<p>File “C:\Users\jessi\Downloads\Code (1)\BA\main1.py”, line 183, in train<br>
output = model(data)</p>
<p>File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl<br>
result = self.forward(*input, **kwargs)</p>
<p>File “C:\Users\jessi\Downloads\Code (1)\BA\main1.py”, line 62, in forward<br>
x = self.fc1(x)</p>
<p>File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl<br>
result = self.forward(*input, **kwargs)</p>
<p>File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\modules\linear.py”, line 91, in forward<br>
return F.linear(input, self.weight, self.bias)</p>
<p>File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1674, in linear<br>
ret = torch.addmm(bias, input, weight.t())</p>
<p>RuntimeError: size mismatch, m1: [1 x 512], m2: [256 x 128] at …\aten\src\TH/generic/THTensorMath.cpp:41</p>
<pre><code class="lang-auto">from __future__ import print_function
import numpy as np
import pandas as pd
import torch
from PIL import Image
from torch.utils.data.dataset import Dataset
from torchvision import transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR



class Net(nn.Module):
    def __init__(self):
        # init is the constructor for a class.The self parameter refers to the instance of the object
        super(Net, self).__init__()
        
        self.conv1 = nn.Conv2d(1, 32, kernel_size=(5,5))
        #3 Channels da buntes Bild
        torch.nn.init.ones_(self.conv1.weight)
        self.pool1 = nn.MaxPool2d(2,2)
        
        self.conv2 = nn.Conv2d(32, 64, kernel_size=(5,5))
        self.pool2 = nn.MaxPool2d(2,2)
        self.conv3 = nn.Conv2d(64, 128,kernel_size=(5,5))
        self.conv4 = nn.Conv2d(128, 64,kernel_size=(5,5))
        self.conv5 = nn.Conv2d(64, 32, kernel_size=(5,5))
        self.conv6 = nn.Conv2d(32, 16, kernel_size=(5,5))
        self.pool3 = nn.MaxPool2d(2,2)
        self.conv7 = nn.Conv2d(16, 32, kernel_size=(5,5))
        self.conv8 = nn.Conv2d(32, 64, kernel_size=(5,5))
        
        #self.dropout1 = nn.Dropout2d(0.25)
        #self.dropout2 = nn.Dropout2d(0.5)
        
        # ((I - K + 2*P) / S) + 1
        self.fc1 = nn.Linear(64*2*2, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3= nn.Linear(64, 32)
        self.fc4= nn.Linear(32, 16)
        self.fc5= nn.Linear(16, 8)
        self.fc6= nn.Linear(8, 4)
        self.fc7= nn.Linear(4, 2)
        self.fc8= nn.Linear(2, 1)
        

    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 = torch.flatten(x, 1)
        
        x = self.fc1(x)
        x = F.relu(x)
        
        x = self.fc2(x)
        x = F.relu(x)
        
        x = self.fc3(x)
        x = F.relu(x)
        
        x = self.fc4(x)
        x = F.relu(x)
        
        x = self.fc5(x)
        x = F.relu(x)
        
        x = self.fc6(x)
        x = F.relu(x)
        
        x = self.fc7(x)
        x = F.relu(x)
        
        x = self.fc8(x)
        
        '''
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        '''
        
        #output = F.log_softmax(x, dim=1)
        return x

</code></pre>

username: ptrblck
cooked: <p>Check the shape of the activation after flattening it and before passing it to <code>self.fc1</code>:</p>
<pre><code class="lang-python">x = torch.flatten(x, 1)
print(x.shape)
</code></pre>
<p>This should give you a shape as <code>[batch_size, num_features]</code>, where <code>num_features</code> should be equal to the input features of <code>self.fc1</code>.<br>
Based on the error message you are seeing I guess <code>x</code> would have the shape <code>[batch_size, 512]</code>, while you set <code>in_features=256</code> for <code>self.fc1</code>, which causes the shape mismatch.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Kiki
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="99986">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>This should give you a shape as <code>[batch_size, num_features]</code>, where <code>num_features</code> should be equal to the input features of <code>self.fc1</code>.</p>
</blockquote>
</aside>
<p>Thank you so much! It’s working !</p>