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

username: K163897_Nafix
cooked: <p>my dataset have 32 x 32 images, what is the error in it?</p>
<pre><code class="lang-auto">
class Genv2(nn.Module):
    def __init__(self, *args, **kwargs):
        super(Genv2, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)

        self.fc1 = nn.Linear(in_features=16*32*32, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=84)
        self.out = nn.Linear(in_features=84, out_features=2)

    def forward(self, t):

        print("in forward 1")
        print(t.shape)    # [1,3,224,224]

        # hidden layer
        t = f.relu(self.conv1(t))
        t = f.max_pool2d(t, kernel_size=2, stride=2)
        print("in forward 2")
        print(t.shape)    # [1,9,224,224]
        # hidden layer
        t = f.relu(self.conv2(t))
        t = f.max_pool2d(t, kernel_size=2, stride=2)

        # hidden layer
        t = f.relu(self.fc1(t.view(-1, 16*32*32)))
        t = f.relu(self.fc2(t))

        # output layer
        t = self.out(t)

        return t


if __name__ == "__main__":
    plt.ion()   # interactive mode
    # Data augmentation and normalization for training
    # Just normalization for validation
    data_transforms = {
        'train': transforms.Compose([
            # transforms.Resize((32, 32)),
            # transforms.RandomResizedCrop(224),
            # transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val': transforms.Compose([
            # transforms.Resize((32, 32)),
            # transforms.Resize(256),
            # transforms.CenterCrop(224),
            transforms.ToTensor(),
            # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    data_dir = 'data/generator'
    image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
                                              data_transforms[x])
                      for x in ['train', 'val']}

    dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=1,
                                                  shuffle=True, num_workers=4)
                   for x in ['train', 'val']}

    dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}

    network = Genv2()

    class_names = image_datasets['train'].classes
    print(class_names)

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    batch = next(iter(dataloaders['train']))
    image, label = batch

    preds = network(image)
</code></pre>

username: ptrblck
cooked: <p>The output of  the second max pooling layer will have a shape of <code>[batch_size, 16 ,5 ,5]</code>, so change your input features in <code>self.fc1</code> to <code>16*5*5=400</code> and adapt the <code>view</code> call:</p>
<pre><code class="lang-python">class Genv2(nn.Module):
    def __init__(self, *args, **kwargs):
        super(Genv2, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)

        self.fc1 = nn.Linear(in_features=16*5*5, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=84)
        self.out = nn.Linear(in_features=84, out_features=2)

    def forward(self, t):

        print("in forward 1")
        print(t.shape)    # [1, 3, 32, 32]

        # hidden layer
        t = F.relu(self.conv1(t))
        t = F.max_pool2d(t, kernel_size=2, stride=2)
        print("in forward 2")
        print(t.shape)    # [1, 3, 14, 14]
        # hidden layer
        t = F.relu(self.conv2(t))
        t = F.max_pool2d(t, kernel_size=2, stride=2)

        # hidden layer
        t = t.view(t.size(0), -1)
        print(t.shape)
        t = F.relu(self.fc1(t))
        t = F.relu(self.fc2(t))

        # output layer
        t = self.out(t)

        return t
</code></pre>

username: K163897_Nafix
cooked: <p>Thanks problem solved!! kindly explain it also? why you write  16<em>5</em>5</p>

