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

Hello. I have a model with the following layers:

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))
)

I have a tensor of size [1, 256, 130, 130] that I give as input to this network. Please note that only the first layer, that is, Linear(in_features=256, out_features=256, bias=True) 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)”
I tried to flatten the tensor for the Linear layer using z_flattened = z.view(z.size(0), -1) 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”
Can someone please tell me what am I doing wrong here?

nn.Linear layers accept inputs as [batch_size, *, in_features] where * 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.
If you want to apply the linear layer in this “sequential way” you would need to permute the input and make sure the feature dimension is the last dim.
On the other hand, if you want to flatten the input for the linear layer you would need to unsqueeze the spatial dimensions afterwards before passing them to the nn.Conv2d layer.

I used the following:

z_permuted = z.permute(0, 2, 3, 1)

I passed z_permuted through the model.
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”

This error is again expected since the output of the linear layer will have the shape [batch_size, *, out_features] while the nn.Conv2d layer expects the channel dimension to be in dim1.
You would thus need to permute the output again.