RuntimeError: Given weight of size 3 1 5 5, expected bias to be 1-dimensional with 3 elements, but got bias of size [6] instead

Your model seems to work fine with bias=False and bias=True:

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5,bias=False)
        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):
        out = self.conv1(x)
        out = F.relu(out)
        out = F.max_pool2d(out, 2)
        out = F.relu(self.conv2(out))
        out = F.max_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = F.relu(self.fc1(out))
        out = F.relu(self.fc2(out))
        out = self.fc3(out)
        return out

model = LeNet()
x = torch.randn(1, 1, 32, 32)
out = model(x)

If you are running the code in a notebook, make sure to initialize all cells before the actual model execution.

2 Likes