# How to calculate the first parameter of nn.Linear()

I am new to PyTorch.

When I was reading some examples, I have some questions.

Latest github example of mnist, in line 15 there is a number 320 in nn.Linear(), but I don’t know how to get that number.

``````class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
``````

I only know that without conv2_drop, in fc1 it should be 2055, but I don’t get how 320 come.

Thank you so much if you can help me.

2 Likes

well I forgot it is markdown…so in fc1 it should be 20*5*5, but I don’t get how 320 come.

Finally I understand…4*4*16

Well it’s `4*4*20 = 320`.

You can calculate the shape for the forward pass for each operation.
While a convolution with `kernel_size=5` and no padding shrinks the activation by 4 pixels in height and width, a max pooling of `kernel_size=2` and `stride=2` pools the activation to half its size.
From the forward pass:

``````# x.shape [1, 28, 28]
x = self.conv1(x) # x.shape: [10, 24, 24]
x = F.max_pool2d(x, 2) # x.shape: [10, 12, 12]
x = F.relu(x)
x = self.conv2(x) # x.shape: [20, 8, 8]
x = self.conv2_drop(x)
x = F.max_pool2d(x, 2) # x.shape: [20, 4, 4]
x = F.relu(x)
x = x.view(-1, 320)
``````
3 Likes