Beginner RuntimeError: shape '[-1, 784]' is invalid for input of size 12288

username: renxx7968
cooked: <p>Hello I am new to pytorch.<br>
I am trying to learn and build a fully connected neural network with Cifar data.<br>
However I am getting this error. I am kinda confused since so many people have complied successfully using this kind of codes.</p>
<p>import torch<br>
import torchvision<br>
from torch.autograd import Variable<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
from torchvision import datasets, transforms</p>
<p>batch_size=4<br>
learning_rate=0.01<br>
epochs=10<br>
log_interval=10</p>
<p>transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])</p>
<p>trainset = torchvision.datasets.CIFAR10(root=’./data’, train=True, download=True, transform=transform)</p>
<p>train_loader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)</p>
<p>testset = torchvision.datasets.CIFAR10(root=’./data’, train=False, download=True, transform=transform)</p>
<p>test_loader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2)</p>
<p>classes = (‘plane’, ‘car’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’)</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.fc1 = nn.Linear(784, 200)<br>
self.fc2 = nn.Linear(200, 200)<br>
self.fc3 = nn.Linear(200, 10)</p>
<pre><code>def forward(self, x):
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return F.log_softmax(x)
</code></pre>
<p>optimizer = optim.SGD(net.parameters(), lr=learning_rate)<br>
Loss = nn.CrossEntropyLoss()</p>
<p>for epoch in range(epochs):<br>
for batch_idx, (data, target) in enumerate(train_loader):<br>
data, target = Variable(data), Variable(target)<br>
# resize data from (batch_size, 1, 28, 28) to (batch_size, 28<em>28)<br>
data = data.view(-1, 28</em>28)<br>
optimizer.zero_grad()<br>
logits = net(data)<br>
loss = Loss(logits, target)<br>
loss.backward()<br>
optimizer.step()<br>
if batch_idx % log_interval == 0:<br>
print(‘Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}’.format(<br>
epoch, batch_idx * len(data), len(train_loader.dataset),<br>
100. * batch_idx / len(train_loader), loss.data[0]))</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
3         data, target = Variable(data), Variable(target)<br>
4         # resize data from (batch_size, 1, 28, 28) to (batch_size, 28<em>28)<br>
----&gt; 5         data = data.view(-1, 28</em>28)<br>
6         optimizer.zero_grad()<br>
7         logits = net(data)</p>
<p>RuntimeError: shape ‘[-1, 784]’ is invalid for input of size 12288</p>
<p>I have tried to change the Batch_Size, but it didn’t work.</p>

username: ptrblck
cooked: <p>CIFAR10 returns images of the shape <code>[3, 32, 32]</code>, which won’t work using your <code>.view(-1, 28*28)</code> call.<br>
Instead use <code>.view(-1, 3*32*32)</code> and set the same value as the number of input features to your first linear layer.</p>
<p>Also, <code>Variables</code> are deprecated, so you can use tensors directly.</p>