username: ptrblck
cooked: <p>The conv and pooling layers in your model will change the shape of your activations depending on the kernel size, stride etc.<br>
Have a look at <a href="http://cs231n.github.io/convolutional-networks/" rel="nofollow noopener">CS231n</a> for a detailed description.</p>
<p>The short version:</p>
<ul>
<li>
<code>nn.Conv2d</code> with <code>kernel_size=5</code> and no padding will subtract 2 pixels on each border (4 pixels in width and height)</li>
<li>
<code>F.max_pool2d</code> with <code>kernel_size</code> and <code>stride=2</code> will divide the spatial size by 2 using the <code>floor</code> operation</li>
</ul>
<p>Here is a breakdown of your <code>forward</code> method (output shapes as comments):</p>
<pre><code class="lang-python">def forward(self, t):
        t = F.relu(self.conv1(t))  # [N, 6, 28, 28]
        t = F.max_pool2d(t, kernel_size=2, stride=2)  # [N, 6, 14, 14]
        t = F.relu(self.conv2(t))  # [N, 16, 10, 10]
        t = F.max_pool2d(t, kernel_size=2, stride=2)  # [N, 16, 5, 5]
        t = t.view(t.size(0), -1)  # [N, 400]
        t = F.relu(self.fc1(t))
        t = F.relu(self.fc2(t))
        t = self.out(t)
        return t
</code></pre>

username: K163897_Nafix
cooked: <p>I think there is issue in this code, when i run code again and again it give me this error</p>
<p>RuntimeError: size mismatch, m1: [1 x 789504], m2: [400 x 120] at …\aten\src\TH/generic/THTensorMath.cpp:961</p>

username: learner12345
cooked: <p>import torch.nn as nn</p>
<p>import torch.nn.functional as F</p>
<p>class Net(nn.Module):</p>
<pre><code>def __init__(self):

    super().__init__()

    self.conv1 = nn.Conv2d(3, 202, 3)

    self.pool = nn.MaxPool2d(2, 2)

    self.conv2 = nn.Conv2d(202, 22, 3)

    self.pool = nn.MaxPool2d(2, 2)

    self.conv3 = nn.Conv2d(22, 220, 3)

    self.pool = nn.MaxPool2d(2, 2)

    self.conv4 = nn.Conv2d(220, 200, 3)

    self.fc1 = nn.Linear(200*3*3, 202)

    self.fc2 = nn.Linear(202, 200)

    self.fc3 = nn.Linear(200, 120)

def forward(self, x):

    x = self.pool(F.relu(self.conv1(x)))

    x = self.pool(F.relu(self.conv2(x)))

    x = x.view(-1, 200 * 3 * 3)

    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>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; data is a list of [inputs, labels]

    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>
<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         loss.backward()</p>
<p>1 frames<br>
 in forward(self, x)<br>
20         x = self.pool(F.relu(self.conv1(x)))<br>
21         x = self.pool(F.relu(self.conv2(x)))<br>
—&gt; 22         x = x.view(-1, 200 * 3 * 3)<br>
23         x = F.relu(self.fc1(x))<br>
24         x = F.relu(self.fc2(x))</p>
<p>RuntimeError: shape ‘[-1, 1800]’ is invalid for input of size 43120</p>
<p>Please help me figure out this error</p>

username: ptrblck
cooked: <p>The <code>view</code> operation is failing, since the specified shape <code>[-1, 1800]</code> doesn’t fit the tensor shape.<br>
You could use <code>x = x.view(x.size(0), -1)</code> instead and check, if you would need to adapt the <code>in_features</code> of <code>self.fc1</code> in case it’s raising a shape mismatch error afterwards.</p>

username: learner12345
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="7" data-topic="55285">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>x = x.view(x.size(0), -1)</p>
</blockquote>
</aside>
<p>Thanks for reply<br>
I tried your suggestions but still I’m getting error<br>
mat1 and mat2 shapes cannot be multiplied (10x4312 and 1800x202)</p>

username: ptrblck
cooked: <p>The second part of my post was a guess that this error would come next:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="7" data-topic="55285">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>and check, if you would need to adapt the <code>in_features</code> of <code>self.fc1</code> in case it’s raising a shape mismatch error afterwards.</p>
</blockquote>
</aside>
<p>so check the <code>in_features</code> of <code>self.fc1</code> and set it to <code>4312</code>, which is the number of features of the input activation.</p>

username: learner12345
cooked: <p>Thanks dear. you are awesome, I was sitting on this error for last two days. Atleast no error in the training model now. Thanks alot</p>