Beginner PyTorch - RuntimeError: shape '[16, 400]' is invalid for input of size 9600
11,374
This means that instead the product of the channel and spatial dimensions is not 5*5*16
. To flatten the tensor, replace x = x.view(x.size(0), 5 * 5 * 16)
with:
x = x.view(x.size(0), -1)
And self.fc1 = nn.Linear(600, 120)
with:
self.fc1 = nn.Linear(600, 120)
Author by
mokiliii Lo
Updated on June 04, 2022Comments
-
mokiliii Lo almost 2 years
I'm trying to build a CNN but I get this error:
---> 52 x = x.view(x.size(0), 5 * 5 * 16) RuntimeError: shape '[16, 400]' is invalid for input of size 9600
It's not clear for me what the inputs of the 'x.view' line should be. Also, I don't really understand how many times I should have this 'x.view' function in my code. Is it only once, after the 3 convolutional layers and 2 linear layers? Or is it 5 times, one after every layer?
Here's my code:
CNN
import torch.nn.functional as F # Convolutional neural network class ConvNet(nn.Module): def __init__(self, num_classes=10): super(ConvNet, self).__init__() self.conv1 = nn.Conv2d( in_channels=3, out_channels=16, kernel_size=3) self.conv2 = nn.Conv2d( in_channels=16, out_channels=24, kernel_size=4) self.conv3 = nn.Conv2d( in_channels=24, out_channels=32, kernel_size=4) self.dropout = nn.Dropout2d(p=0.3) self.pool = nn.MaxPool2d(2) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(512, 10) self.final = nn.Softmax(dim=1) def forward(self, x): print('shape 0 ' + str(x.shape)) x = F.max_pool2d(F.relu(self.conv1(x)), 2) x = self.dropout(x) print('shape 1 ' + str(x.shape)) x = F.max_pool2d(F.relu(self.conv2(x)), 2) x = self.dropout(x) print('shape 2 ' + str(x.shape)) # x = F.max_pool2d(F.relu(self.conv3(x)), 2) # x = self.dropout(x) x = F.interpolate(x, size=(5, 5)) x = x.view(x.size(0), 5 * 5 * 16) x = self.fc1(x) return x net = ConvNet()
Can someone help me understand the problem?
The output of 'x.shape' is:
shape 0 torch.Size([16, 3, 256, 256])
shape 1 torch.Size([16, 16, 127, 127])
shape 2 torch.Size([16, 24, 62, 62])
Thanks
-
mokiliii Lo about 4 yearsThanks for your answer. Should I flatten the tensor at each convolution, or just once after my 3 convolutions, or 5 times (bc I have 5 layers in total), or.. ?
-
ccl about 4 yearsTensor flattening is done right before your fully-connected (linear) layer(s), so you only have to do it once i.e. after
F.interpolate()
. -
ccl about 4 yearsDid my answer help? If so feel free to accept/vote on this answer.
-
mokiliii Lo about 4 yearsNot really, I'm getting another error now:
RuntimeError: size mismatch, m1: [16 x 800], m2: [600 x 120]
any idea? -
ccl about 4 yearsDid you modify your code/input size? Did you add an FC layer?
-
Sameen almost 2 yearsThe size mismatch issue is mainly caused by the input size, make sure your input is not smaller than what the net is expecting.