Help with simple CNN

I’m getting this error from a simple CNN construction:

RuntimeError: Expected 3-dimensional tensor, but got 5-dimensional tensor for argument #1 'self' (while checking arguments for avg_pool1d)

I’m sure I implemented something wrong.

The CNN is defined:

class ConvNet1DPaper(nn.Module):
    def __init__(self, num_classes=7):
        super(ConvNet1DPaper, self).__init__()
        self.cnn = nn.Sequential(
            nn.Conv1d(1, 80, kernel_size=(100, 1, 1), stride=5),
            nn.AvgPool1d(kernel_size=(3, 1, 1), stride=2),
            nn.Conv1d(1, 80, kernel_size=(50, 1, 80), stride=5),
            nn.AvgPool1d(kernel_size=(3, 1, 1), stride=1),
            nn.Conv1d(1, 80, kernel_size=(25, 1, 80), stride=2),
            nn.AvgPool1d(kernel_size=(3, 1, 1), stride=1),
        self.fc = nn.Sequential(
            nn.Linear(880, 700),
            nn.Linear(700, 70),
            nn.Linear(70, num_classes))

    def forward(self, x):
        out = self.cnn(x)
        out = self.fc(out)
        return out

Can you please post the shape of the input going into the model You will need to modify either the dimensions of the input or play around with the parameters being used in your model instantiation

1 Like

It should just be a single 1800-length 1D array. I just tried changing the dimension to (1, 1800) and got a different error:

RuntimeError: Expected 5-dimensional input for 5-dimensional weight [80, 1, 100, 1, 1], but got 3-dimensional input of size [2, 1, 1800] instead

I see. One way to make sense of issues with a forward pass is to only take one individual piece at a time and confirm that your sample is compatible. In this case, the first convolution layer is a problem, if you were to use (randomly chosen kernel_size)

conv = nn.Conv1d(1, 80, kernel_size=3, stride=5)

Your sample would then be able to be passed through that single convolution layer. I’ll leave the parsing of the subsequent layers up to you :slight_smile:

Take a look at the docs to check/confirm that you are using items correctly: Conv1d — PyTorch 1.10.0 documentation

Oh, hmm… that doesn’t work, I’m trying to reproduce this architecture: