How to build a CNN model with PyTorch?

CSstudent: <p>I am trying to build a CNN with the following depth and parameters:</p>
<ul>
<li>Convolution Layer 1: 3 input channels, 16 output channels, 3x3 kernel</li>
<li>Convolution Layer 2: 16 input channels, 24 output channels, 4x4 kernel</li>
<li>Convolution Layer 3: 24 input channels, 32 output channels, 4x4 kernel</li>
<li>Fully connected Layer 1: * input channels, 512 output</li>
<li>Fully connected Layer 2: 512 input channels, 10 output channels</li>
</ul>
<p>(<em>) - The input size of the first connected layer is calculated to be 29</em>29*32 (flattened previous layer).</p>
<p>Each convolution layer is followed by a ReLU and a max-pool operation with kernel size 2x2. After each sequence of convolution, ReLU and max-pool, a dropout operation with p=0.3 is added.</p>
<p>I have written the following code, however I am not entirely sure whether I have correctly implemented the design described above.</p>
<pre><code class="lang-auto">class ConvNet(nn.Module):

    def __init__(self, num_classes=10):
        super(ConvNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, 3)
        self.conv2 = nn.Conv2d(16, 24, 4)
        self.conv3 = nn.Conv2d(24, 32, 4)
        self.maxPool = nn.MaxPool2D(2)
        self.dropout = nn.Dropout2D(p=0.3)
        self.relu = nn.ReLU()
        self.fc1 = nn.Linear(29*29*32,512)
        self.fc2 = nn.Linear(512, num_classes)
        self.final = nn.Softmax(dim=1)

    def forward(self, x):
        x = self.conv1(x)
        x = self.relu(self.conv1(x))
        x = self.maxPool(x)
        x = self.dropout(x)
        x = self.conv2(x)
        x = self.relu(self.conv2(x))
        x = self.maxPool(x)
        x = self.dropout(x)
        x = self.conv3(x)
        x = self.relu(self.conv3(x))
        x = self.maxPool(x)
        x = self.dropout(x)
        x = x.reshape(x.size(0), -1)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.final(x)
        return x
</code></pre>
<p>Any advice would be greatly appreciated as although I have prior Python experience, I am very inexperienced in AI/ML methods.</p>

ptrblck: <p>The code looks generally great! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
Some minor issues:</p>
<ul>
<li>Based on the architecture it seems you are working on a multi-class classification use case. If that’s the case, you should remove the <code>nn.Softmax</code> as the last activation function and use <code>nn.CrossEntropyLoss</code>, which will internally apply <code>F.log_softmax</code> and <code>nn.NLLLoss</code>.</li>
</ul>
<aside class="quote no-group" data-username="CSstudent" data-post="1" data-topic="70201">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/d78d45/48.png" class="avatar"> CSstudent:</div>
<blockquote>
<p>Each convolution layer is followed by a ReLU and a max-pool operation with kernel size 2x2.</p>
</blockquote>
</aside>
<ul>
<li>This statement is currently not true, as you are using reusing your conv layers:</li>
</ul>
<pre data-code-wrap="python"><code class="lang-python">        x = self.conv1(x)
        x = self.relu(self.conv1(x))
</code></pre>
<p>This code will use <code>self.conv1</code> twice. I’m not sure, if that’s your use case, but it doesn’t match your explanation. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>