RuntimeError: shape '[-1, 400]' is invalid for input of size

902003b7648b9e4c4217: <p>In order to classify images with pytorch, I modified my local data while using ImageFolder based on the following URL<br>
<a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html</a><br>
However,”RuntimeError: shape ‘[-1, 400]’ is invalid for input of size” is displayed and I do not know the cause.</p>
<p>I asked you because I do not know how to fix it,please.</p>
<blockquote>
<p>import torch<br>
import torchvision<br>
import torchvision.transforms as transforms<br>
import matplotlib.pyplot as plt<br>
import numpy as np<br>
import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>import torch.optim as optim<br>
transform = transforms.Compose(<br>
[transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])</p>
<p>data_transform = transforms.Compose([<br>
transforms.RandomResizedCrop(224),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
<span class="hashtag-raw">#transforms</span>.Normalize(mean=[0.485, 0.456, 0.406],<br>
#                  std=[0.229, 0.224, 0.225])<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br>
] )<br>
<span class="hashtag-raw">#rootはローカルのアドレスがあると考えてください</span><br>
trainset = torchvision.datasets.ImageFolder(root=‘my local dataset’, transform=data_transform)<br>
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,<br>
shuffle=False, num_workers=2)<br>
testset = torchvision.datasets.ImageFolder(root=‘my local dataset’, transform=data_transform)<br>
testloader = torch.utils.data.DataLoader(testset, batch_size=4,<br>
shuffle=False, num_workers=2)<br>
classes = (‘１’, ‘２’, ‘３’, ‘４’,<br>
‘５’, ‘６’, ‘７’, ‘８’, ‘９’)<br>
class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 6, 5)<br>
self.pool = nn.MaxPool2d(2, 2)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 5 * 5, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 10)</p>
<pre><code>def forward(self, x):
    x = self.pool(F.relu(self.conv1(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>
<p>net = Net()</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)<br>
for epoch in range(2):  # loop over the dataset multiple times</p>
<pre><code>running_loss = 0.0
for i, data in enumerate(trainloader, 0):
    # get the inputs
    inputs, labels = data

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    outputs = net(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

    # print statistics
    running_loss += loss.item()
    if i % 2000 == 1999:    # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 2000))
        running_loss = 0.0
</code></pre>
<p>print(‘Finished Training’)</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
11<br>
12         # forward + backward + optimize<br>
—&gt; 13         outputs = net(inputs)<br>
14         loss = criterion(outputs, labels)<br>
15</p>
<p>~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
487             result = self._slow_forward(*input, **kwargs)<br>
488         else:<br>
 → 489             result = self.forward(*input, **kwargs)<br>
490         for hook in self._forward_hooks.values():<br>
491             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
14         x = self.pool(F.relu(self.conv2(x)))<br>
15         x = x.contiguous()<br>
—&gt; 16         x = x.view(-1, 16 * 5 * 5)<br>
17         x = F.relu(self.fc1(x))<br>
18         x = F.relu(self.fc2(x))</p>
<p>RuntimeError: shape ‘[-1, 400]’ is invalid for input of size 87616</p>
</blockquote>

ptrblck: <p>The <code>view</code> operation which should flatten <code>x</code> is throwing this error, since the size of <code>16*5*5</code> is not matching your activation size.<br>
In the tutorial the CIFAR10 dataset is used, which has a spatial resolution of <code>32x32</code>.<br>
Most likely your custom dataset has a different spatial size, such that the <code>view</code> is failing.<br>
Based on the shape given in the error message, it looks like your activation should have the shape <code>[batch_size=4, channels=16, height=37, width=37]</code>, which results in <code>87616</code> values.<br>
Try to change the input size if your linear layer to <code>16*37*37</code> and the flattening to:</p>
<pre><code class="lang-python">x = x.view(x.size(0), 16*37*37)
</code></pre>

902003b7648b9e4c4217: <p>Thank you for your kindness. I tried to change “x = x.view(x.size(0), 16<em>37</em>37)”.<br>
But I have a new error,“RuntimeError: size mismatch, m1: [4 x 21904], m2: [4000 x 120] at /Users/administrator/nightlies/pytorch-1.0.0/wheel_build_dirs/conda_3.6/conda/conda-bld/pytorch_1544137972173/work/aten/src/TH/generic/THTensorMath.cpp:940”<br>
I tried to search the answer.but I have no idea.<br>
I predict my black and white images to be the cause.</p>

902003b7648b9e4c4217: <p>When I changed self.fc1 = nn.Linear(16 * 37* 37, 120), Error is gone.<br>
Thank you,so much!!!</p>

Kaitav_Mehta: <p>Many Thanks for the help. I was working for grayscale image classifier for 64*64 images.</p>
<p>RuntimeError: shape ‘[-1, 400]’ is invalid for input of size 10816<br>
I had this error, I knew it was issue with my net flattening, but didn’t knew what to change. Read your post and solved the issue with 90% accuracy for my classifier.<br>
4- Batch size<br>
13 = squareRoot(10816/4/16)</p>
<p>I had to set x = x.view(4, 16 * 13 * 13).</p>

anthony_nwachukwu: <p>Having this error</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-27-8c2bbe9e4336&gt; in &lt;module&gt;
      1 #Fit the model
----&gt; 2 learn.fit_one_cycle(1,slice(lr))#5

/opt/conda/lib/python3.6/site-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
     20     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
     21                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
---&gt; 22     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
     23 
     24 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None):

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
    200         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
    201         self.cb_fns_registered = True
--&gt; 202         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
    203 
    204     def create_opt(self, lr:Floats, wd:Floats=0.)-&gt;None:

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
    104             if not cb_handler.skip_validate and not learn.data.empty_val:
    105                 val_loss = validate(learn.model, learn.data.valid_dl, loss_func=learn.loss_func,
--&gt; 106                                        cb_handler=cb_handler, pbar=pbar)
    107             else: val_loss=None
    108             if cb_handler.on_epoch_end(val_loss): break

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
     61             if not is_listy(yb): yb = [yb]
     62             nums.append(first_el(yb).shape[0])
---&gt; 63             if cb_handler and cb_handler.on_batch_end(val_losses[-1]): break
     64             if n_batch and (len(nums)&gt;=n_batch): break
     65         nums = np.array(nums, dtype=np.float32)

/opt/conda/lib/python3.6/site-packages/fastai/callback.py in on_batch_end(self, loss)
    306         "Handle end of processing one batch with `loss`."
    307         self.state_dict['last_loss'] = loss
--&gt; 308         self('batch_end', call_mets = not self.state_dict['train'])
    309         if self.state_dict['train']:
    310             self.state_dict['iteration'] += 1

/opt/conda/lib/python3.6/site-packages/fastai/callback.py in __call__(self, cb_name, call_mets, **kwargs)
    248         "Call through to all of the `CallbakHandler` functions."
    249         if call_mets:
--&gt; 250             for met in self.metrics: self._call_and_update(met, cb_name, **kwargs)
    251         for cb in self.callbacks: self._call_and_update(cb, cb_name, **kwargs)
    252 

/opt/conda/lib/python3.6/site-packages/fastai/callback.py in _call_and_update(self, cb, cb_name, **kwargs)
    239     def _call_and_update(self, cb, cb_name, **kwargs)-&gt;None:
    240         "Call `cb_name` on `cb` and update the inner state."
--&gt; 241         new = ifnone(getattr(cb, f'on_{cb_name}')(**self.state_dict, **kwargs), dict())
    242         for k,v in new.items():
    243             if k not in self.state_dict:

/opt/conda/lib/python3.6/site-packages/fastai/callback.py in on_batch_end(self, last_output, last_target, **kwargs)
    342         if not is_listy(last_target): last_target=[last_target]
    343         self.count += first_el(last_target).size(0)
--&gt; 344         val = self.func(last_output, *last_target)
    345         if self.world:
    346             val = val.clone()

&lt;ipython-input-25-f37731fdae06&gt; in accuracy_1(input, targs)
      5     targs = targs.view(-1).long()
      6     n = targs.shape[0]
----&gt; 7     input = input.argmax(dim=-1).view(n,-1)
      8     targs = targs.view(n,-1)
      9     return (input==targs).float().mean()

RuntimeError: shape '[70912, -1]' is invalid for input of size 64
</code></pre>

johnvalen1: <p>I just want to add on that I, too, had the same problem and this solved it.</p>
<p>My input images were of size 224*224 (1 channel, grayscale).<br>
My batch size was chosen to be 32.</p>
<p>So for <code>x = x.view(-1, channels*height*width)</code> (note -1 since I’m flattening), I had to do:</p>
<p>height = width = sqrt( E / 32 / 16 ) where E comes from:<br>
“RuntimeError: shape ‘[-1, 400]’ is invalid for input of size E”</p>
<p>and channels is the number of filter channels from the previous convolution layer.</p>
<p>I guess that the formula here is:</p>
<p>height = width = sqrt( E / batch_size / n_channels)</p>

Abstain: <p>Hi! When I first met this same problem,I fixed it accordding to the below reply. But When I run the program , after it finishe one epoch, the same error appear again and I don’t know the cause, and what I should to do. I askes you, please.the program is below:</p>
<p>from <strong>future</strong> import print_function<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
import torchvision<br>
from torchvision import datasets, transforms<br>
import matplotlib.pyplot as plt<br>
import numpy as np<br>
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)</p>
<p>def loadtraindata():<br>
“”“加载数据”""<br>
path = r"D:/MNIST_test/train"<br>
trainset = torchvision.datasets.ImageFolder(path,<br>
transform=transforms.Compose([transforms.Resize((32, 32)),  # 将图片缩放到指定大小<br>
transforms.CenterCrop(32),  # 随机裁剪<br>
transforms.ToTensor(),  # 数据类型的转换<br>
transforms.Normalize((0.1307,), (0.3081,))<br>
]))<br>
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,<br>
shuffle=True, num_workers=0)<br>
return trainloader</p>
<p>class Net(nn.Module):<br>
“”“搭建网络”""<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
# self.conv1 = nn.Conv2d(1, 10, kernel_size=5)<br>
self.conv1 = nn.Conv2d(3, 10, kernel_size=5)<br>
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)<br>
self.conv2_drop = nn.Dropout2d()<br>
# self.fc1 = nn.Linear(320, 50)<br>
self.fc1 = nn.Linear(500, 32)<br>
# self.fc2 = nn.Linear(50, 10)<br>
self.fc2 = nn.Linear(32, 10)</p>
<pre><code>    # Spatial transformer localization-network
    self.localization = nn.Sequential(
        # nn.Conv2d(1, 8, kernel_size=7),
        nn.Conv2d(3, 8, kernel_size=7),
        nn.MaxPool2d(2, stride=2),
        nn.ReLU(True),
        nn.Conv2d(8, 10, kernel_size=5),
        nn.MaxPool2d(2, stride=2),
        nn.ReLU(True)
    )

    # Regressor for the 3 * 2 affine matrix
    self.fc_loc = nn.Sequential(
        # nn.Linear(10 * 3 * 3, 32),
        nn.Linear(10 * 4 * 4, 32),
        nn.ReLU(True),
        nn.Linear(32, 3 * 2)
    )

    # Initialize the weights/bias with identity transformation
    self.fc_loc[2].weight.data.zero_()
    self.fc_loc[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))

