RuntimeError: shape '[-1, 20480]' is invalid for input of size 10240

username: Daniele_Rea
cooked: <p>Hello everyone, I found many topics about this error but I couldn’t find a solution, I kindly ask for help me.</p>
<p>CODE:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision.transforms as transforms

# MNIST
train_data = torchvision.datasets.MNIST(
    root="./data", train=True, download=True, transform=transforms.ToTensor()
)

# NN
class MyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.pool1 = nn.MaxPool2d(2)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.pool2 = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(20 * 32 * 32, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.pool2(x)
        x = x.view(-1, 20 * 32 * 32)
        x = self.fc1(x)
        x = self.fc2(x)
        return x

net = MyNet()

# data train
train_loader = torch.utils.data.DataLoader(
    train_data, batch_size=32, shuffle=True
)

optimizer = torch.optim.SGD(net.parameters(), lr=0.001)

for epoch in range(10):
    for i, (images, labels) in enumerate(train_loader):
        outputs = net(images)

        loss = torch.nn.functional.cross_entropy(outputs, labels)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if i % 100 == 0:
            print(f"Loss: {loss.item()}")

</code></pre>
<h2><a name="error-1" class="anchor" href="#error-1"></a>ERROR:<br>
`</h2>
<p>RuntimeError                              Traceback (most recent call last)</p>
<p> in &lt;cell line: 40&gt;()<br>
40 for epoch in range(10):<br>
41     for i, (images, labels) in enumerate(train_loader):<br>
—&gt; 42         outputs = net(images)<br>
43<br>
44         loss = torch.nn.functional.cross_entropy(outputs, labels)</p>
<p>2 frames</p>
<p> in forward(self, x)<br>
24         x = self.conv2(x)<br>
25         x = self.pool2(x)<br>
—&gt; 26         x = x.view(-1, 20 * 32 * 32)<br>
27         x = self.fc1(x)<br>
28         x = self.fc2(x)</p>
<p>RuntimeError: shape ‘[-1, 20480]’ is invalid for input of size 10240<br>
`</p>
<p>I’m trying to resize the shapes but I can’t do it right because solving one brings up another.</p>
<p>I will appreciate an explanation of my error.</p>
<p>Thanks in advance.</p>

username: Daniele_Rea
cooked: <p>I found a solution simply adjusting the shapes ( in the code #&lt;&lt;==):</p>
<pre><code class="lang-auto">class MyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.pool1 = nn.MaxPool2d(2)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.pool2 = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(10 * 32, 500) #&lt;&lt;==
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.pool2(x)
        x = x.view(-1, 10 * 32) #&lt;&lt;==
        x = self.fc1(x)
        x = self.fc2(x)
        return x
</code></pre>
<p>Thanks for all the topics that made me understand the issue.<br>
obviously if there is something wrong in this code I will very happy to improve from your responses.</p>

username: ptrblck
cooked: <p>Generally, if you want to flatten an activation tensor you would keep the batch dimension and flatten the remaining dimensions into a single one.<br>
In your code: <code>x = x.view(-1, 10 * 32)</code> you are defining the feature dimension and are moving the remaining samples into the batch dimension, which would create size mismatches if the number of features changes while experimenting with this model.<br>
The recommended approach would be: <code>x = x.view(x.size(0), -1)</code> which keeps the same batch size and moves the rest to the feature dimension.</p>