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

username: shafei
cooked: <p>class Network(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)<br>
self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5)</p>
<pre><code>    self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
    self.fc2 = nn.Linear(in_features=120, out_features=60)
    self.out = nn.Linear(in_features=60, out_features=10)
    
 

def forward(self,t):
    t = self.conv1(t)
    t = F.relu(self.conv1(t))
    t = F.max_pool2d(t, kernel_size=2, stride=2)
    
    t = self.conv2(t)
    t = F.relu(self.conv2(t))
    t = F.max_pool2d(t, kernel_size=2, stride=2)
    
    
    t = F.relu(self.fc1(t.reshape(-1, 12*4*4)))
    t = F.relu(self.fc2(t))
    t = self.out(t)
    
    return t 
</code></pre>
<p>network = Network()<br>
sample = next(iter(train_set))</p>
<p>image, label = sample<br>
image.shape</p>
<p>image.unsqueeze(0).shape</p>
<p>pred = network(image.unsqueeze(0))</p>
<p>don’t know why I am getting this error</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
----&gt; 1 pred = network(image.unsqueeze(0))</p>
<p>~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p> in forward(self, t)<br>
13     def forward(self,t):<br>
14         t = self.conv1(t)<br>
—&gt; 15         t = F.relu(self.conv1(t))<br>
16         t = F.max_pool2d(t, kernel_size=2, stride=2)<br>
17</p>
<p>~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)<br>
343<br>
344     def forward(self, input):<br>
–&gt; 345         return self.conv2d_forward(input, self.weight)<br>
346<br>
347 class Conv3d(_ConvNd):</p>
<p>~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)<br>
340                             _pair(0), self.dilation, self.groups)<br>
341         return F.conv2d(input, weight, self.bias, self.stride,<br>
–&gt; 342                         self.padding, self.dilation, self.groups)<br>
343<br>
344     def forward(self, input):</p>
<p>RuntimeError: Given groups=1, weight of size 6 1 5 5, expected input[1, 6, 24, 24] to have 1 channels, but got 6 channels instead</p>

username: ptrblck
cooked: <p>You are accidentally reusing <code>self.conv1</code> (and <code>self.conv2</code>), which will throw this error, so you should remove the first usage.</p>