How do you calculate the channel or input to the first linear layer?

xsquared: <p>Here is a network and if you could please explain to me how the <code>128 * 1 * 1</code> shape is calculated I will appreciate it very much.</p>
<p>I am aware of this formula <code>(W + F + 2P / S) + 1</code> but I am having trouble calculating<code>128 * 1 * 1</code>.</p>
<p>In this formula:<br>
W = Input Width<br>
F = Kernel size<br>
P = Padding<br>
S = Stride</p>
<p>The size of the input is <code>(1,28,28)</code> ie the MNIST dataset from torchvision.</p>
<p>So as you can see I have looked into this problem but I cannot calculate the <code>128 * 1 * 1</code> input to<br>
<code>self.f1 = nn.Linear(128 * 1 * 1, 1000)</code> in the network below So, if  you could answer this question using some formula I will appreciate it very much.</p>
<pre><code class="lang-auto">class Net(nn.Module):
    """A representation of a convolutional neural network comprised of VGG blocks."""
    def __init__(self, n_channels):
        super(Net, self).__init__()
        # VGG block 1
        self.conv1 = nn.Conv2d(n_channels, 64, (3,3))
        self.act1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d((2,2), stride=(2,2))
        # VGG block 2
        self.conv2 = nn.Conv2d(64, 64, (3,3))
        self.act2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d((2,2), stride=(2,2))
        # VGG block 3
        self.conv3 = nn.Conv2d(64, 128, (3,3))
        self.act3 = nn.ReLU()
        self.pool3 = nn.MaxPool2d((2,2), stride=(2,2))
        # Fully connected layer
        self.f1 = nn.Linear(128 * 1 * 1, 1000)
        self.act4 = nn.ReLU()
        # Output layer
        self.f2 = nn.Linear(1000, 10)
        self.act5 = nn.Softmax(dim=1)

    def forward(self, X):
        """This function forward propagates the input."""
        # VGG block 1
        X = self.conv1(X)
        X = self.act1(X)
        X = self.pool1(X)
        # VGG block 2
        X = self.conv2(X)
        X = self.act2(X)
        X = self.pool2(X)
        # VGG block 3
        X = self.conv3(X)
        X = self.act3(X)
        X = self.pool3(X)
        # Flatten
        X = X.view(-1, 128)
        # Fully connected layer
        X = self.f1(X)
        X = self.act4(X)
        # Output layer
        X = self.f2(X)
        X = self.act5(X)

        return X
        
        
</code></pre>
<p>thanks!</p>

nitaifingerhut: <p>you need to provide the size of the input for that</p>

xsquared: <p>i edited my question. the size is <code>(1, 28, 28)</code>.</p>

nitaifingerhut: <p>so (excluding the batch dimension):<br>
input: 1X28X28<br>
after conv2d: 64X28X28<br>
after maxpool: 64X13X13<br>
after conv2d: 64X11X11<br>
after maxpool: 64X5X5<br>
after conv2d: 128X3X3<br>
after maxpool: 128X1X1</p>
<p>now you have tensor of size (N, 128X1X1) which you view as (N, 128) by flattening it.<br>
That’s why the number of input features to the first linear layer is 128</p>

xsquared: <p>thank you so much!</p>
<p>Just to make sure i would like to ask this. after the first pool you actually get 13.5, right? but you rounded off to 13? this is correct, right?</p>

nitaifingerhut: <p>yes<br>
look here:<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html</a><br>
you need to round down</p>

xsquared: <p>thank you so much!</p>
<p>I appreciate it!</p>

BRAHMA_REDDY_AKUMALL: <p>How it can be 64<em>28</em>28 when we have kernel size 3,3.It will be 64<em>26</em>26.Or I am wrong…</p>