RuntimeError: Expected 3-dimensional input for 3-dimensional weight [3, 1, 3], but got 2-dimensional input of size [64, 200] instead

hfdp: <p>Hello,</p>
<p>I am trying to run a CNN that looks like this:</p>
<pre><code class="lang-auto">&gt; class MyModel(nn.Module):
&gt;     def __init__(self):
&gt;         super(MyModel, self).__init__()
&gt;         self.conv1 = nn.Conv1d(1, 3, kernel_size=3) 
&gt;         self.conv2 = nn.Conv1d(3, 6, kernel_size=6) 
&gt;         self.conv3 = nn.Conv1d(6, 12, kernel_size=12) 
&gt;         self.conv4 = nn.Conv1d(12,24, kernel_size=24) 
&gt;         self.conv5 = nn.Conv1d(24,48, kernel_size = 48)
&gt;         self.conv_drop = nn.Dropout2d()
&gt;         self.conv6_transpose = nn.ConvTranspose1d(48,24, kernel_size = 48)
&gt;         self.conv7_transpose = nn.ConvTranspose1d(24,12, kernel_size = 24)
&gt;         self.conv8_transpose = nn.ConvTranspose1d(12,6, kernel_size = 12)
&gt;         self.conv9_transpose=nn.ConvTranspose1d(6,3, kernel_size = 6)
&gt;         self.conv10_transpose = nn.ConvTranspose1d(3,1, kernel_size = 3)
&gt;        
&gt;     
&gt;         self.bn1 = nn.BatchNorm1d(3)
&gt;         self.bn2 = nn.BatchNorm1d(6)
&gt;         self.bn3 = nn.BatchNorm1d(12)
&gt;         self.bn4 = nn.BatchNorm1d(24)
&gt;         self.bn5 = nn.BatchNorm1d(48)
&gt;         self.bn6 = nn.BatchNorm1d(24)
&gt;         self.bn7 = nn.BatchNorm1d(12)
&gt;         self.bn8 = nn.BatchNorm1d(6)
&gt;         self.bn9 = nn.BatchNorm1d(3)
&gt;         self.bn10 = nn.BatchNorm1d(1)
&gt;        
&gt;     
&gt;     def forward(self, x):
&gt;    
&gt;        #downsample
&gt;         out = self.conv1(x)
&gt;         out = F.relu(out)
&gt;         out = self.bn1(out)
&gt;         
&gt;         out = self.conv2(out)
&gt;         out = F.relu(out)
&gt;         out = self.bn2(out)
&gt;         
&gt;         out = self.conv3(out)
&gt;         out = F.relu(out)
&gt;         out = self.bn3(out)
&gt;         
&gt;         out = self.conv4(out)
&gt;         out = F.relu(out)
&gt;         out = self.bn4(out)
&gt;         
&gt;         #bottleneck
&gt;         out = self.conv5(out)
&gt;         out = F.relu(out)
&gt;         out = self.bn5(out)
&gt;         out = self.conv_drop(out)
&gt;         
&gt;         #upsample
&gt;         out = self.conv6_transpose(out)
&gt;         out = self.conv6_drop(out)
&gt;         out = F.relu(out)
&gt;         out = out.unsqueeze(1)
&gt;         out = self.bn6(out)
&gt;         
&gt;         out = self.conv7_transpose(out)
&gt;         out = self.conv_drop(out)
&gt;         out = F.relu(out)
&gt;         out = out.unsqueeze(1)
&gt;         out = self.bn7(out)
&gt;         
&gt;         out = self.conv8_transpose(out)
&gt;         out = self.conv_drop(out)
&gt;         out = F.relu(out)
&gt;         out = out.unsqueeze(1)
&gt;         out = self.bn8(out)
&gt;         
&gt;         out = self.conv9_transpose(out)
&gt;         out = self.conv_drop(out)
&gt;         out = F.relu(out)
&gt;         out = out.unsqueeze(1)
&gt;         out = self.bn9(out)
&gt;         
&gt;         out = self.conv10_transpose(out)
&gt;         out = self.conv_drop(out)
&gt;         out = F.relu(out)
&gt;         out = out.unsqueeze(1)
&gt;         out = self.bn10(out)
&gt;         
&gt;         return out
&gt;         
&gt;         
&gt; model = MyModel()
</code></pre>
<p>However now I ge the error: <strong>Expected 3-dimensional input for 3-dimensional weight [3, 1, 3], but got 2-dimensional input of size [64, 200] instead.</strong> Does anybody know what can be done now? I am relatively new to pytorch so help would be welcome <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>The input data is a tensor that needs to undergo several conv1d’s and transpose conv1d’s to downsample and upsample.</p>

ptrblck: <p>Based on the error message, it seems that the first conv layer raises this issue.<br>
Assuming your input of <code>[64, 200]</code> is using the dimensions <code>[batch_size, seq_length]</code>, then you should add the channel dimension via:</p>
<pre><code class="lang-python">input = input.unsqueeze(1)
</code></pre>
<p>Let me know, if that helps.</p>

hfdp: <p>Thanks, it helped.</p>
<p>However, now I get a message <strong>ValueError: expected 4D input (got 3D input)</strong><br>
Does this have to do with the fact that I am downsampling and then upsampling, meaning i have to unsqueeze them again?</p>

ptrblck: <p>This error is most likely thrown by <code>nn.Dropout2d()</code>, which expects an input of <code>[batch_size, channels, height, width]</code> to zero out entire channels.<br>
If you want to apply the same behavior to your use case, you could unsqueeze a fake spatial dimension, use the dropout layer, and squeeze the activation afterwards.</p>

florencelucey: <p>I am having a similar issue with some code I am working on. Where should [input = input.unsqueeze(1)] be inserted to add the channel dimension?</p>

ptrblck: <p>It depends on your use case and where an error is raised.<br>
E.g. if the model’s forward pass raises an error about a missing channel dimension, you would need to unsqueeze the input before feeding it to the model:</p>
<pre><code class="lang-python">output = model(input)
# error about a missing channel dimension

# fix
input = input.unsqueeze(0)
output = model(input)
</code></pre>