When training a CNN using PyTorch in Python, I get the following error:
RuntimeError: invalid argument 2: size '[-3 x 3136]' is invalid for input with 160000 elements at /opt/conda/conda-bld/pytorch-cpu_1515613813020/work/torch/lib/TH/THStorage.c:41
This is related to the x.view line in the model below:
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(3,32,5,padding=2) # 1 input, 32 out, filter size = 5x5, 2 block outer padding
self.conv2 = nn.Conv2d(32,64,5,padding=2) # 32 input, 64 out, filter size = 5x5, 2 block padding
self.fc1 = nn.Linear(64*7*7,1024) # Fully connected layer
self.fc2 = nn.Linear(1024,2) #Fully connected layer 2 out.
def forward(self,x):
x = F.max_pool2d(F.relu(self.conv1(x)), 2) # Max pool over convolution with 2x2 pooling
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # Max pool over convolution with 2x2 pooling
x = x.view(-1,64*7*7) # tensor.view() reshapes the tensor
x = F.relu(self.fc1(x)) # Activation function after passing through fully connected layer
x = F.dropout(x, training=True) #Dropout regularisation
x = self.fc2(x) # Pass through final fully connected layer
return F.log_softmax(x) # Give results using softmax
model = Net()
print(model)
I'm not sure if this is a result of the images having 3 channels or something else entirely. I understand that this command should reshape the images into single dimensional arrays ready for the fully connected layer so I'm not sure how to fix this issue when the error is claiming an input of 160000 elements.