How to select parameters for nn.Linear layer while training a CNN?
I am trying to train a CNN to classify images from the Fashion-MNIST data using Conv2d, Maxpool and Linear layers. I came across a code as mentioned below with in_features = 12*4*4 in nn.Linear layer.

Can I please get help on how to select an in_features parameter for nn.Linear layer?

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5)

        self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=60)
        self.out = nn.Linear(in_features=60, out_features=10)


Each example in the Fashion-MNIST dataset is a 28 x 28 grayscale image.


Input is 28 x 28
We do a 5 x 5 convolution without padding(since default padding=0) and stride=1(by default), so we lose 2 pixels at each side, we drop down to 24 x 24, i.e.,(28-5)/1 + 1
We then perform maxpooling operation with receptive field of 2 x 2, we cut each dimension by half, down to 12 x 12
We again do another 5 x 5 convolution without padding and stride=1, we drop down to 8 x 8, i.e.,(12-5)/1 + 1
Then, we perform another maxpooling operation, we drop down to 4 x 4


That's why, self.fc1 = nn.Linear(in_features=12*4*4, out_features=120). It's basically, n_features_conv * height * width, where height and width are 4 respectively and n_features_conv  is same as out_channels of the conv2D layer lying just above it. 

Note if you change the size of input image, then you will have to perform the above calculations and adjust the first Linear layer accordingly.

Hope this helps you!
