RuntimeError: shape '[1, 1024]' is invalid for input of size 50176

I am trying to use Alexnet over the CIFAR-10 dataset. I get the following error:

<ipython-input-11-34884668038d> in forward(self, x)
     37     def forward(self, x):
     38         x = self.features(x)
---> 39         x = x.view(x.size(0), 256 * 2 * 2)
     40         x = self.classifier(x)
     41         return x

RuntimeError: shape '[1, 1024]' is invalid for input of size 50176

My Alexnet model code is as follows:

NUM_CLASSES = 10


class AlexNet(nn.Module):
    def __init__(self, num_classes=NUM_CLASSES):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(64, 192, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 2 * 2, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), 256 * 2 * 2)
        x = self.classifier(x)
        return x

Any help would be appreciated :slight_smile:

Thank you. I’ve resolved my issue. It was because I was passing the size as 224 when CIFAR images work with size as 32.