RuntimeError: Given input size: (32x20x1). Calculated output size: (32x10x0). Output size is too small

vin: <p>While running my script below, I get the error message, indicating a mismatch in the spatial structure of my CNN network during training. Mentioning an incoherence of the MaxPool at the activation function of the second CNN (conv2). Here is the shape of my tensor (torch.Size([157056, 40, 2])), which indicates the shape of the collected audio data after transformation.<br>
How to solve the problem? Need your help. Thanks<br>
Here is my:<br>
in_features = 576<br>
out_features = 200<br>
last_feature = 10</p>
<pre><code class="lang-auto">class CNNLetNet(nn.ModuleList):
    def __init__(self):
        super(CNNLetNet, self).__init__()
        """
         @ defining four cnn network to perform our data 
         in a set of sequential layers
        """
        self.conv1 = nn.Sequential(
            nn.Conv2d(1, 16, 3, stride=1, padding=1)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 3, stride=1, padding=1)
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(32, 64, 3, stride=1, padding=1)
        )
        self.fc1 = nn.Sequential(
            nn.Linear(64 * 3 * 3, 200)
        )
        self.fc2 = nn.Sequential(
            nn.Linear(200, 60)
        )
        self.fc3 = nn.Sequential(
            nn.Linear(60, 10)
        )
        self.pool = nn.MaxPool2d(2, 2)

    def forward(self, input_data):
        input_data = self.pool(F.relu(self.conv1(input_data)))
        input_data = self.pool(F.relu(self.conv2(input_data)))
        input_data = self.pool(F.relu(self.conv3(input_data)))
        input_data = torch.flatten(input_data, 1)
        input_data = F.relu(self.fc1(input_data))
        input_data = F.relu(self.fc2(input_data))
        input_data = self.fc3(input_data)
        return input_data

</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/0/00a92bb988ff4cd7e13c23161aa293e5fff30381.png" data-download-href="https://discuss.pytorch.org/uploads/default/00a92bb988ff4cd7e13c23161aa293e5fff30381" title="微信截图_20220421190722"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/0/00a92bb988ff4cd7e13c23161aa293e5fff30381_2_690x296.png" alt="微信截图_20220421190722" data-base62-sha1="5QrHn4OM9gVyzn6cYUrGUFUPZv" width="690" height="296" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/0/00a92bb988ff4cd7e13c23161aa293e5fff30381_2_690x296.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/0/00a92bb988ff4cd7e13c23161aa293e5fff30381_2_1035x444.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/0/00a92bb988ff4cd7e13c23161aa293e5fff30381_2_1380x592.png 2x" data-dominant-color="302F33"><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">微信截图_20220421190722</span><span class="informations">1611×693 97.3 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>

ptrblck: <p>The spatial size of your input is too small for the model architecture and one of the pooling layers would create an empty tensor.<br>
Remove some pooling layers or increase the spatial size of the input.</p>

vin: <p>Thanks, Mr. Now I have reshaped my data and reduced the size number. The shape is now <code>torch.Size([30, 100, 100])</code> with 30 matrix of <code>100*100</code>. The error raises the following message:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/7/172c3ea0b8a04302dd995712ed93327084ce1fae.png" data-download-href="https://discuss.pytorch.org/uploads/default/172c3ea0b8a04302dd995712ed93327084ce1fae" title="55"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/172c3ea0b8a04302dd995712ed93327084ce1fae_2_690x310.png" alt="55" data-base62-sha1="3iZM577sP3ccHa1Y1847FBPtLY2" width="690" height="310" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/172c3ea0b8a04302dd995712ed93327084ce1fae_2_690x310.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/172c3ea0b8a04302dd995712ed93327084ce1fae_2_1035x465.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/7/172c3ea0b8a04302dd995712ed93327084ce1fae_2_1380x620.png 2x" data-dominant-color="2F2F33"><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">55</span><span class="informations">1500×675 90.6 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>
<pre><code class="lang-auto">self.conv1 = nn.Sequential(
            nn.Conv2d(1, 16, 3, stride=1, padding=1)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 3, stride=1, padding=1)
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(32, 64, 3, stride=1, padding=1)
        )
        self.fc1 = nn.Sequential(
            nn.Linear(64 * 3 * 3, 250)
        )
        self.fc2 = nn.Sequential(
            nn.Linear(250, 150)
        )
        self.fc3 = nn.Sequential(
            nn.Linear(150, 80)
        )
        self.fc4 = nn.Sequential(
            nn.Linear(80, 10)
        )
        self.pool = nn.Sequential(
            nn.MaxPool2d(2, 2)
        )

    def forward(self, input_data):
        input_data = self.pool(F.relu(self.conv1(input_data)))
        input_data = self.pool(F.relu(self.conv2(input_data)))
        input_data = self.pool(F.relu(self.conv3(input_data)))
        input_data = torch.flatten(input_data, 1)
        input_data = F.relu(self.fc1(input_data))
        input_data = F.relu(self.fc2(input_data))
        input_data = F.relu(self.fc3(input_data))
        input_data = self.fc4(input_data)
        return input_data
</code></pre>

ptrblck: <p>The <code>in_features</code> in <code>self.fc1</code> are set to a wrong value (<code>64 * 3 * 3</code> instead of <code>9216</code> for this input shape).</p>

vin: <p>So, how can I modify it? Need your suggestions. Thanks</p>

ptrblck: <p>Change the <code>in_features</code> to <code>9216</code>.</p>

vin: <p>Thank you for your help.</p>

vin: <p>Another issue raising. While training the model, it seems the data to be a tuple not a tensor. During preprocessing I have convert the numpy() into tensor by <code> signal = transforms.ToTensor()(np.array(signal)).float()</code>, this seems to be correct right? So, how can I make it works:</p>
<pre><code class="lang-auto">     for epoch in range(epochs):
        model.train()
        running_loss = 0.0
        for i, data in enumerate(trainloader, 0):
            target, labels = data

            outputs, input_data = model(target)
            loss = criterion(outputs, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            running_loss += loss.item()
            if i % 100 == 0:
               print(f"nEpochs : {epoch + 1} | nSteps : {i + 1}/ {ntotal_len} | nLoss : {running_loss / 100}")
            print("Finished training")

</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ff96f619b08799147ed57bf31c7b7a04f405082c.png" data-download-href="https://discuss.pytorch.org/uploads/default/ff96f619b08799147ed57bf31c7b7a04f405082c" title="66"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff96f619b08799147ed57bf31c7b7a04f405082c_2_690x181.png" alt="66" data-base62-sha1="At3bF8zlcP6WNlDWTbsWAHxNraI" width="690" height="181" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff96f619b08799147ed57bf31c7b7a04f405082c_2_690x181.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff96f619b08799147ed57bf31c7b7a04f405082c_2_1035x271.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff96f619b08799147ed57bf31c7b7a04f405082c_2_1380x362.png 2x" data-dominant-color="312F33"><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">66</span><span class="informations">1639×432 62.7 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>

ptrblck: <p>Check why <code>labels</code> is a <code>tuple</code> and try to pass it as a tensor to the criterion instead.<br>
I don’t know why it’s a <code>tuple</code> currently, so cannot suggest a proper solution.</p>

vin: <p>Thank you Sr. Solved</p>