RuntimeError: size mismatch, m1: [512 x 4], m2: [2048 x 512]

ky_Pa: <p>Where should I locate this error? And how will I correct it?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/8/08e35ec820811b066e0b208ae8ea5b112074bbc4.png" data-download-href="https://discuss.pytorch.org/uploads/default/08e35ec820811b066e0b208ae8ea5b112074bbc4" title="微信图片_20200627142244"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/8/08e35ec820811b066e0b208ae8ea5b112074bbc4.png" alt="微信图片_20200627142244" data-base62-sha1="1gCXpxSrD81eX9BF3CPoZT2qdww" width="690" height="222" data-dominant-color="0D0D0D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20200627142244</span><span class="informations">1213×392 25.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F


class ResidualBlock(nn.Module):
    def __init__(self, in_features):
        super(ResidualBlock, self).__init__()

        conv_block = [  nn.ReflectionPad2d(1),
                        nn.Conv2d(in_features, in_features, 3),
                        nn.InstanceNorm2d(in_features),
                        nn.ReLU(inplace=True),
                        nn.ReflectionPad2d(1),
                        nn.Conv2d(in_features, in_features, 3),
                        nn.InstanceNorm2d(in_features)  ]

        self.conv_block = nn.Sequential(*conv_block)

    def forward(self, x):
        return x + self.conv_block(x)


class Generator(nn.Module):
    def __init__(self, input_nc, output_nc, n_residual_blocks=9):
        super(Generator, self).__init__()

        # Initial convolution block       
        model = [   nn.ReflectionPad2d(3),
                    nn.Conv2d(input_nc, 64, 7),
                    nn.InstanceNorm2d(64),
                    nn.ReLU(inplace=True) ]

        # Downsampling
        in_features = 64
        out_features = in_features*2
        for _ in range(2):
            model += [  nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
                        nn.InstanceNorm2d(out_features),
                        nn.ReLU(inplace=True) ]
            in_features = out_features
            out_features = in_features*2

        # Residual blocks
        for _ in range(n_residual_blocks):
            model += [ResidualBlock(in_features)]
        
        # Upsampling
        out_features = in_features//2
        for _ in range(2):
            model += [  nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
                        nn.InstanceNorm2d(out_features),
                        nn.ReLU(inplace=True) ]
            in_features = out_features
            out_features = in_features//2

        # Output layer
        model += [  nn.ReflectionPad2d(3),
                    nn.Conv2d(64, output_nc, 7),
                    nn.Tanh() ]

        self.model = nn.Sequential(*model)

    def forward(self, x):
        return self.model(x)


class Classifier(nn.Module):
    def __init__(self, input_nc):
        super(Classifier, self).__init__()
       
        self.classifier = nn.Sequential(
            # batch_size x input_nc x 256 x 256
            nn.Conv2d(input_nc, 32, kernel_size=5, stride=2, padding=2),
            nn.ReLU(True),
            # batch_size x 32 x 128 x 128
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
            # batch_size x 32 x 64 x 64
            nn.Conv2d(32, 64, kernel_size=5, stride=2, padding=2),
            nn.ReLU(True),
            # batch_size x 64 x 32 x 32
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
            # batch_size x 64 x 16 x 16
            nn.Conv2d(64, 128, kernel_size=5, stride=2, padding=2),
            nn.ReLU(True),
            # batch_size x 128 x 8 x 8
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
            # batch_size x 128 x 4 x 4
            nn.Linear(2048, 512),
            nn.ReLU(True),
            # batch_size x 512
            nn.Linear(512, 128),
            nn.ReLU(True),
            # batch_size x 128
            nn.Linear(128, 3)
        )

    def forward(self, inputs):
        out = self.classifier(inputs)
        return out


class Discriminator(nn.Module):
    def __init__(self, input_nc):
        super(Discriminator, self).__init__()

        # A bunch of convolutions one after another
        model = [   nn.Conv2d(input_nc, 64, 4, stride=2, padding=1),
                    nn.LeakyReLU(0.2, inplace=True) ]

        model += [  nn.Conv2d(64, 128, 4, stride=2, padding=1),
                    nn.InstanceNorm2d(128), 
                    nn.LeakyReLU(0.2, inplace=True) ]

        model += [  nn.Conv2d(128, 256, 4, stride=2, padding=1),
                    nn.InstanceNorm2d(256), 
                    nn.LeakyReLU(0.2, inplace=True) ]

        model += [  nn.Conv2d(256, 512, 4, padding=1),
                    nn.InstanceNorm2d(512), 
                    nn.LeakyReLU(0.2, inplace=True) ]

        # FCN classification layer
        model += [nn.Conv2d(512, 1, 4, padding=1)]

        self.model = nn.Sequential(*model)

    def forward(self, x):
        x =  self.model(x)
        # Average pooling and flatten
        return F.avg_pool2d(x, x.size()[2:]).view(x.size()[0], -1)</code></pre>

ky_Pa: <p>Hi, <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Can you help me see how this problem should be solved?</p>

harsha_g: <aside class="quote no-group" data-username="ky_Pa" data-post="1" data-topic="87085">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/eada6e/48.png" class="avatar"> ky_Pa:</div>
<blockquote>
<pre><code class="lang-auto">            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
            # batch_size x 128 x 4 x 4
            nn.Linear(2048, 512),
</code></pre>
</blockquote>
</aside>
<p>You might want to add <code>nn.Flatten()</code> between the <code>nn.MaxPool2d</code> and <code>nn.Linear()</code> calls in the <code>Classifier</code>.</p>

ky_Pa: <p>Thank you very much for your answers. After adding nn.Flatten(), I encountered this problem. Is my format wrong or Pytorch version?</p>
<pre><code class="lang-auto">            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
            # 128 x 4 x 4
            nn.Flatten().F,
            nn.Linear(2048, 512),
            nn.ReLU(True),</code></pre>

ky_Pa: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/6/16fddccabce914fdf3e7e98edff92a726d0f5117.png" alt="微信图片_20200628141514" data-base62-sha1="3hooVYP2BBwMIgM3Tjcd9nIGL4j" width="635" height="39"></p>

harsha_g: <p><code>nn.Flatten()</code> is part of PyTorch 1.2.0 and later. Either update your pytorch version or you may have to find alternate ways of doing that. More on that here <a href="https://discuss.pytorch.org/t/flatten-layer-of-pytorch-build-by-sequential-container/5983" class="inline-onebox">Flatten layer of PyTorch build by sequential container</a></p>