# Spatial transformer network forward function
def stn(self, x):
    xs = self.localization(x)
    # print(xs.size(0))  # 4
    # xs = xs.view(-1, 10* 3* 3)
    xs = xs.view(xs.size(0), 10 * 4 * 4)  # 64*3*3*10-&gt; 64*90
    theta = self.fc_loc(xs)  # 64*6
    theta = theta.view(-1, 2, 3)  # reshape 64*2*3 Transform matrix

    grid = F.affine_grid(theta, x.size())
    x = F.grid_sample(x, grid)

    return x

def forward(self, x):
    # transform the input
    x = self.stn(x)

    # Perform the usual forward pass
    x = F.relu(F.max_pool2d(self.conv1(x), 2))
    x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
    # print(x.size(0))  # 4
    x = x.view(4, 500)
    print("OK")
    x = F.relu(self.fc1(x))
    print(1)
    x = F.dropout(x, training=self.training)
    print(2)
    x = self.fc2(x)
    print(3)
    return F.log_softmax(x, dim=1)
</code></pre>
<p>model = Net().to(device)<br>
print(4)</p>
<p>classes = (‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’)</p>
<p>def train(epoch):<br>
“”“训练模型”""<br>
optimizer = optim.SGD(model.parameters(), lr=0.01)<br>
train_loader = loadtraindata()<br>
model.train()<br>
for batch_idx, (data, target) in enumerate(train_loader):<br>
data, target = data.to(device), target.to(device)</p>
<pre><code>    optimizer.zero_grad()
    output = model(data)
    loss = F.nll_loss(output, target)
    loss.backward()
    optimizer.step()
    if batch_idx % 500 == 0:
        print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
            epoch, batch_idx * len(data), len(train_loader.dataset),
            100. * batch_idx / len(train_loader), loss.item()))

