RuntimeError: mat1 and mat2 shapes cannot be multiplied (16x12288 and 18432x1)

jharshraj: <p>Error message:<br>
File “train.py”, line 232, in <br>
err_Dv_real, Dv_real_mean = bp_v(real_videos, 0.9)<br>
File “train.py”, line 178, in bp_v<br>
outputs = dis_v(inputs)<br>
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\Harsh\Documents\finalproject\mocogan-master\models.py”, line 74, in forward<br>
output = self.main(input)<br>
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\container.py”, line 119, in forward<br>
input = module(input)<br>
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\linear.py”, line 94, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\functional.py”, line 1753, in linear<br>
return torch._C._nn.linear(input, weight, bias)<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (16x12288 and 18432x1)</p>
<p>Code:</p>
<pre><code class="lang-auto">class Discriminator_V(nn.Module):
    def __init__(self, nc=3, ndf=64, T=16, ngpu=1):
        super(Discriminator_V, self).__init__()
        self.ngpu = ngpu
        self.main = nn.Sequential(
            # input is (nc) x T x 96 x 96
            nn.Conv3d(nc, ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x T/2 x 48 x 48
            nn.Conv3d(ndf, ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm3d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x T/4 x 24 x 24
            nn.Conv3d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm3d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x T/8 x 12 x 12
            nn.Conv3d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm3d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*8) x T/16  x 6 x 6
            Flatten(),
            nn.Linear(int((ndf*8)*(T/16)*6*6), 1),
            nn.Sigmoid()
        )

    def forward(self, input):
        if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu &gt; 1:
            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
        else:
            output = self.main(input)

        return output.view(-1, 1).squeeze(1)

class Flatten(nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)
</code></pre>
<p>`</p>

crcrpar: <p>Hi,</p>
<aside class="quote no-group" data-username="jharshraj" data-post="1" data-topic="115097">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jharshraj/48/35929_2.png" class="avatar"> jharshraj:</div>
<blockquote>
<p><code>nn.Linear(int((ndf*8)*(T/16)*6*6), 1),</code></p>
</blockquote>
</aside>
<p>I don’t know the size of input images but <code>int((ndf*8)*(T/16)*6*6)</code> is <code>18432</code> and <code>12288</code> is the size of inputs for <code>nn.Linear</code>. For this specific usecase, the easiest fix would be <code>nn.Linear(12288, 1)</code>.</p>
<p>As a more general workaround, I recommend <a href="https://pytorch.org/docs/stable/generated/torch.nn.LazyLinear.html" class="inline-onebox" rel="noopener nofollow ugc">LazyLinear — PyTorch 2.1 documentation</a> if you can use v1.8</p>