RunTime Error: Function AddmmBackward returned an invalid gradient at index 1 (Mismatching shapes)

username: akshaybharadwaj
cooked: <p>Hey All,</p>
<p>I am trying to implement a VAE, and i am having trouble calculating the gradient for the model. I believe this is happening in the decoder. The exact error message is <strong>Function AddmmBackward returned an invalid gradient at index 1 - got [10, 32] but expected shape compatible with [10, 1024]</strong>. Here is the decoder model.</p>
<pre><code class="lang-auto">class decoderNW(nn.Module):
    def __init__(self):
        super(decoderNW,self).__init__()

        channels = 32
        kernelSize = 4
        padding = (2,0)
        stride = (2,2)
        outputpadding = (1,0)

        self.FC1 = nn.Linear(channels, 1024)

        self.FC2 = nn.Linear(channels, 10656)

        self.deConv3x301 = nn.ConvTranspose2d(channels, 64, kernel_size=kernelSize, stride=stride, output_padding=outputpadding)
        nn.init.xavier_uniform_(self.deConv3x301.weight)

        self.deConv3x302 = nn.ConvTranspose2d(64, 128, kernel_size=kernelSize, stride=stride, output_padding=outputpadding)
        nn.init.xavier_uniform_(self.deConv3x302.weight)

        self.deConv3x303 = nn.ConvTranspose2d(128, 64, kernel_size=kernelSize, stride=stride, output_padding=outputpadding)
        nn.init.xavier_uniform_(self.deConv3x303.weight)

        self.deConv3x304 = nn.ConvTranspose2d(64, 3, kernel_size=kernelSize, stride=stride)
        nn.init.xavier_uniform_(self.deConv3x304.weight)

        self.bn1 = nn.BatchNorm1d(1024)
        self.bn2 = nn.BatchNorm2d(64)
        self.bn3 = nn.BatchNorm2d(128)
        self.bn4 = nn.BatchNorm2d(64)
 


        self.ReLU = nn.ReLU(inplace=True)

        self.sigmoid = nn.Sigmoid()

    def forward(self,x):

        x = self.FC1(x)
        x = self.bn1(x)
        x = self.ReLU(x)

        x = self.FC2(x)

        # Reshape x as 8x42x75
        x = x.view(x.size(0),32,9,37)

        x = self.deConv3x301(x)
        x = self.bn2(x)
        x = self.ReLU(x)

        x = self.deConv3x302(x)
        x = self.bn3(x)
        x = self.ReLU(x)

        x = self.deConv3x303(x)
        x = self.bn4(x)
        x = self.ReLU(x)

        x = self.deConv3x304(x)
        x = self.sigmoid(x)

        return(x)
</code></pre>
<p>I believe its happening when I am trying to reshape the tensor into a 2D tensor (like image) from FC to deconv layer.</p>
<p>I have tried using reshape function, but the same problem persists. Im not sure where I am going wrong. Any help is greatly appreciated.</p>
<p>Thanks.</p>
<p>PS: I get this error when I run backward(). Here is the code snippet for that!</p>
<pre><code class="lang-auto">            optimizerVAE.zero_grad()
            variationalAE.train()

            vaeT = vaeT.to('cuda')

            mu, sigma, xHat, z = variationalAE(srcClrT)

            loss = vaeLoss(srcClrT, mu, sigma, xHat, z)

            loss.backward()
</code></pre>

username: ptrblck
cooked: <p>I cannot reproduce the issue using:</p>
<pre><code class="lang-python">model = decoderNW()
x = torch.randn(2, 32)
out = model(x)
out.mean().backward()
</code></pre>
<p>but also needed to modify your model in:</p>
<pre><code class="lang-python">        self.FC1 = nn.Linear(channels, 1024)

        self.FC2 = nn.Linear(1024, 10656)
</code></pre>
<p>as I didn’t know the input shapes for the desired reshaping.<br>
Could you update the code with random inputs so that we could execute it and reproduce the issue?</p>

username: whoab
cooked: <p>I got this error because the noise vector I was passing into my encoder had the wrong latent dimension size. Check that.</p>