How is the weight matrix set and its dimension in CNN 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 .
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])