Class Net module Layers size missmatch Error

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
Error Code:
Traceback (most recent call last):

File “C:\Users\jessi\Downloads\Code (1)\BA\”, line 238, in
train(model, device, train_loader, optimizer, epoch)

File “C:\Users\jessi\Downloads\Code (1)\BA\”, line 183, in train
output = model(data)

File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\modules\”, line 722, in _call_impl
result = self.forward(*input, **kwargs)

File “C:\Users\jessi\Downloads\Code (1)\BA\”, line 62, in forward
x = self.fc1(x)

File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\modules\”, line 722, in _call_impl
result = self.forward(*input, **kwargs)

File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\modules\”, line 91, in forward
return F.linear(input, self.weight, self.bias)

File “C:\Users\jessi\anaconda3\lib\site-packages\torch\nn\”, line 1674, in linear
ret = torch.addmm(bias, input, weight.t())

RuntimeError: size mismatch, m1: [1 x 512], m2: [256 x 128] at …\aten\src\TH/generic/THTensorMath.cpp:41

from __future__ import print_function
import numpy as np
import pandas as pd
import torch
from PIL import Image
from 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
        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

Check the shape of the activation after flattening it and before passing it to self.fc1:

x = torch.flatten(x, 1)

This should give you a shape as [batch_size, num_features], where num_features should be equal to the input features of self.fc1.
Based on the error message you are seeing I guess x would have the shape [batch_size, 512], while you set in_features=256 for self.fc1, which causes the shape mismatch.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. :wink:

1 Like

Thank you so much! It’s working !