1
$\begingroup$

I am trying to calculate the output size of each layer and the number of parameters for 3 class classification using CNN. I have calculated till the final maxpooling layer and would really appreciate help in understanding how to reach the fully connected layer.

In Matlab I checked that the size for fully connected (FC) is 1176 and the weights are 3*1176 I am struggling to understand what these 2 numbers mean and how they have been calculated. I could guess that 3 comes from the number of classes but how did 1176 come? It does not match my calculation.

Question: How to determine the dimension of the last layer- FC? Since I have 3 classes, do I have 3 layers? Please correct me where wrong. Attached is the screenshot which shows the dimensions for the weight as 3*1176 .

img

$\endgroup$
2
  • $\begingroup$ The number of neurons in your final fully connected layer is dependent on the number of classes you have, so you should have 3 neurons in the last layer. The number of fully connected layers after the convolutional layers is something you can decide for yourself. $\endgroup$
    – Oxbowerce
    Mar 22, 2020 at 19:00
  • $\begingroup$ How can I decide? As seen from the screenshot, Matlab is showing 1176 a number which I have no clue how it has come. It is shown as weights. So how come I have 3*1176 weights? What happens after the 4th step's calculation. These things are not clear to me. $\endgroup$
    – Sm1
    Mar 22, 2020 at 19:36

1 Answer 1

2
$\begingroup$

The 1176 is the number of neurons in the layer before the last layer, which is the number of pixels in the convolutional layer but flattened (height*width*n_filters, i.e. 7*7*24=1176). The 3 comes from the number of neurons in the final layer, since you have 3 classes your final layer has 3 neurons, 1 for each class (which will output zero or one). This then gives a weight matrix of 3x1176 for the weights between the second to last layer and the last layer. Using torch we can show the dimensions of the data passed between the layers in the network. In the example below I have omitted the batch normalization and relu layers since they do not affect the dimensions. The dimensions are defined as follows: batch_size * n_channels * height * width.

import torch

x = torch.randn(1, 1, 28, 28)
x.shape
# torch.Size([1, 1, 28, 28])

conv1 = torch.nn.Conv2d(in_channels=1, out_channels=12, kernel_size=3, padding=1)(x)
conv1.shape
# torch.Size([1, 12, 28, 28])

pool1 = torch.nn.MaxPool2d(kernel_size=2, stride=2)(conv1)
pool1.shape
# torch.Size([1, 12, 14, 14])

conv2 = torch.nn.Conv2d(in_channels=12, out_channels=24, kernel_size=3, padding=1)(pool1)
conv2.shape
# torch.Size([1, 24, 14, 14])

pool2 = torch.nn.MaxPool2d(kernel_size=2, stride=2)(conv2)
pool2.shape
# torch.Size([1, 24, 7, 7])

pool2.view(-1).shape
# torch.Size([1176])
$\endgroup$
3
  • $\begingroup$ Thank you very much for answering the question. I compared the sizes mentioned in your program & calculated by myself. Starting from the values at step 3) conv2 I start getting different results. Your code mentions the size as 14 but I got 20.5 i.e., [20.5*20.5*24] as the output from convolutional layer 2. The input is 14. I am not getting 7 in the final step. If it is not too much for you, can you please let me know if I made mistake in the formula or something else?? $\endgroup$
    – Sm1
    Mar 22, 2020 at 23:37
  • $\begingroup$ I think you made a mistake in your calculation. Using the size of 14 from the first pooling layer and filling in the values in your formula gives the following calculation (14 - 3 + 2) / 1 + 1 = 13 / 1 + 1 = 14, which is equal to what is shown in my code. $\endgroup$
    – Oxbowerce
    Mar 23, 2020 at 17:50
  • $\begingroup$ Thank you very much! $\endgroup$
    – Sm1
    Mar 23, 2020 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.