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

Error message:
File “train.py”, line 232, in
err_Dv_real, Dv_real_mean = bp_v(real_videos, 0.9)
File “train.py”, line 178, in bp_v
outputs = dis_v(inputs)
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\Harsh\Documents\finalproject\mocogan-master\models.py”, line 74, in forward
output = self.main(input)
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\container.py”, line 119, in forward
input = module(input)
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\linear.py”, line 94, in forward
return F.linear(input, self.weight, self.bias)
File “C:\Users\Harsh\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\functional.py”, line 1753, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (16x12288 and 18432x1)

Code:

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

`

Hi,

I don’t know the size of input images but int((ndf*8)*(T/16)*6*6) is 18432 and 12288 is the size of inputs for nn.Linear. For this specific usecase, the easiest fix would be nn.Linear(12288, 1).

As a more general workaround, I recommend LazyLinear — PyTorch 2.1 documentation if you can use v1.8

1 Like