RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x40 and 128x40)

class Generator(nn.Module):
def init(self, z_dim, M):
super(Generator, self).init()
self.z_dim = z_dim
self.main = nn.Sequential(
nn.ConvTranspose2d(self.z_dim, 256, M, 1, 0, bias=False), # 4, 4
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, 3, 4, stride=2, padding=1, bias=False),
nn.Tanh()
)

def forward(self, z):
    return self.main(z.view(-1, self.z_dim, 1, 1))

class Discriminator(nn.Module):
def init(self, M):
super(Discriminator, self).init()

    self.main = nn.Sequential(
        # 64
        nn.Conv2d(3, 32, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.02, inplace=True),
        # 32
        nn.Conv2d(32, 64, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.02, inplace=True),
        nn.BatchNorm2d(64),
        nn.Dropout(p=0.5),
        # 16
        nn.Conv2d(64, 128, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.02, inplace=True),
        nn.BatchNorm2d(128),
        nn.Dropout(p=0.5),
        # 8
        nn.Conv2d(128, 10, 5, 2, 2, bias=False),
        nn.ReLU(True)
        # 4
    )

    self.linear = nn.Linear(16 // 16 * 16 //10 * 128, 40)

def forward(self, x):
    x = self.main(x)
    x = torch.flatten(x, start_dim=1)
    x = self.linear(x)
    return x

class Generator32(Generator):
def init(self, z_dim):
super().init(z_dim, M=2)

class Discriminator32(Discriminator):
def init(self):
super().init(M=32)

屏幕快照 2022-05-17 下午4.35.24

self.linear = nn.Linear(16 // 16 * 16 //10 * 128, 40)

I guess the issue comes from here.

If you don’t want to figure out the size of in_features, then you can simply change this to LazyLinear.

self.linear = nn.LazyLinear(out_features=40)

Have a look at this little trick as well, which will piggy-back on the error message to tell you what size you need to make your Linear layer (though using LazyLinear, as Matias suggested, works perfectly fine as well).

1 Like

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x10 and 40x40)

so said. I just want to use the architecture from this Hausdorff GAN: Improving GAN Generation Quality With Hausdorff Metric | IEEE Journals & Magazine | IEEE Xplore
to instead pytorch-gan-collections/dcgan.py at master · w86763777/pytorch-gan-collections · GitHub

Hi, I tried to use a colab to show the problem.

If you can point out the issue. Thank you!

class Generator(nn.Module):
def init(self, z_dim, M):
super(Generator, self).init()
self.z_dim = z_dim
self.main = nn.Sequential(
nn.ConvTranspose2d(self.z_dim, 256, M, 1, 0, bias=False), # 4, 4
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, 3, 4, stride=2, padding=1, bias=False),
nn.Tanh()
)

def forward(self, z):
    return self.main(z.view(-1, self.z_dim, 1, 1))

class Discriminator(nn.Module):
def init(self, M):
super(Discriminator, self).init()

    self.main = nn.Sequential(
        # 32
        nn.Conv2d(3, 32, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.2, inplace=True),
        # 32
        nn.Conv2d(32, 64, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.2, inplace=True),
        nn.BatchNorm2d(64),
        # 16
        nn.Conv2d(64, 128, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.2, inplace=True),
        nn.BatchNorm2d(128),
        # 8
        nn.Conv2d(128, 10, 5, 2, 2, bias=False),
        nn.ReLU(True)
        # 4
    )

    self.linear = nn.LazyLinear(out_features=10)

def forward(self, x):
    x = self.main(x)
    x = torch.flatten(x, start_dim=1)
    x = self.linear(x)
    return x

class Generator32(Generator):
def init(self, z_dim):
super().init(z_dim, M=2)

class Discriminator32(Discriminator):
def init(self):
super().init(M=32)

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x10 and 40x10)

Hi, sorry but I have not looked at it into greater detail.

I would suggest to follow the tutorial on the pytorch page and make it work. Then when you have that you can start modifying to your needs (it is the same as in the github repository you posted).


When you get an error like this ↓ you should make sure that the inner dimensions match. For this particular case it seems that it would be solved by switching the dimensions of the second tensor.


However, it is always good to explain in your own words what it is that you want to make step by step.

This way it will be easier to determine which size things are supposed to be.


Also a few tips:

  • When writing code, you can/should put three of these ``` in the begining and three at the end of the code. This way it will be more readable.
  • It is more helpful to put the entire stacktrace and not just the error that you get. That way one can see where the error occurs. Right now with this “RuntimeError: mat1…” it is very difficult to determine where the error exactly occurs.

import torch
import torch.nn as nn
import torch.nn.init as init

class Generator(nn.Module):
def init(self, z_dim, M=8):
super().init()
self.M = M
self.linear = nn.Linear(z_dim, M * M * 256)
self.main = nn.Sequential(
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1),
nn.Tanh())
self.initialize()

def initialize(self):
    for m in self.modules():
        if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d, nn.Linear)):
            init.normal_(m.weight, std=0.02)
            init.zeros_(m.bias)

def forward(self, z, *args, **kwargs):
    x = self.linear(z)
    x = x.view(x.size(0), -1, self.M, self.M)
    x = self.main(x)
    return x

class Discriminator(nn.Module):
def init(self, M=32):
super(Discriminator, self).init()

    self.main = nn.Sequential(
        # 64
        nn.Conv2d(3, 32, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.02, inplace=True),
        # 32
        nn.Conv2d(32, 64, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.02, inplace=True),
        nn.BatchNorm2d(64),
        nn.Dropout(p=0.5),
        # 16
        nn.Conv2d(64, 128, 5, 2, 2, bias=False),
        nn.LeakyReLU(0.02, inplace=True),
        nn.BatchNorm2d(128),
        nn.Dropout(p=0.5),
        # 8
        nn.Conv2d(128, 10, 5, 2, 2, bias=False),
        nn.ReLU(True)
        # 4
    )

    self.linear = nn.Linear(M // 16 * M // 16 * 10, 1)

def forward(self, x):
    x = self.main(x)
    x = torch.flatten(x, start_dim=1)
    x = self.linear(x)
    return x

class Generator32(Generator):
def init(self, z_dim):
super().init(z_dim, M=8)

class Discriminator32(Discriminator):
def init(self):
super().init(M=32)

this works. anyway thank you