Decoder size mismatch error

Afternoon,

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/THCTensorMathBlas.cu:268

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)),
            ('HT1',nn.Hardtanh()),
            ('fc2',nn.Linear(30,64)),
            ('co1',nn.ConvTranspose1d(1,1,2,stride=2)),#128
            ('co2',nn.ConvTranspose1d(1,1,2,stride=2)),#256
            ('up1',nn.Upsample(scale_factor=2)), #512
            ('co3',nn.Conv1d(1,1,8,stride=2,padding=4)),#256
            ('co4',nn.ConvTranspose1d(1,1,8,stride=4,padding=2)),#1024
            ('co5',nn.ConvTranspose1d(1,1,4,stride=2,padding=1)),#2048
            ('up2',nn.Upsample(scale_factor=2)),#4096
            ('co6',nn.Conv1d(1,1,8,stride=4,padding=4)),#1024
            ('co7',nn.ConvTranspose1d(1,1,2,stride=1)),#1025
            ('co8',nn.ConvTranspose1d(1,1,2,stride=2)),#2050
            ('up3',nn.Upsample(scale_factor=2)),#4100
            ('co9',nn.Conv1d(1,1,16,stride=8,padding=6)),#512
            ('co10',nn.ConvTranspose1d(1,1,8,stride=4,padding=8)),#2048
            ]))
    def forward(self, z):
        #x.view returns a new tensor with the same data as the self tensor but different shape
        print(z.shape)
        z = F.relu(self.fc1(z))
        print(z)
        print(z.shape)
        z = z.view(-1, 32, 21)
        z = self.decode(z)  
        out=torch.sigmod(z)
        return out

Can anyone help me please?

Cheers

Chaslie

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

summary(model.Decoder,(1,10))

to

summary(model.Decoder,(1,2))

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/THCTensorMathBlas.cu:268

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

help???

Chaslie

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

ptrblck,

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_array=np.load(‘load_directory’)

x_train=train_array.transpose([2,0,1]).reshape(42000,2048)
X_train=torch.Tensor(X_train)
y_train=np.arange(1,42001,1)
y_train=y_train.reshape(42000,1)
y_train=torch.from_numpy(y_train)

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

chaslie