nn.Sequential return less parameter compared to layerwise implementation

username: rkd
cooked: <p>I am new to pytorch. I am constructing simple pytorch model using with nn.sequential and using layers. I am getting different number of parameters. Can someone guide or explain the reason for different number of parameters in model. My implementation is as</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
from pytorch_model_summary import summary

class Net1(nn.Module):
    def __init__(self):
        super(Net1, self).__init__()
        self.Seqconv= nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=2, padding= 1, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True)
            )
    def  forward(self,x):       
        x= self.Seqconv(x)
        return x

class Net2(nn.Module):
    def __init__(self):
        super(Net2, self).__init__()
        self.conv=nn.Conv2d(in_channels=3, out_channels=32, kernel_size=(3,3), 
                            stride=2, padding=1)
        self.bn  = nn.BatchNorm2d(32)
        self.relu = nn.ReLU()
        
        
    def  forward(self,x):       
        x= self.relu(self.bn(self.conv(x)))        
        return x


x= torch.randn(1,3,224,224)
print(summary(Net1(), x, show_input=True))
# Yield Total params: 928

print(summary(Net2(), x, show_input=True))
# Yield Total params: 960

</code></pre>

username: BramVanroy
cooked: <p>It’s because you do not use bias in conv2d in <code>Net1</code>.</p>

username: ptrblck
cooked: <aside class="quote no-group quote-modified" data-username="BramVanroy" data-post="2" data-topic="97511">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bramvanroy/48/28628_2.png" class="avatar"> BramVanroy:</div>
<blockquote>
<p>PS use <code>from torch import nn</code> instead of <code>import torch.nn as nn</code> .</p>
</blockquote>
</aside>
<p>Just out of curiosity: why should the <code>from</code> import be preferred?</p>

username: BramVanroy
cooked: <p>Perhaps I was a bit commanding in my way of formulating things. Note that my. Comment is about “as” not about “import” VS “from… import”. AFAIK there is no noticeable usage difference if you assign ‘as’ nn or just import the submodule. However, ‘as’ is intended to specify an identifier for the loaded module. Therefore it is quite silly to assign it to the same name it already has.</p>

username: ptrblck
cooked: <p>Yeah, I think you are right and I have to admit that I never questiond it as I automatically type it in the “as nn” way based on the first official tutorials (e.g. <a href="https://github.com/pytorch/examples/blob/a5de3ce4da81bacc21864ace40dbc8a2e810df5a/imagenet/main.py#L9">ImageNet example</a>, <a href="https://github.com/pytorch/examples/blob/a5de3ce4da81bacc21864ace40dbc8a2e810df5a/mnist/main.py#L4">MNIST example</a>).</p>

username: BramVanroy
cooked: <p>Well perhaps it comes down to preference anyway! I was too strong in my statement. I don’t think there is one right or wrong answer. To me, my way seems “cleaner” but that’s subjective.</p>
<p>I removed it from my comment.</p>

username: ptrblck
cooked: <p>Oh no, I really don’t want to say you are wrong and I think the “as nn” import is really not necessary.<br>
After you’ve mentioned it it was probably the first time I thought about it. <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=9" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:"></p>
<p>Sorry, don’t want to hijack this topic for this discussion.</p>