Decoder size mismatch error

username: chaslie
cooked: <p>Afternoon,</p>
<p>I am hoping someone can help me i am getting the following error message:</p>
<pre><code>output = input.matmul(weight.t())
</code></pre>
<p>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</p>
<pre><code class="lang-auto">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

</code></pre>
<p>Can anyone help me please?</p>
<p>Cheers</p>
<p>Chaslie</p>

username: chaslie
cooked: <p>Some additional information - it may help, or it may mean nothing.<br>
changing</p>
<pre><code class="lang-auto">summary(model.Decoder,(1,10))
</code></pre>
<p>to</p>
<pre><code class="lang-auto">summary(model.Decoder,(1,2))

</code></pre>
<p>changed the error to</p>
<p>RuntimeError: shape ‘[-1, 32, 21]’ is invalid for input of size 60</p>
<p>so i changed  z = z.view(-1, 32, 21) to z = z.view(-1, 5, 6)</p>
<p>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</p>
<p>Which leads to believe the problem is somewhere within the decoder input tensor size, but i don’t know how to fix it…</p>
<p>help???</p>
<p>Chaslie</p>

username: ptrblck
cooked: <p>I guess the error is thrown while passing the output of <code>fc2</code> to <code>co1</code>?<br>
<code>nn.ConvTranspose1d</code> 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).</p>

username: chaslie
cooked: <p>ptrblck,</p>
<p>would that be best done by moving the z.view term from def forward, or by adding a z.view term between fc2 &amp; co1?</p>

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

username: chaslie
cooked: <p>Hi ptrblck,</p>
<p>brilliant you are a genius (I have been pulling my hair out all day over this one <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:">)…</p>
<p>Now I have:</p>
<p>AttributeError: ‘list’ object has no attribute ‘cuda’</p>
<pre><code class="lang-auto">    for epoch in range(num_epochs):
        for data in train_dataset:
            data = data.cuda()
</code></pre>
<p>This is really frustrating because this worked as a normal autoencoder, but the moment i converted it to a variational autoencoder it stopped working…</p>

username: ptrblck
cooked: <p>Could you post the code for your <code>train_dataset</code>?<br>
It seems you are returning a <code>list</code> instead of a <code>tensor</code> or <code>tuple</code> of tensors.</p>

username: chaslie
cooked: <p>hi Ptrblck,</p>
<p>I am loading the data from a local npy file:<br>
hi P</p>
<p>train_array=np.load(‘load_directory’)</p>
<p>x_train=train_array.transpose([2,0,1]).reshape(42000,2048)<br>
X_train=torch.Tensor(X_train)<br>
y_train=np.arange(1,42001,1)<br>
y_train=y_train.reshape(42000,1)<br>
y_train=torch.from_numpy(y_train)</p>
<p>train_dataset = TensorDataset(X_train, y_train)<br>
train_dataset2 = DataLoader(train_dataset, batch_size=BATCHSIZE, shuffle=True)</p>
<p>I was having trouble with this and I don’t understand why (though when i look at the data it says its a tensor)</p>

username: ptrblck
cooked: <p>Thanks for the code.<br>
Your <code>DataLoader</code> returns a data and target batch, so either unwrap it in the loop:</p>
<pre><code class="lang-python">for batch in train_dataset2:
    data = batch[0]
    target = batch[1]
</code></pre>
<p>or assign the two variables in the loop statement:</p>
<pre><code class="lang-python">for data, target in train_dataset2:
    ...
</code></pre>
<p>Also, I would recommend to name the <code>DataLoader</code> something like <code>train_loader</code> so avoid confusion. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: chaslie
cooked: <p>Thanks for all your help,</p>
<p>final silly question,</p>
<p>I now get:</p>
<p>RuntimeError: Expected 3-dimensional input for 3-dimensional weight 1 1, but got 2-dimensional input of size [16, 2048] instead</p>
<p>I believe that this is showing the 16 (batch size) and 2048 (length of the vector), but do i need to resize this?</p>

username: ptrblck
cooked: <p>I’m not familiar with your use case, but you could reshape the output of your linear layer before feeding it to the <code>nn.ConvTranpose1d</code> layer or just add a dummy channel dimension using:</p>
<pre><code class="lang-python">output = output.unsqueeze(1)
</code></pre>
<p>Based on the number of input channels in <code>co1</code>, it seems the dummy channel dimension is probably what you want.</p>

username: chaslie
cooked: <p>Hi Ptrblck,</p>
<p>thanks for your help. I have got it working (to a fashion).</p>
<p>chaslie</p>