RuntimeError: mat1 and mat2 shapes cannot be multiplied (33280x130 and 256x256), RuntimeError: Expected 4-dimensional input for 4-dimensional weight [256, 256, 3, 3], but got 2-dimensional input of size [1, 256] instead

username: shrbrh
cooked: <p>Hello.                                                                                                                                                                I have a model with the following layers:</p>
<pre><code class="lang-auto">Sequential(
  (0): Linear(in_features=256, out_features=256, bias=True)
  (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
  (2): ReLU()
  (3): ReflectionPad2d((1, 1, 1, 1))
  (4): Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1))
  (5): ReLU()
  (6): Upsample(scale_factor=2.0, mode=nearest)
  (7): ReflectionPad2d((1, 1, 1, 1))
  (8): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1))
  (9): ReLU()
  (10): ReflectionPad2d((1, 1, 1, 1))
  (11): Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1))
  (12): ReLU()
  (13): Upsample(scale_factor=2.0, mode=nearest)
  (14): ReflectionPad2d((1, 1, 1, 1))
  (15): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1))
  (16): ReLU()
  (17): ReflectionPad2d((1, 1, 1, 1))
  (18): Conv2d(64, 3, kernel_size=(3, 3), stride=(1, 1))
)
</code></pre>
<p>I have a tensor of size <code>[1, 256, 130, 130]</code> that I give as input to this network. Please note that only the first layer, that is, <code>Linear(in_features=256, out_features=256, bias=True)</code> is trainable and the rest have fixed weights. Now when I give the tensor as input I am getting the following error: “RuntimeError: mat1 and mat2 shapes cannot be multiplied (33280x130 and 256x256)”<br>
I tried to flatten the tensor for the Linear layer using  <code>z_flattened = z.view(z.size(0), -1) </code> which again gives the following error: “RuntimeError: Expected 4-dimensional input for 4-dimensional weight [256, 256, 3, 3], but got 2-dimensional input of size [1, 256] instead”<br>
Can someone please tell me what am I doing wrong here?</p>

username: ptrblck
cooked: <p><code>nn.Linear</code> layers accept inputs as <code>[batch_size, *, in_features]</code> where <code>*</code> denotes additional dimensions. If these dimensions are provided the linear layer will be applied on each of these dims as if you would iterate these.<br>
If you want to apply the linear layer in this “sequential way” you would need to <code>permute</code> the input and make sure the feature dimension is the last dim.<br>
On the other hand, if you want to flatten the input for the linear layer you would need to <code>unsqueeze</code> the spatial dimensions afterwards before passing them to the <code>nn.Conv2d</code> layer.</p>

username: shrbrh
cooked: <p>I used the following:</p>
<pre><code class="lang-auto">z_permuted = z.permute(0, 2, 3, 1)
</code></pre>
<p>I passed z_permuted through the model.<br>
But I am still getting the error: “RuntimeError: Given groups=1, weight of size [256, 256, 3, 3], expected input[1, 130, 130, 256] to have 256 channels, but got 130 channels instead”</p>

username: ptrblck
cooked: <p>This error is again expected since the output of the linear layer will have the shape <code>[batch_size, *, out_features]</code> while the <code>nn.Conv2d</code> layer expects the channel dimension to be in <code>dim1</code>.<br>
You would thus need to permute the output again.</p>