CNN model pytorch

I tested this code

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        

        self.features1 = nn.Sequential(
            nn.Conv2d(1, 6, 3, 1, 1),
            nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(6,6, 3, 1, 1),
            nn.ReLU(),
            nn.MaxPool2d(2)
        )
      
        self.fc_out = nn.Linear((6), 2)
        
    def forward(self, x1):
        x1 = self.features1(x1)
        x1 = x1.view(x1.size(0), -1)
        x1 = F.relu(self.fc_out(x1))
        x1 = self.fc_out(x1)
        return x1

but this problem is displayed

RuntimeError: mat1 and mat2 shapes cannot be multiplied (6451x6 and 32x2)

help me plzzz

the problem in layer conv?

There are two problems with this code,

  • You are applying self.fc_out layer two times in your forward function,
x1 = F.relu(self.fc_out(x1))
x1 = self.fc_out(x1)
  • If after correcting the above, the error comes up it might be because of number of input neurons in self.fc_out. To fix it, comment out self.fc_out layer totally from init and forward, pass images through the model and print the resultant shape, the last dim should be your number of input_neurons, put it back and now you have fixed your self.fc_out.

Hi csblacknet, thanks for this response. But I didn’t quite understand what I am changing in the

code

how do i change my code?

help me plzz!! :slightly_smiling_face:

Try this one, the correct code is here,

thanks csblacknet :slightly_smiling_face:

i will test it :slightly_smiling_face:

CSblacknet how to display the final image when i do the test and train?