RuntimeError: shape '[-1, 16384]' is invalid for input of size 789504

my dataset have 32 x 32 images, what is the error in it?


class Genv2(nn.Module):
    def __init__(self, *args, **kwargs):
        super(Genv2, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)

        self.fc1 = nn.Linear(in_features=16*32*32, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=84)
        self.out = nn.Linear(in_features=84, out_features=2)

    def forward(self, t):

        print("in forward 1")
        print(t.shape)    # [1,3,224,224]

        # hidden layer
        t = f.relu(self.conv1(t))
        t = f.max_pool2d(t, kernel_size=2, stride=2)
        print("in forward 2")
        print(t.shape)    # [1,9,224,224]
        # hidden layer
        t = f.relu(self.conv2(t))
        t = f.max_pool2d(t, kernel_size=2, stride=2)

        # hidden layer
        t = f.relu(self.fc1(t.view(-1, 16*32*32)))
        t = f.relu(self.fc2(t))

        # output layer
        t = self.out(t)

        return t


if __name__ == "__main__":
    plt.ion()   # interactive mode
    # Data augmentation and normalization for training
    # Just normalization for validation
    data_transforms = {
        'train': transforms.Compose([
            # transforms.Resize((32, 32)),
            # transforms.RandomResizedCrop(224),
            # transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val': transforms.Compose([
            # transforms.Resize((32, 32)),
            # transforms.Resize(256),
            # transforms.CenterCrop(224),
            transforms.ToTensor(),
            # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    data_dir = 'data/generator'
    image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
                                              data_transforms[x])
                      for x in ['train', 'val']}

    dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=1,
                                                  shuffle=True, num_workers=4)
                   for x in ['train', 'val']}

    dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}

    network = Genv2()

    class_names = image_datasets['train'].classes
    print(class_names)

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    batch = next(iter(dataloaders['train']))
    image, label = batch

    preds = network(image)

The output of the second max pooling layer will have a shape of [batch_size, 16 ,5 ,5], so change your input features in self.fc1 to 16*5*5=400 and adapt the view call:

class Genv2(nn.Module):
    def __init__(self, *args, **kwargs):
        super(Genv2, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)

        self.fc1 = nn.Linear(in_features=16*5*5, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=84)
        self.out = nn.Linear(in_features=84, out_features=2)

    def forward(self, t):

        print("in forward 1")
        print(t.shape)    # [1, 3, 32, 32]

        # hidden layer
        t = F.relu(self.conv1(t))
        t = F.max_pool2d(t, kernel_size=2, stride=2)
        print("in forward 2")
        print(t.shape)    # [1, 3, 14, 14]
        # hidden layer
        t = F.relu(self.conv2(t))
        t = F.max_pool2d(t, kernel_size=2, stride=2)

        # hidden layer
        t = t.view(t.size(0), -1)
        print(t.shape)
        t = F.relu(self.fc1(t))
        t = F.relu(self.fc2(t))

        # output layer
        t = self.out(t)

        return t
1 Like

Thanks problem solved!! kindly explain it also? why you write 1655

The conv and pooling layers in your model will change the shape of your activations depending on the kernel size, stride etc.
Have a look at CS231n for a detailed description.

The short version:

  • nn.Conv2d with kernel_size=5 and no padding will subtract 2 pixels on each border (4 pixels in width and height)
  • F.max_pool2d with kernel_size and stride=2 will divide the spatial size by 2 using the floor operation

Here is a breakdown of your forward method (output shapes as comments):

def forward(self, t):
        t = F.relu(self.conv1(t))  # [N, 6, 28, 28]
        t = F.max_pool2d(t, kernel_size=2, stride=2)  # [N, 6, 14, 14]
        t = F.relu(self.conv2(t))  # [N, 16, 10, 10]
        t = F.max_pool2d(t, kernel_size=2, stride=2)  # [N, 16, 5, 5]
        t = t.view(t.size(0), -1)  # [N, 400]
        t = F.relu(self.fc1(t))
        t = F.relu(self.fc2(t))
        t = self.out(t)
        return t
1 Like

I think there is issue in this code, when i run code again and again it give me this error

RuntimeError: size mismatch, m1: [1 x 789504], m2: [400 x 120] at …\aten\src\TH/generic/THTensorMath.cpp:961

import torch.nn as nn

import torch.nn.functional as F

class Net(nn.Module):

def __init__(self):

    super().__init__()

    self.conv1 = nn.Conv2d(3, 202, 3)

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

    self.conv2 = nn.Conv2d(202, 22, 3)

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

    self.conv3 = nn.Conv2d(22, 220, 3)

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

    self.conv4 = nn.Conv2d(220, 200, 3)

    self.fc1 = nn.Linear(200*3*3, 202)

    self.fc2 = nn.Linear(202, 200)

    self.fc3 = nn.Linear(200, 120)

def forward(self, x):

    x = self.pool(F.relu(self.conv1(x)))

    x = self.pool(F.relu(self.conv2(x)))

    x = x.view(-1, 200 * 3 * 3)

    x = F.relu(self.fc1(x))

    x = F.relu(self.fc2(x))

    x = self.fc3(x)

    return x

net = Net()

for epoch in range(2): # loop over the dataset multiple times

running_loss = 0.0

for i, data in enumerate(trainloader, 0):

    # get the inputs; data is a list of [inputs, labels]

    inputs, labels = data

    # zero the parameter gradients

    optimizer.zero_grad()

    # forward + backward + optimize

    outputs = net(inputs)

    loss = criterion(outputs, labels)

    loss.backward()

    optimizer.step()

    # print statistics

    running_loss += loss.item()

    if i % 2000 == 1999:    # print every 2000 mini-batches

        print('[%d, %5d] loss: %.3f' %

              (epoch + 1, i + 1, running_loss / 2000))

        running_loss = 0.0

print(‘Finished Training’)

RuntimeError Traceback (most recent call last)
in ()
11
12 # forward + backward + optimize
—> 13 outputs = net(inputs)
14 loss = criterion(outputs, labels)
15 loss.backward()

1 frames
in forward(self, x)
20 x = self.pool(F.relu(self.conv1(x)))
21 x = self.pool(F.relu(self.conv2(x)))
—> 22 x = x.view(-1, 200 * 3 * 3)
23 x = F.relu(self.fc1(x))
24 x = F.relu(self.fc2(x))

RuntimeError: shape ‘[-1, 1800]’ is invalid for input of size 43120

Please help me figure out this error

The view operation is failing, since the specified shape [-1, 1800] doesn’t fit the tensor shape.
You could use x = x.view(x.size(0), -1) instead and check, if you would need to adapt the in_features of self.fc1 in case it’s raising a shape mismatch error afterwards.

Thanks for reply
I tried your suggestions but still I’m getting error
mat1 and mat2 shapes cannot be multiplied (10x4312 and 1800x202)

The second part of my post was a guess that this error would come next:

so check the in_features of self.fc1 and set it to 4312, which is the number of features of the input activation.

Thanks dear. you are awesome, I was sitting on this error for last two days. Atleast no error in the training model now. Thanks alot