Hi Bruno!



 CasellaJr:

        self.conv1 = nn.Conv2d(in_channels=in_channels,out_channels=out_channels , **conv2_params)

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



You are passing a tensor of the wrong shape into the first Conv2d
layer in one of your VGGBlocks, most likely because you are passing
an input batch of 1-d vectors, rather than “3-d” images to your model.
Conv2d requires a shape of [nBatch, in_channels, height, width]
for its input (where nBatch can be arbitrary, in_channels matches
the in_channels of the Conv2d, and height and width are at least
as large kernel_size).  You need the in_channels dimension, even
if in_channels = 1.
I’m guessing that you have a batch size of 128, and that your input
images have been flattened into 1-d vectors of length 2048.
The following example illustrates a valid input and then reproduces
your error with invalid input:
>>> import torch
>>> torch.__version__
'1.7.1'
>>> conv = torch.nn.Conv2d (1, 64, (3, 3))
>>> x_good = torch.randn (128, 1, 32, 64)
>>> x_bad = torch.randn (128, 32 * 64)
>>> conv (x_good).shape
torch.Size([128, 64, 30, 62])
>>> conv (x_bad).shape
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 423, in forward
    return self._conv_forward(input, self.weight)
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 419, in _conv_forward
    return F.conv2d(input, weight, self.bias, self.stride,
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 1, 3, 3], but got 2-dimensional input of size [128, 2048] instead

Best.
K. Frank