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

Hello everyone, I found many topics about this error but I couldn’t find a solution, I kindly ask for help me.

CODE:

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()}")

ERROR:
`

RuntimeError Traceback (most recent call last)

in <cell line: 40>()
40 for epoch in range(10):
41 for i, (images, labels) in enumerate(train_loader):
—> 42 outputs = net(images)
43
44 loss = torch.nn.functional.cross_entropy(outputs, labels)

2 frames

in forward(self, x)
24 x = self.conv2(x)
25 x = self.pool2(x)
—> 26 x = x.view(-1, 20 * 32 * 32)
27 x = self.fc1(x)
28 x = self.fc2(x)

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

I’m trying to resize the shapes but I can’t do it right because solving one brings up another.

I will appreciate an explanation of my error.

Thanks in advance.

I found a solution simply adjusting the shapes ( in the code #<<==):

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) #<<==
        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) #<<==
        x = self.fc1(x)
        x = self.fc2(x)
        return x

Thanks for all the topics that made me understand the issue.
obviously if there is something wrong in this code I will very happy to improve from your responses.

Generally, if you want to flatten an activation tensor you would keep the batch dimension and flatten the remaining dimensions into a single one.
In your code: x = x.view(-1, 10 * 32) 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.
The recommended approach would be: x = x.view(x.size(0), -1) which keeps the same batch size and moves the rest to the feature dimension.