Expected 3D (unbatched) or 4D (batched) input to conv_transpose2d, but got input of size: [64, 128]

Dear all
I got an error message
‘Expected 3D (unbatched) or 4D (batched) input to conv_transpose2d, but got input of size: [64, 128]’
while I coded the following autoencoder model:
I know there are many similar question that had been answered, but I am sorry that I still could find out the error key point

def init(self, latent_dim_value=128):
super(Net, self).init()
self.latent_value = latent_value
self.img_size = 32

    self.encoder = nn.Sequential(
        nn.Conv2d(3 ,32,  3, stride=2, padding=1),
        nn.ReLU(True),
        nn.MaxPool2d(2),

        nn.Conv2d(32, 64, 3, stride=2, padding=1),
        nn.ReLU(True),
        nn.MaxPool2d(2),

        nn.Conv2d(64, 128, 3, stride=2, padding=1),
        nn.ReLU(True),
    )
    
    self.fc_layer1 = nn.Sequential(
        nn.Linear(128, self.latent_dim),
        nn.PReLU(),)
    self.fc_layer2 = nn.Sequential(
        nn.Linear(self.latent_dim,128),
        nn.PReLU(),)

    self.decoder = nn.Sequential(
        nn.ConvTranspose2d(128, 64, 5 , 2, 1),
        nn.ReLU(True),
        nn.ConvTranspose2d(64, 32, 9, 2 , 1),
        nn.ReLU(True),
        nn.ConvTranspose2d(32, 256 ,17 , 2, 1),
        nn.Tanh()
    )
            
def forward(self, x):
    feature_map = self.encoder(x)
    y = self.fc_layer1(feature_map.reshape(feature_map.shape[0], -1))
    z = self.fc_layer2(y)
   Residual_x= self.decoder(z)
    
    return y, Residual_x

The nn.ConvTranspose2d layer expects a 4-dimensional tensor by default in the shape [batch_size, channels, height, width] or a 3-dimensional tensor if you skip the batch dimension.
In your code it seems the input tensor z has 2 dimensions only, which is incompatible.

1 Like