F.relu(self.fc1(x)) is causing RuntimeError problem
I have implemented the following CNN for my training and validation data sets
that contain 90 and 20 images respectively divided into 3 classes:

def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(3, 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, 50)
    self.fc3 = nn.Linear(50, len(classes))


def forward(self, x):
    print(x.shape)
    x = self.pool(F.relu(self.conv1(x)))
    print(x.shape)
    x = self.pool(F.relu(self.conv2(x)))
    print(x.shape)
    x = x.view(x.size(0),-1)
        #x = x.view(-1,x.size(1)*x.size(2)*x.size(3))
        #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


I am getting the following error while running:

RuntimeError: size mismatch, m1: [1 x 214720], m2: [400 x 120] at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/TH/generic/THTensorMath.c:2033 for x = F.relu(self.fc1(x))


Can someone please suggest what I should do to get rid of this problem? I changed the x.view(...) by following some threads. However, it did not help in this case.

The size of the in_channels to self.fc1 is dependent on the input image size and not on the kernel-size.

In your case, 
self.fc1 = nn.Linear(16 * 5 * 5, 120) should be nn.Linear(16 * image_size * image_size)
where, image_size: is the size of the image in the last convolution layer.

Sample Code:

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

class Net(nn.Module):
    def __init__(self, classes):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 6, 5, padding=2)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5, padding=2)
        self.fc1 = nn.Linear(16 * 25 * 25, 120)
        self.fc2 = nn.Linear(120, 50)
        self.fc3 = nn.Linear(50, classes)

    def forward(self, x):
        print('one', x.shape)
        x = self.pool(F.relu(self.conv1(x)))
        print('two', x.shape)
        x = self.pool(F.relu(self.conv2(x)))
        print('three', x.shape)
        x = x.view(-1, np.product(x.shape[1:]))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

x = torch.rand((32, 3, 100, 100))
net = Net(2)
out= net(x)
print('out', out.shape)




one torch.Size([32, 3, 100, 100])
two torch.Size([32, 6, 50, 50])
three torch.Size([32, 16, 25, 25])
out torch.Size([32, 2])

