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

sumorday: <p>class Generator(nn.Module):<br>
def <strong>init</strong>(self, z_dim, M):<br>
super(Generator, self).<strong>init</strong>()<br>
self.z_dim = z_dim<br>
self.main = nn.Sequential(<br>
nn.ConvTranspose2d(self.z_dim, 256, M, 1, 0, bias=False),  # 4, 4<br>
nn.BatchNorm2d(256),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(256, 128, 4, stride=2, padding=1, bias=False),<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1, bias=False),<br>
nn.BatchNorm2d(64),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(64, 3, 4, stride=2, padding=1, bias=False),<br>
nn.Tanh()<br>
)</p>
<pre><code>def forward(self, z):
    return self.main(z.view(-1, self.z_dim, 1, 1))
</code></pre>
<p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self, M):<br>
super(Discriminator, self).<strong>init</strong>()</p>
<pre><code>    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
</code></pre>
<p>class Generator32(Generator):<br>
def <strong>init</strong>(self, z_dim):<br>
super().<strong>init</strong>(z_dim, M=2)</p>
<p>class Discriminator32(Discriminator):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>(M=32)</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/3/a3fb001a884f595b1e65e3a40a8c74cf7eaffa7c.png" alt="屏幕快照 2022-05-17 下午4.35.24" data-base62-sha1="noDANGsjQhFyIPYtHZfCxnYfjmQ" width="498" height="442"></p>

sumorday: <pre><code>self.linear = nn.Linear(16 // 16 * 16 //10 * 128, 40)
</code></pre>
<p>I guess the issue comes from here.</p>

Matias_Vasquez: <p>If you don’t want to figure out the size of <code>in_features</code>, then you can simply change this to <code>LazyLinear</code>.</p>
<pre><code class="lang-python">self.linear = nn.LazyLinear(out_features=40)
</code></pre>

Andrei_Cristea: <p>Have a look at <a href="https://gist.github.com/ancri/953aea7c6ea8f59723e9d7e9d745413a" rel="noopener nofollow ugc">this little trick</a> 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).</p>

sumorday: <aside class="quote no-group" data-username="Matias_Vasquez" data-post="3" data-topic="152067">
<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/matias_vasquez/48/47847_2.png" class="avatar"> Matias_Vasquez:</div>
<blockquote>
<p><code>self.linear = nn.LazyLinear(out_features=40)</code></p>
</blockquote>
</aside>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x10 and 40x40)</p>

sumorday: <p>so said. I just want to use the architecture from this <a href="https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&amp;arnumber=9381694&amp;tag=1" class="inline-onebox" rel="noopener nofollow ugc">Hausdorff GAN: Improving GAN Generation Quality With Hausdorff Metric | IEEE Journals &amp; Magazine | IEEE Xplore</a><br>
to instead <a href="https://github.com/w86763777/pytorch-gan-collections/blob/master/source/models/dcgan.py" class="inline-onebox" rel="noopener nofollow ugc">pytorch-gan-collections/dcgan.py at master · w86763777/pytorch-gan-collections · GitHub</a></p>

sumorday: <p>Hi, I tried to use a colab to show the problem.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1Jw-oUA3Qi-uPK2I17gloAdFVSH1-5wyI">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20220517-060051-RC00_449180643" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1Jw-oUA3Qi-uPK2I17gloAdFVSH1-5wyI" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1Jw-oUA3Qi-uPK2I17gloAdFVSH1-5wyI" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
If you can point out the issue. Thank you!</p>

sumorday: <p>class Generator(nn.Module):<br>
def <strong>init</strong>(self, z_dim, M):<br>
super(Generator, self).<strong>init</strong>()<br>
self.z_dim = z_dim<br>
self.main = nn.Sequential(<br>
nn.ConvTranspose2d(self.z_dim, 256, M, 1, 0, bias=False),  # 4, 4<br>
nn.BatchNorm2d(256),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(256, 128, 4, stride=2, padding=1, bias=False),<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1, bias=False),<br>
nn.BatchNorm2d(64),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(64, 3, 4, stride=2, padding=1, bias=False),<br>
nn.Tanh()<br>
)</p>
<pre><code>def forward(self, z):
    return self.main(z.view(-1, self.z_dim, 1, 1))
</code></pre>
<p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self, M):<br>
super(Discriminator, self).<strong>init</strong>()</p>
<pre><code>    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
</code></pre>
<p>class Generator32(Generator):<br>
def <strong>init</strong>(self, z_dim):<br>
super().<strong>init</strong>(z_dim, M=2)</p>
<p>class Discriminator32(Discriminator):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>(M=32)</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x10 and 40x10)</p>

Matias_Vasquez: <p>Hi, sorry but I have not looked at it into greater detail.</p>
<p>I would suggest to follow the <a href="https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html" rel="noopener nofollow ugc">tutorial on the pytorch page</a> 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).</p>
<hr>
<p>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.</p>
<aside class="quote no-group" data-username="sumorday" data-post="8" data-topic="152067">
<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/sumorday/48/49768_2.png" class="avatar"> sumorday:</div>
<blockquote>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x10 and 40x10)</p>
</blockquote>
</aside>
<hr>
<p>However, it is always good to explain in your own words what it is that you want to make step by step.</p>
<p>This way it will be easier to determine which size things are supposed to be.</p>
<hr>
<p>Also a few tips:</p>
<ul>
<li>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.</li>
<li>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.</li>
</ul>

sumorday: <p>import torch<br>
import torch.nn as nn<br>
import torch.nn.init as init</p>
<p>class Generator(nn.Module):<br>
def <strong>init</strong>(self, z_dim, M=8):<br>
super().<strong>init</strong>()<br>
self.M = M<br>
self.linear = nn.Linear(z_dim, M * M * 256)<br>
self.main = nn.Sequential(<br>
nn.BatchNorm2d(256),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1),<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1),<br>
nn.BatchNorm2d(64),<br>
nn.ReLU(True),<br>
nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1),<br>
nn.Tanh())<br>
self.initialize()</p>
<pre><code>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
</code></pre>
<p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self, M=32):<br>
super(Discriminator, self).<strong>init</strong>()</p>
<pre><code>    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
</code></pre>
<p>class Generator32(Generator):<br>
def <strong>init</strong>(self, z_dim):<br>
super().<strong>init</strong>(z_dim, M=8)</p>
<p>class Discriminator32(Discriminator):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>(M=32)</p>
<p>this works. anyway thank you</p>