Shape '[32, 150528]' is invalid for input of size 1492992

username: Danyllo_Carlos
cooked: <pre><code class="lang-auto">Using GPU: True

Epoch 1/10
----------
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-13-cd4c03781ec7&gt; in &lt;module&gt;()
      8 exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1) #decay
      9                                                             # 0.1 every 5 epochs
---&gt; 10 model_ft = train_model(net, criterion, optimizer, exp_lr_scheduler, num_epochs=10)

2 frames
&lt;ipython-input-12-e791953bf1c6&gt; in forward(self, x)
     17         # If the size is a square you can only specify a single number
     18         x = F.max_pool2d(F.relu(self.conv2(x)), 2)
---&gt; 19         x = x.view(x.size(0), 3 * 224 * 224)
     20         x = F.relu(self.fc1(x))
     21         x = F.relu(self.fc2(x))
</code></pre>
<p>I have this problem but I don’t know how to solve this. Could someone help me, please?</p>
<p>That’s my Net</p>
<pre><code class="lang-auto">class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(3, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(3 * 224 * 224, 144)  # 6*6 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(x.size(0), 3 * 224 * 224)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features
</code></pre>

username: mathematics
cooked: <p>I expect, your problem is when using view         <code>x = x.view(x.size(0), 3 * 224 * 224)</code> which is giving error,<br>
without  providing explicit shape it might be better to use<br>
<code>x.view(x.shape[0],-1)</code></p>
<p>Another problem might be in <code>self.fc1</code> and <code>self.fc2</code><br>
As we know,  <code>torch.nn.Linear(in_features, out_features)</code><br>
so you need to change,</p>
<p>fc1 : <code>in_features</code> to be shape of <code>self.conv2</code> returning shape, in my case if input is <code>224x224</code> then<br>
and also <code>out_features</code> should be input of <code>fc2</code> as in example,</p>
<pre><code class="lang-auto">self.fc1 = nn.Linear(46656, 144)  # 6*6 from image dimension
self.fc2 = nn.Linear(144, 84)
</code></pre>
<p>Hope it solves problem</p>

username: Danyllo_Carlos
cooked: <aside class="quote no-group" data-username="mathematics" data-post="2" data-topic="93641">
<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/mathematics/48/40082_2.png" class="avatar"> mathematics:</div>
<blockquote>
<p><code>46656</code></p>
</blockquote>
</aside>
<p>I follow your orientations and I had another problem:</p>
<pre><code class="lang-auto">Using GPU: True

Epoch 1/10
----------
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-102-cd4c03781ec7&gt; in &lt;module&gt;()
      8 exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1) #decay
      9                                                             # 0.1 every 5 epochs
---&gt; 10 model_ft = train_model(net, criterion, optimizer, exp_lr_scheduler, num_epochs=10)

5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1672     if input.dim() == 2 and bias is not None:
   1673         # fused op is marginally faster
-&gt; 1674         ret = torch.addmm(bias, input, weight.t())
   1675     else:
   1676         output = input.matmul(weight.t())

RuntimeError: mat1 dim 1 must match mat2 dim 0
</code></pre>

username: mathematics
cooked: <p>please, could you print me your input shape.<br>
I tried, Here is code</p>
<pre><code class="lang-auto">class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(3, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(46656, 144)  # 6*6 from image dimension
        self.fc2 = nn.Linear(144, 84)
        self.fc3 = nn.Linear(84, 10)
  def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(x.shape[0], -1)
        print(x.shape) # prints flatten shape
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features
</code></pre>
<p>It does not throws error when you have input <code>t = torch.randn([1, 3, 224,224])</code>. However if input size is different it might be problem, you should use <code>transforms</code> in Dataloader to reduce size to applicable for model.</p>

username: Danyllo_Carlos
cooked: <p>My input shape is torch.Size([128, 16, 54, 54]).</p>

username: mathematics
cooked: <p>I think, No , your input size,<br>
use <code>print(x.shape)</code> just below forward function</p>

username: Danyllo_Carlos
cooked: <p>Sorry, you’re right! I did it wrong before reshape.</p>
<pre><code class="lang-auto"> def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        #print(x.shape)
        x = x.view(x.shape[0], -1)
        print(x.shape)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
</code></pre>
<p>The print result is torch.Size([128, 46656]).</p>

username: Danyllo_Carlos
cooked: <p>How do you find the input_features =  46656?</p>
<pre><code class="lang-auto">self.fc1 = nn.Linear(46656, 144)  # 6*6 from image dimension
</code></pre>

username: mathematics
cooked: <p>You can get it from just printing below reshaped shape. As  <code>torch.Size([128, 46656])</code> is flattened shape where<br>
<code>128</code>: is no of batch size<br>
<code>46656</code> is tensor need to passed to the Linear <code>in_features</code>.</p>
<p>This only happens when you have input of shape<code> [batch, channel, h,w] i.e [128,3,224,224]</code> .  If you wanted to have different input shape , you can see reshape shape and use it accordingly.</p>

username: Danyllo_Carlos
cooked: <p>I got it. My problem is gone. Thank you so much for your help, <a class="mention" href="/u/mathematics">@mathematics</a>!</p>