Decoder size mismatch error


I am hoping someone can help me i am getting the following error message:

output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [2 x 10], m2: [2 x 10] at C:/w/1/s/tmp_conda_3.7_044431/conda/conda-bld/pytorch_1556686009173/work/aten/src\THC/generic/

class Decoder(nn.Module):
    def __init__(self,lat_dim):
        super(Decoder, self).__init__()
        self.fc1 = nn.Linear(lat_dim, 10)

        self.decode = nn.Sequential(OrderedDict([
#            ('fc1',nn.Linear(lat_dim,30)),
            ('up1',nn.Upsample(scale_factor=2)), #512
    def forward(self, z):
        #x.view returns a new tensor with the same data as the self tensor but different shape
        z = F.relu(self.fc1(z))
        z = z.view(-1, 32, 21)
        z = self.decode(z)  
        return out

Can anyone help me please?



Some additional information - it may help, or it may mean nothing.




changed the error to

RuntimeError: shape ‘[-1, 32, 21]’ is invalid for input of size 60

so i changed z = z.view(-1, 32, 21) to z = z.view(-1, 5, 6)

RuntimeError: size mismatch, m1: [10 x 6], m2: [30 x 64] at C:/w/1/s/tmp_conda_3.7_044431/conda/conda-bld/pytorch_1556686009173/work/aten/src\THC/generic/

Which leads to believe the problem is somewhere within the decoder input tensor size, but i don’t know how to fix it…



I guess the error is thrown while passing the output of fc2 to co1?
nn.ConvTranspose1d expects a 3-dimensional input, so you might need to reshape the output of your linear layer (if you are using a 2-dimensional input for it).


would that be best done by moving the z.view term from def forward, or by adding a z.view term between fc2 & co1?

Personally, I would apply the linear layer separately in forward and reshape the output using view after it.
Once you have the right shape, just pass it to the nn.Sequential block, which would then only contain the conv/transposed conv/upsampling layers (3-dim input).

Hi ptrblck,

brilliant you are a genius (I have been pulling my hair out all day over this one :slight_smile:)…

Now I have:

AttributeError: ‘list’ object has no attribute ‘cuda’

    for epoch in range(num_epochs):
        for data in train_dataset:
            data = data.cuda()

This is really frustrating because this worked as a normal autoencoder, but the moment i converted it to a variational autoencoder it stopped working…

Could you post the code for your train_dataset?
It seems you are returning a list instead of a tensor or tuple of tensors.

hi Ptrblck,

I am loading the data from a local npy file:
hi P



train_dataset = TensorDataset(X_train, y_train)
train_dataset2 = DataLoader(train_dataset, batch_size=BATCHSIZE, shuffle=True)

I was having trouble with this and I don’t understand why (though when i look at the data it says its a tensor)

Thanks for the code.
Your DataLoader returns a data and target batch, so either unwrap it in the loop:

for batch in train_dataset2:
    data = batch[0]
    target = batch[1]

or assign the two variables in the loop statement:

for data, target in train_dataset2:

Also, I would recommend to name the DataLoader something like train_loader so avoid confusion. :wink:

Thanks for all your help,

final silly question,

I now get:

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

I believe that this is showing the 16 (batch size) and 2048 (length of the vector), but do i need to resize this?

I’m not familiar with your use case, but you could reshape the output of your linear layer before feeding it to the nn.ConvTranpose1d layer or just add a dummy channel dimension using:

output = output.unsqueeze(1)

Based on the number of input channels in co1, it seems the dummy channel dimension is probably what you want.

Hi Ptrblck,

thanks for your help. I have got it working (to a fashion).