Why am I receiving the following error from Conv2D in the pytorch library?

username: Balazs_Gonczy
cooked: <p>I have the following issue with Conv2D in pytorch, I am trying to feed in a tensor like this: “in_shape = (64,3, 200, 220)” and I am getting error telling me that it expects this shape: “[2, 64, 3, 200, 220]”. Why would it like to see an extra dimension with 2? Any ideas?</p>

username: JuanFMontesinos
cooked: <p>Well,<br>
I would say that pytorch is not saying that <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>Could you paste the exact error and the layer you think it comes from?<br>
Pytorch 2d convs work with the format you are describing (B,C,H,W)</p>

username: Balazs_Gonczy
cooked: <p>I have solved it, I was providing more dimensions that it was needed and for some reason conv2d placed 2 in front of the vector’s shape tuple.</p>
<p>But right now I am having an issue about connecting CNN and GRU together for speaker identification task. Any idea how to do that?</p>

username: JuanFMontesinos
cooked: <p>You usually use max pooling and average pooling to obtain a feature vector. Then (either the dimensions match) or you can use a linear layer to go from the N feats from the cnn to M feats of the gru.</p>

username: Balazs_Gonczy
cooked: <p>Yes I am trying to do that, but I have an error like RNN wants a 3d vector but the CNN gives a 4d one. <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>

username: JuanFMontesinos
cooked: <p>So you have something like<br>
B,C,T,H,W<br>
then you pull and get<br>
B,C,T,1<br>
squeeze<br>
B,C,T<br>
then you need to permute (depending on whether u use the batch first flag or not)<br>
T,B,C<br>
Linear layer if necessary<br>
T,B,C’</p>

username: Balazs_Gonczy
cooked: <p>I’m doing something like this:</p>
<p>from torch import nn, optim</p>
<h1>
<a name="create-a-sequential-model-1" class="anchor" href="#create-a-sequential-model-1"></a>Create a sequential model</h1>
<p>model = nn.Sequential()</p>
<h1>
<a name="add-convolutional-and-pooling-layers-2" class="anchor" href="#add-convolutional-and-pooling-layers-2"></a>Add convolutional and pooling layers</h1>
<p>model.add_module(‘Conv_1’, nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(4,4)))</p>
<p>model.add_module(‘LRelu_1’, nn.LeakyReLU())</p>
<p>model.add_module(‘BatchN_1’, nn.BatchNorm2d(64))</p>
<p>model.add_module(‘AvgPool_1’, nn.AvgPool2d(kernel_size=4))</p>
<p>model.add_module(‘BatchN_2’, nn.BatchNorm2d(64))</p>
<p>model.add_module(‘Conv_2’, nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(4,4)))</p>
<p>model.add_module(‘LRelu_2’, nn.LeakyReLU())</p>
<p>model.add_module(‘BatchN_3’, nn.BatchNorm2d(128))</p>
<p>model.add_module(‘AvgPool_2’, nn.AvgPool2d(kernel_size=4))</p>
<p>model.add_module(‘BatchN_4’, nn.BatchNorm2d(128))</p>
<p>model.add_module(‘Conv_3’, nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3,3)))</p>
<p>model.add_module(‘LRelu_3’, nn.LeakyReLU())</p>
<p>model.add_module(‘BatchN_3’, nn.BatchNorm2d(128))</p>
<p>model.add_module(‘AvgPool_3’, nn.AvgPool2d(kernel_size=2))</p>
<p>model.add_module(‘BatchN_5’, nn.BatchNorm2d(128))</p>
<p>model.add_module(‘Conv_4’, nn.Conv2d(in_channels=128, out_channels=64, kernel_size=(2,2)))</p>
<p>model.add_module(‘LRelu_4’, nn.LeakyReLU())</p>
<h1>
<a name="add-a-flatten-layer-to-the-model-3" class="anchor" href="#add-a-flatten-layer-to-the-model-3"></a>Add a Flatten layer to the model</h1>
<p><span class="hashtag">#model</span>.add_module(‘Flatten’, nn.Flatten())</p>
<h1>
<a name="add-a-linear-layer-with-64-units-and-relu-activation-4" class="anchor" href="#add-a-linear-layer-with-64-units-and-relu-activation-4"></a>Add a Linear layer with 64 units and relu activation</h1>
<p>model.add_module(‘Linear_1’, nn.Linear(in_features=4, out_features=256, bias=True))</p>
<p>model.add_module(‘LRelu_5’, nn.LeakyReLU())</p>
<p>model.add_module(‘GRU_1’,nn.GRU(256,64,32))</p>
<h1>
<a name="add-the-last-linear-layer-5" class="anchor" href="#add-the-last-linear-layer-5"></a>Add the last Linear layer.</h1>
<p>model.add_module(‘Linear_2’, nn.Linear(in_features=256, out_features=183, bias=True))</p>
<p>model.add_module(‘Out_activation’, nn.Softmax(-1)) #??? -1</p>
<p>model = model.to(device)</p>
<p>from torchsummary import summary</p>
<p>in_shape = (3, 200, 220)</p>
<p>summary(model, input_size=(in_shape))</p>

username: Balazs_Gonczy
cooked: <p>If you could help me that would be fantastic contribution to Pytorch forum, because there is nothing about CNN-GRU’s connection.</p>

username: Balazs_Gonczy
cooked: <p>Now with this code the error is: <strong>RuntimeError</strong> : input must have 3 dimensions, got 4</p>