Mismatched dimensions

gar: <p>I’m trying to make a CNN audio classification model</p>
<pre><code class="lang-auto">class AudioClassification(nn.Module):
    def __init__(self, input_count,output_count, channel_count=32, stride=16 ):
        super().__init__()
        self.conv1 = nn.Conv1d(input_count, channel_count, kernel_size=80, stride=stride)
        self.bn1 = nn.BatchNorm1d(channel_count)
        self.pool1 = nn.MaxPool1d(4)
        self.conv2 = nn.Conv1d(channel_count, channel_count, kernel_size=3)
        self.bn2 = nn.BatchNorm1d(channel_count)
        self.pool2 = nn.MaxPool1d(4)
        self.fc1 = nn.Linear(2 * channel_count, output_count)
    
    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.fc1(x)
        return F.log_softmax(x, dim=2)

def train(model, epoch, log_interval):
    model.train()
    for batch_count, (data, target) in enumerate(train_loader):
        data = transform(data)
        output = model(data)
        
        loss = F = F.nll_loss(output.squeeze(), target)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        if batch_count % log_interval == 0:
            print(f"Train Epoch: {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)}({100. * batch_idx / len(train_loader):.0f}%)]\tLoss: {loss.item():.6f}")
        pbar.update(pbar_update)
        losses.append(loss.item())
</code></pre>
<p>and I’m running into the following error:<br>
RuntimeError: Expected 3-dimensional input for 3-dimensional weight [32, 22050, 80], but got 2-dimensional input of size [32, 22050] instead</p>
<p>I understand its from the kernel_size but I’m not sure how to change my data to include it</p>

KFrank: <p>Hi Gar!</p>
<aside class="quote no-group quote-modified" data-username="gar" data-post="1" data-topic="170225" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/db5fbb/48.png" class="avatar"> gar:</div>
<blockquote>
<pre><code class="lang-auto">class AudioClassification(nn.Module):
    def __init__(self, input_count,output_count, channel_count=32, stride=16 ):
        super().__init__()
        self.conv1 = nn.Conv1d(input_count, channel_count, kernel_size=80, stride=stride)
</code></pre>
<p>RuntimeError: Expected 3-dimensional input for 3-dimensional weight [32, 22050, 80], but got 2-dimensional input of size [32, 22050] instead</p>
</blockquote>
</aside>
<p>When you pass an <code>input</code> to a <code>Conv1d</code>, it is expected to have shape<br>
<code>[nBatch, channels, length]</code>, where <code>channels</code> matches the<br>
<code>in_channels</code> with which you instantiated the <code>Conv1d</code>.  (<code>nBatch</code> and<br>
<code>length</code> are not required by a given <code>Conv1d</code> to have specific values.)</p>
<p>It looks like you are passing a two-dimensional tensor into your model<br>
that presumably lacks an <code>nBatch</code> dimension.  <code>nBatch</code> can be <code>1</code>, but<br>
the <code>nBatch</code> dimension has to be there.</p>
<p>It looks like the <code>length</code> of your input sample is <code>22050</code>.  That’s fine.</p>
<p>I believe that the main problem is with <code>conv1</code>, the first <code>Conv1d</code> in your<br>
model, and that you are mixing up the <code>length</code> of your <code>input</code> with the<br>
with the number of input channels (<code>in_channels</code>) expected by <code>conv1</code>.</p>
<p>It appears that you are instantiating <code>conv1</code> as:</p>
<pre data-code-wrap="python"><code class="lang-python">self.conv1 = nn.Conv1d (in_channels = 22050, out_channels = 32, kernel_size = 80)
</code></pre>
<p>If the input to your model really does have 32 channels, then you would<br>
want something like:</p>
<pre data-code-wrap="python"><code class="lang-python">self.conv1 = nn.Conv1d (in_channels = 32, out_channels = 32, kernel_size = 80)
</code></pre>
<p>(It’s perfectly reasonable to have <code>in_channels</code> and <code>out_channels</code> be the<br>
same, but they don’t have to be.  However, <code>in_channels</code> does have to be<br>
the same as the number channels of the input passed into your model.)</p>
<p>What is the shape of the input to your <code>model</code> (<code>data</code>, in your training loop)?<br>
How many channels does it have?  What is the meaning and value of<br>
<code>channel_count</code> – used internally by your model?</p>
<p>Best.</p>
<p>K. Frank</p>