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

mrartemev: <p>Well, I’m trying to make a simple net but can’t deal this error. Any help will be appreciated</p>
<pre><code class="lang-auto">    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)
</code></pre>
<p>The error happening at the outputs = net(inputs) line.</p>
<pre><code class="lang-auto">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
</code></pre>
<p>My dataloader returns numpy arrays with shape = (4, 1, 80, 80)</p>
<p>Model is declared as</p>
<pre><code class="lang-auto">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
</code></pre>

SimonW: <p>Could you add a print after this line and see if the size is compatible with the view after it?</p>
<aside class="quote no-group" data-username="mrartemev" data-post="1" data-topic="13024">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mrartemev/48/3313_2.png" class="avatar"> mrartemev:</div>
<blockquote>
<p>x = self.pool(F.relu(self.conv2(x)))</p>
</blockquote>
</aside>

mrartemev: <p>Actually, I forgot to mention that this error happens at the first convolution layer</p>
<pre><code class="lang-auto">x = self.conv1(x)
</code></pre>

ptrblck: <p>Could you print the shape of <code>x</code> anyway like <a class="mention" href="/u/simonw">@SimonW</a> suggested.<br>
Calculating the size manually, <code>x</code> should be <code>[batch, 16, 17, 17]</code>.</p>

SimonW: <p>A full error trace will be great <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

mrartemev: <p>Yea, the shape of x after second conv layer is [4, 16, 17, 17], 4 is batch size.<br>
I guess it is a view issue about dimensions</p>

SimonW: <p>Yeah you can’t view a tensor of that shape as [-1, 400]</p>

Neha_Gaikwad: <p>Hey Did you found the solution ?<br>
I am getting the same error when i add data argumentation to reduce over fitting</p>

Cyrus: <p>Hey Did you found the solution ?<br>
I am getting the same error when i add data argumentation to reduce over fitting</p>