print('Finished Training')
torch.save(model, 'model.pkl')  # 保存整个神经网络的结构和模型参数
torch.save(model.state_dict(), 'net_params.pkl')  # 只保存神经网络的模型参数
</code></pre>
<p>for epoch in range(1, 14):<br>
train(epoch</p>

praggya: <p>def _compute_unrolled_model(self, input_source,input_target, domain_label_source,domain_label_target, eta, network_optimizer):</p>
<pre><code>loss_source = self.model._loss(input_source, domain_label_source)

loss_target = self.model._loss(input_target,domain_label_target)

loss=loss_source+loss_target

theta = _concat(self.model.parameters()).data

try:

  moment = _concat(network_optimizer.state[v]['momentum_buffer'] for v in self.model.parameters()).mul_(self.network_momentum)

except:

  moment = torch.zeros_like(theta)

dtheta = _concat(torch.autograd.grad(loss, self.model.parameters(),retain_graph=True)).data + self.network_weight_decay*theta

unrolled_model = self._construct_model_from_theta(theta.sub(eta, moment+dtheta))

return unrolled_model
</code></pre>
<p>def _construct_model_from_theta(self, theta):</p>
<pre><code>model_new = self.model.new()

model_dict = self.model.state_dict()

params, offset = {}, 0

for k, v in self.model.named_parameters():

  v_length = np.prod(v.size())

  params[k] = theta[offset: offset+v_length].view(v.size())

  offset += v_length

assert offset == len(theta)

model_dict.update(params)

model_new.load_state_dict(model_dict)

return model_new.cuda()</code></pre>

praggya: <p>File “/voyager-volume/code_1_test/code_1_test/code_1_test/code_1_test/code_1_test/architect.py”, line 158, in _construct_model_from_theta<br>
params[k] = theta[offset: offset+v_length].view(v.size())<br>
RuntimeError: shape ‘[48]’ is invalid for input of size 0<br>
<a class="mention" href="/u/ptrblck">@ptrblck</a> ,could you tell why I am facing this issue</p>

ptrblck: <p>It seems taht the <code>offset</code> and <code>v_length</code> calculation combined with the slicing of <code>theta</code> is wrong.<br>
As you can see here:</p>
<pre><code class="lang-python">torch.randn([10])[10:20].view(48)
&gt; RuntimeError: shape '[48]' is invalid for input of size 0
</code></pre>
<p>you are most likely creating an empty tensor in <code>theta[offset: offset+v_length]</code> while the <code>view</code> operation expects to be used on <code>48</code> values.</p>