Stack Exchange
PyTorch Runtime error: Given groups=1, weight of size [16, 395, 3], expected input[1, 16, 395] to have 395 channels, but got 16 channels instead
So I'm trying to build a classifier for 1d signals as follows:
class M5(nn.Module):
    def __init__(self, n_input=1, n_output=3, stride=4, n_channel=16):
        super().__init__()
        self.conv1 = nn.Conv1d(n_input, n_channel, kernel_size=3)
        self.bn1 = nn.BatchNorm1d(n_channel)
        self.pool1 = nn.MaxPool1d(3)
        self.conv2 = nn.Conv1d(n_channel, n_channel, kernel_size=2)
        self.bn2 = nn.BatchNorm1d(n_channel)
        self.pool2 = nn.MaxPool1d(2)
        self.conv3 = nn.Conv1d(n_channel, 2 * n_channel, kernel_size=2)
        self.bn3 = nn.BatchNorm1d(2 * n_channel)
        self.pool3 = nn.MaxPool1d(2)
        self.conv4 = nn.Conv1d(2 * n_channel, 2 * n_channel, kernel_size=2)
        self.bn4 = nn.BatchNorm1d(2 * n_channel)
        self.pool4 = nn.MaxPool1d(2)
        self.fc1 = nn.Linear(2 * n_channel, n_output)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(self.bn1(x))
        x = self.pool1(x)
        x = self.conv2(x)
        x = F.relu(self.bn2(x))
        x = self.pool2(x)
        x = self.conv3(x)
        x = F.relu(self.bn3(x))
        x = self.pool3(x)
        x = self.conv4(x)
        x = F.relu(self.bn4(x))
        x = self.pool4(x)
        x = F.avg_pool1d(x, x.shape[-1])
        x = x.permute(0, 1)
        x = self.fc1(x)
        return F.log_softmax(x, dim=2)

model = M5(n_input=ex.shape[0], n_output=len(labels))

ex.shape[0] here is 395, len(labels) corresponds to the number of unique classes. My input data is in a batch of size 16 and length of each tensor in the batch is 395. The error indicates that the first layer of my NN does not get the appropriate sized input but I for the life of me cannot figure out why not. Can anyone advise?

Inputs to conv1d should have a shape of (N,Cin​,L), while your inputs is of shape (N, 395). Thus, you need to add an extra dim. I converted your inputs into shape of (N, 1, 395) as follows:
import torch
from torch import nn
from torch.nn import functional as F
class M5(nn.Module):
    def __init__(self, n_input=1, n_output=3, stride=4, n_channel=16):
        super().__init__()
        self.conv1 = nn.Conv1d(n_input, n_channel, kernel_size=3)
        self.bn1 = nn.BatchNorm1d(n_channel)
        self.pool1 = nn.MaxPool1d(3)
        self.conv2 = nn.Conv1d(n_channel, n_channel, kernel_size=2)
        self.bn2 = nn.BatchNorm1d(n_channel)
        self.pool2 = nn.MaxPool1d(2)
        self.conv3 = nn.Conv1d(n_channel, 2 * n_channel, kernel_size=2)
        self.bn3 = nn.BatchNorm1d(2 * n_channel)
        self.pool3 = nn.MaxPool1d(2)
        self.conv4 = nn.Conv1d(2 * n_channel, 2 * n_channel, kernel_size=2)
        self.bn4 = nn.BatchNorm1d(2 * n_channel)
        self.pool4 = nn.MaxPool1d(2)
        self.fc1 = nn.Linear(2 * n_channel, n_output)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(self.bn1(x))
        x = self.pool1(x)
        x = self.conv2(x)
        x = F.relu(self.bn2(x))
        x = self.pool2(x)
        x = self.conv3(x)
        x = F.relu(self.bn3(x))
        x = self.pool3(x)
        x = self.conv4(x)
        x = F.relu(self.bn4(x))
        x = self.pool4(x)
        x = F.avg_pool1d(x, x.shape[-1]).squeeze()
        # x = x.permute(0, 1)  ## commented this line
        x = self.fc1(x)
        return F.log_softmax(x, dim=-1) # pass dim=-1 as last dim

model = M5(n_input=1, n_output=10)
inputs = torch.randn(16, 395)
model(inputs.unsqueeze(1)).shape


Thank you so much, I will be trying this tomorrow and will let you know.

This leads to a problem with the fully connected layer at the end. 'mat1 and mat2 cannot be multiplied (512x1 and 32x3)'. Do you have any idea why this mismatch exists?

I ran the code I provided, it's working. Please edit the question and include you last code, with the shape of the inputs.

I realized i made a silly typo, my bad and thank you for the answer
