RuntimeError: Given groups=1, weight of size [8, 16, 3, 3], expected input[64, 32, 13, 13] to have 16 channels, but got 32 channels instead

username: xsquared
cooked: <p>Im getting the error</p>
<p><code>RuntimeError: Given groups=1, weight of size [8, 16, 3, 3], expected input[64, 32, 13, 13] to have 16 channels, but got 32 channels instead</code></p>
<p>Here is the network:</p>
<pre><code class="lang-auto">class Net(nn.Module):
    """A representation of a convolutional neural network comprised of VGG blocks."""
    def __init__(self, n_channels):
        super(Net, self).__init__()
        # VGG block 1
        self.conv1 = nn.Conv2d(n_channels, 64, (3,3))
        self.act1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d((2,2), stride=(2,2))
        # VGG block 2
        self.conv2 = nn.Conv2d(64, 64, (3,3))
        self.act2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d((2,2), stride=(2,2))
        # VGG block 3
        self.conv3 = nn.Conv2d(64, 128, (3,3))
        self.act3 = nn.ReLU()
        self.pool3 = nn.MaxPool2d((2,2), stride=(2,2))
        # Fully connected layer
        self.f1 = nn.Linear(8192, 1000)
        self.act4 = nn.ReLU()
        # Output layer
        self.f2 = nn.Linear(1000, 10)
        self.act5 = nn.Softmax(dim=1)

    def forward(self, X):
        """This function forward propagates the input."""
        # VGG block 1
        X = self.conv1(X)
        X = self.act1(X)
        X = self.pool1(X)
        # VGG block 2
        X = self.conv2(X)
        X = self.act2(X)
        X = self.pool2(X)
        # VGG block 3
        X = self.conv3(X)
        X = self.act3(X)
        X = self.pool3(X)
        print(X.shape)
        # Flatten
        X = X.view(-1, 8192)
        # Fully connected layer
        X = self.f1(X)
        X = self.act4(X)
        # Output layer
        X = self.f2(X)
        X = self.act5(X)

        return X
        
</code></pre>
<p>Why am I getting the above error?</p>
<p>Could you please provide an explanation with your answer? thanks.</p>

username: Eta_C
cooked: <p>Before</p>
<pre><code class="lang-python"># VGG block 3
self.conv2 = nn.Conv2d(16, 8, (3,3))
self.act3 = nn.ReLU()
self.pool3 = nn.MaxPool2d((2,2), stride=(2,2))
</code></pre>
<p>After</p>
<pre><code class="lang-python"># VGG block 3
self.conv3 = nn.Conv2d(16, 8, (3,3))
self.act3 = nn.ReLU()
self.pool3 = nn.MaxPool2d((2,2), stride=(2,2))
</code></pre>

username: ptrblck
cooked: <p>You are replacing <code>self.conv2</code>:</p>
<pre><code class="lang-python">        self.conv2 = nn.Conv2d(32, 16, (3,3))
        self.act2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d((2,2), stride=(2,2))
        # VGG block 3
        self.conv2 = nn.Conv2d(16, 8, (3,3))
</code></pre>
<p>The second <code>self.conv2</code> definition should most likely be <code>self.conv3</code>.</p>
<p>EDIT: a few seconds too late! <a class="mention" href="/u/eta_c">@Eta_C</a> <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=9" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:"></p>

username: xsquared
cooked: <p>yeah i noticed that typo too and i fixed it. But im having trouble determining the linear’s channels? I have written 8192 but its wrong. Im working on the mnist dataset whose images have the following shape: 1,28,28. I edited the network i posted.</p>

username: Eta_C
cooked: <p>Choose one:</p>
<ol>
<li>resize the input from (28, 28) to about (80, 80)</li>
<li>use 256 instead of 8192</li>
</ol>

username: xsquared
cooked: <p>i chose to change it to 256 but i got this error <code>ValueError: Expected input batch_size (32) to match target batch_size (64)</code></p>