Can anyone point out the error in dimensions of permute?

krishna511: <p>This is the model I m referring</p>
<pre><code class="lang-auto">class CNN_spec(torch.nn.Module):
     def __init__(self, num_classes=7):
        super(CNN_spec, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=3,stride=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3,stride=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=4, stride=4))
        self.layer3 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3,stride=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=4, stride=4))
        self.layer4 = nn.Sequential(
            nn.Conv2d(128, 128, kernel_size=3,stride=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=4, stride=4))
        self.layer5 = nn.LSTM(128,1000 )
        self.emotion_layer = nn.Linear(2000,num_classes)
       

     def forward(self,inputs): 
         out = self.layer1(inputs)
         out = self.layer2(out)
         out = self.layer3(out)
         out = self.layer4(out)
         out = out.permute(0, 2, 1)
         out, (final_hidden_state, final_cell_state) = self.layer5(out)

        #out = out[:, -1, :].reshape(out.shape[0], 1, out.shape[2])
         

         mean = torch.mean(out, 1)
         std = torch.std(out, 1)
         stat = torch.cat((mean, std), 1)
         pred_emo = self.emotion_layer(stat)
         return pred_emo
</code></pre>
<p>The error is : number of dims don’t match in permute.<br>
Any suggestions</p>

Nikronic: <p>Hi,</p>
<p><code>out</code> from <code>self.layer4</code> is 4D tensor in shape <code>[batch, channel, h, w]</code>. But you are refering to 3D tensor using your <code>permute</code>.</p>
<aside class="quote no-group" data-username="krishna511" data-post="1" data-topic="107217">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/krishna511/48/30486_2.png" class="avatar"> krishna511:</div>
<blockquote>
<p><code> out = out.permute(0, 2, 1)</code></p>
</blockquote>
</aside>
<p>I am not sure about your idea, but something like <code>out.permute(0, 2, 3, 1)</code> will work.</p>
<p>PS. It would be much easier to debug if you print whole stack trace of error.<br>
Also, documentation is clear about majority of modules’ behavior. Might need to check it.</p>
<p>Bests</p>

krishna511: <p>Ok let me try that.<br>
Thank you</p>

krishna511: <p>Actually this dimesion error is due to size mismatch, lstm can take input in 3 dim , here this 2dcnn is giving (batch_size,no of channels, m,n), where mxn is the size of spectroogram, reducing at each conv layer. If it was 1dcnn then permute (0,2,1) is okay, so I think i need to read this spectrogram row wise to input lstm layer, how to rehsape it like that?<br>
[batch_size,no of channels, mxn], am I doing it right?<br>
plz guide</p>

krishna511: <p>its done!<br>
just use<br>
<code>nn.Flatten(2,3)</code></p>