Help with simple CNN

Sam_Lerman: <p>I’m getting this error from a simple CNN construction:</p>
<p><code>RuntimeError: Expected 3-dimensional tensor, but got 5-dimensional tensor for argument #1 'self' (while checking arguments for avg_pool1d)</code></p>
<p>I’m sure I implemented something wrong.</p>
<p>The CNN is defined:</p>
<pre><code class="lang-auto">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),
            nn.Flatten(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
</code></pre>

aza: <p>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</p>

Sam_Lerman: <p>It should just be a single 1800-length 1D array. I just tried changing the dimension to (1, 1800) and got a different error:</p>
<p><code>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</code></p>

aza: <p>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)</p>
<blockquote>
<p>conv = nn.Conv1d(1, 80, kernel_size=3, stride=5)</p>
</blockquote>
<p>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 <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Take a look at the docs to check/confirm that you are using items correctly: <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html" class="inline-onebox" rel="noopener nofollow ugc">Conv1d — PyTorch 2.1 documentation</a></p>

Sam_Lerman: <p>Oh, hmm… that doesn’t work, I’m trying to reproduce this architecture:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd5c35862e9204e139ad1eeaa1f4ea011257a27a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cd5c35862e9204e139ad1eeaa1f4ea011257a27a" title="Screen Shot 2021-11-01 at 2.24.53 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd5c35862e9204e139ad1eeaa1f4ea011257a27a_2_660x500.jpeg" alt="Screen Shot 2021-11-01 at 2.24.53 PM" data-base62-sha1="tiHrbwyaJfMciwgSUvzfO89Zoee" width="660" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd5c35862e9204e139ad1eeaa1f4ea011257a27a_2_660x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd5c35862e9204e139ad1eeaa1f4ea011257a27a_2_990x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd5c35862e9204e139ad1eeaa1f4ea011257a27a_2_1320x1000.jpeg 2x" data-dominant-color="F5F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2021-11-01 at 2.24.53 PM</span><span class="informations">1736×1314 78.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>