Given groups=1, weight of size 6 1 5 5, expected input[1, 6, 14, 14] to have 1 channels, but got 6 channels instead

I am new to pytorch and had a little trouble that i can’t solve,please help me.

My code:

import torch
import torch.nn as nn
import torch.nn.functional as F

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        
        self.fc1 = nn.Linear(16*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    
    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv1(x)), 2)
        x = x.view(x.size()[0], -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()
input = torch.randn(1, 1, 32, 32)
out = net(input)

and the error:

RuntimeError: Given groups=1, weight of size 6 6 5 5, expected input[1, 1, 32, 32] to have 6 channels, but got 1 channels instead

Thank you very much

Hi,

In your forward function you are calling conv1 layer 2 times. The second time, it should be conv2. This will fix your issue.

Thanks

Oh!!!Thank you very much!!!I am so stupid:joy: