RuntimeError: Given groups=1, weight of size [32, 3, 128, 1], expected input[1, 2, 3, 155648] to have 3 channels, but got 2 channels instead

Ana_Martins: <p>Hi, I am trying to use a CNN with 2D convolutions on 1D data, because I am trying to deploy my model on an FPGA, and Conv1d is not supported by it. This is what my model looks like:</p>
<pre><code class="lang-auto">class Network(nn.Module):
    def __init__(self, d):
        super().__init__()

        self.d = d

        self.batchnorm = nn.BatchNorm1d(3)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool1d(kernel_size=(4, 1), stride=4)
        self.flatten = nn.Flatten()

        self.conv1 = nn.Conv2d(
            in_channels=3, out_channels=32, kernel_size=(128, 1)
        )
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(32, 1))
        self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(16, 1))
        self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=(32, 1))
        self.conv5 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(128, 1))

        self.fc1 = nn.Linear(59904, 128)
        self.fc2 = nn.Linear(128, 1)

    def forward(self, x):
        print(f"Input shape: {x.shape}")

        if self.d == "pc":
            x = self.batchnorm(x.type(torch.FloatTensor))
        else:
            x = self.batchnorm(x)

        print(f"Shape after batch normalization: {x.shape}")

        x = self.conv1(x)

        print(f"Shape after 1st convolution: {x.shape}")

        x = self.relu(x)
        x = self.maxpool(x)
        x = self.conv2(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.conv3(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.conv4(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.conv5(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)

        return x
</code></pre>
<p>And these are the shapes of the prints in the code:</p>
<pre><code class="lang-auto">Input shape: torch.Size([2, 3, 155648])
Shape after batch normalization: torch.Size([2, 3, 155648])
</code></pre>
<p>However, my error <code>RuntimeError: Given groups=1, weight of size [32, 3, 128, 1], expected input[1, 2, 3, 155648] to have 3 channels, but got 2 channels instead</code> is saying, from what I understand, that the input shape of conv1 has the shape (1, 2, 3, 155648), but I have no idea where the first dimension comes from.<br>
I have also tried to input x[0] to conv1 but it tells me I am inputting something of the shape (3, 155648) and it is expecting something 3D or 4D…</p>
<p>Does anyone have any tips?</p>

Ana_Martins: <p>ChatGPT got there first <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>The fix was adding this before the conv1 layer, since apparently Conv2d forces the input to be 4D and it was adding the dimension in the wrong place:</p>
<pre><code class="lang-auto"># Reshape input tensor to add dummy height dimension
        x = x.view(
            x.size(0),
            x.size(1),
            x.size(-1),
            1,
        )
</code></pre>