Input size and target size is not matching

username: Aditya_Srivastava
cooked: <p>batch size = 4</p>
<p>image size = 128</p>
<p>loss = MSELoss</p>
<p>I am trying to generate 6 numeric values from an image of size 128x128 using the below CNN model.<br>
I am getting a warning</p>
<p>Using a target size (torch.Size([4, 6])) that is different to the input size (torch.Size([1, 6])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.</p>
<p>I read many discussions here but sorry I am not getting it. please help me.</p>
<pre><code class="lang-auto">class MultiLabelNN(nn.Module):

    def __init__(self):

        super(MultiLabelNN, self).__init__()

        self.conv1 = nn.Conv2d(3,64, 5)

        self.pool = nn.MaxPool2d(2,2)

        self.relu = nn.ReLU()

        self.sigmoid = nn.Sigmoid()

        self.conv2 = nn.Conv2d(64, 128, 5)        

        self.conv3 = nn.Conv2d(128, 256, 5)

        self.conv4 = nn.Conv2d(256,320,5)

        self.fc1 = nn.Linear(20480,2048)

        self.fc2 = nn.Linear(2048, 1024)

        self.fc3 = nn.Linear(1024, 512)

        self.fc4 = nn.Linear(512, 256)

        self.fc5 = nn.Linear(256, 6)

    def forward(self, x):

        x = self.conv1(x)

        x = self.relu(x)    

        x = self.pool(x)  

        x = self.conv2(x)

        x = self.relu(x)

        x = self.pool(x)

        x = self.conv3(x)

        x = self.relu(x)

        x = self.pool(x)

        x = self.conv4(x)

        x = self.relu(x)

        x = self.pool(x)        

        x = x.view(-1, 20480)

        x = self.fc1(x)

        x = self.relu(x)        

        x = self.fc2(x)

        x = self.relu(x)      

        x = self.fc3(x)

        x = self.relu(x)

        x = self.fc4(x)

        x = self.relu(x)

        x = self.fc5(x)

        x = self.sigmoid(x)

        return x
</code></pre>

username: ptrblck
cooked: <p>I guess you might be changing the batch size of the intermediate activations using:</p>
<pre><code class="lang-python">x = x.view(-1, 20480)
</code></pre>
<p>Replace this line of code with <code>x = x.view(x.size(0), -1)</code> to flatten in the feature dimension and check if you would be running into shape mismatches in the following linear layers.</p>

username: Aditya_Srivastava
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="144785">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>x = x.view(x.size(0), -1)</p>
</blockquote>
</aside>
<p>Thank <a class="mention" href="/u/ptrblck">@ptrblck</a> for your reponse.</p>
<p>Now I am getting<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x5120 and 20480x2048)</p>

username: ptrblck
cooked: <p>OK, that’s great as I have expected to see the shape mismatch in the next linear layer.<br>
Change:</p>
<pre><code class="lang-python">self.fc1 = nn.Linear(20480,2048)
</code></pre>
<p>to:</p>
<pre><code class="lang-python">self.fc1 = nn.Linear(5120, 2048)
</code></pre>
<p>and it should work.</p>

username: Eta_C
cooked: <p>As PyTorch based on dynamic graph, how to explicitly determine the input dims of <code>Linear</code> layer is the common problem for each novice. <img src="https://discuss.pytorch.org/images/emoji/apple/rofl.png?v=12" title=":rofl:" class="emoji" alt=":rofl:" loading="lazy" width="20" height="20"></p>

username: ptrblck
cooked: <p>If you don’t like to calculate the shape (often not trivial) or don’t want to use the reported shape in the error message, the <code>nn.Lazy*</code> layers are here to help which will calculate the shapes for you <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

username: Aditya_Srivastava
cooked: <p>it is working now. Thanks a lot <a class="mention" href="/u/ptrblck">@ptrblck</a> you are a life savior <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=12" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20"></p>

username: Aditya_Srivastava
cooked: <p>You are absolutely right buddy <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=12" title=":grin:" class="emoji" alt=":grin:" loading="lazy" width="20" height="20"><br>
I am from that group <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=12" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>