Invalid argument 2: size '[-1 x 400]' is invalid

Well, I’m trying to make a simple net but can’t deal this error. Any help will be appreciated

    for i, (x, l) in enumerate(zip(dataloader(X), labels)):
        # wrap inputs in Variable
        inputs, labels = Variable(torch.from_numpy(x)), Variable(torch.from_numpy(np.array(l)))
        inputs = inputs.float()
        labels = labels.float()
        # zero the parameter gradients
        optimizer.zero_grad()
        # forward + backward + optimize
        outputs = net(inputs)

The error happening at the outputs = net(inputs) line.

RuntimeError: invalid argument 2: dimension 0 out of range of 0D tensor at /Users/soumith/minicondabuild3/conda-bld/pytorch_1512381214802/work/torch/lib/TH/generic/THTensor.c:24

My dataloader returns numpy arrays with shape = (4, 1, 80, 80)

Model is declared as

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.pool(x)
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

Could you add a print after this line and see if the size is compatible with the view after it?

Actually, I forgot to mention that this error happens at the first convolution layer

x = self.conv1(x)

Could you print the shape of x anyway like @SimonW suggested.
Calculating the size manually, x should be [batch, 16, 17, 17].

A full error trace will be great :slight_smile:

Yea, the shape of x after second conv layer is [4, 16, 17, 17], 4 is batch size.
I guess it is a view issue about dimensions

Yeah you can’t view a tensor of that shape as [-1, 400]

1 Like

Hey Did you found the solution ?
I am getting the same error when i add data argumentation to reduce over fitting

Hey Did you found the solution ?
I am getting the same error when i add data argumentation to reduce over fitting