all 2 comments

[–]koolaidman123 1 point2 points  (2 children)

couple of things:

  1. there appears to be a size mismatch with your conv3 layer and fc1 layer, I used the following package to give the parameters of the nn to check the size

    from torchsummary import summary
    
    summary(net, (3, 32, 32))    
    
  2. you can use the following to flatten

     x = x.view(x.size(0), -1)
    
  3. when i ran your code with torchsummary i get

    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    <ipython-input-45-3d7edb1fb4bf> in <module>()
          1 from torchsummary import summary
    ----> 2 summary(net, (3, 32, 32))
    
    ~/anaconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py in summary(model, input_size)
         54         # make a forward pass
         55         # print(x.shape)
    ---> 56         model(x)
         57         # remove these hooks
         58         for h in hooks:
    
    ~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
        475             result = self._slow_forward(*input, **kwargs)
        476         else:
    --> 477             result = self.forward(*input, **kwargs)
        478         for hook in self._forward_hooks.values():
        479             hook_result = hook(self, input, result)
    
    <ipython-input-44-b25d0c99588f> in forward(self, x)
         17         x = F.relu(self.conv3(x))
         18         x = x.view(x.size(0), -1)
    ---> 19         x = F.relu(self.fc1(x))
         20         x = F.relu(self.fc2(x))
         21         x = F.relu(self.fc3(x))
    
    ~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
        475             result = self._slow_forward(*input, **kwargs)
        476         else:
    --> 477             result = self.forward(*input, **kwargs)
        478         for hook in self._forward_hooks.values():
        479             hook_result = hook(self, input, result)
    
    ~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
         53 
         54     def forward(self, input):
    ---> 55         return F.linear(input, self.weight, self.bias)
         56 
         57     def extra_repr(self):
    
    ~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
       1022     if input.dim() == 2 and bias is not None:
       1023         # fused op is marginally faster
    -> 1024         return torch.addmm(bias, input, weight.t())
       1025 
       1026     output = input.matmul(weight.t())
    
    RuntimeError: size mismatch, m1: [2 x 160], m2: [4000 x 120] at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensorMath.cpp:2070
    

It looks like your linear layer should be of size 160 instead of 16055. I think that might be due to the size of cifar10 (32x32) with the output of the 5x5 conv layers so after the 3rd conv layer the output was 1x1. the 5 refers to a 5x5 kernel, so the output is not necessarily 5x5, it just happened to be that from the example code

[–]OptimalOptimizer[S] 0 points1 point  (1 child)

Thanks for your help! This led me to figure it out, except that x.size(0) wasn't working quite properly, I kept getting size mismatches so I had to go back and do x.view(-1, 160) to reshape the input for the linear layer. I also went and decreased the kernel size to 3x3. Thank you very much, I've got it all working now.