PyTorch self.module?

WeiQin_Chuah: <p>Hi, I have came across something like this, can someone explain what is the purpose of this chunk of code?</p>
<p>Thank you very much <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.Conv3d):
                n = m.kernel_size[0] * m.kernel_size[1]*m.kernel_size[2] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm3d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                m.bias.data.zero_()
</code></pre>

shubhvachher: <p>I’m not sure what you mean by <code>self.module</code> in your title…</p>
<p>The <code>self.modules()</code> method returns an iterable to the many layers or “modules” defined in the model class. This particular piece of code is using that <code>self.modules()</code> iterable to initialize the weights of the different layers present in the model. <code>isinstance()</code> checks if the particular layer “m” is an instance of a conv2d or linear or conv3d layer etc. and initializes the weights accordingly.</p>
<p>Why don’t you check the docs for all these functions to get a better understanding?</p>
<pre><code class="lang-auto">model.modules()
model.named_modules()
model.parameters()
model.named_parameters()
model.children()
model.named_children()
</code></pre>

WeiQin_Chuah: <p>Thank you very much for your answer.</p>
<p>P.S. I am new to PyTorch and not very aware of the available resources. <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>

WeiQin_Chuah: <p><a class="mention" href="/u/shubhvachher">@shubhvachher</a> I have another question regarding this. Why initialize the weights manually?<br>
Correct me if I am wrong, the weights are initialized when you create your layers when using the <code>torch.nn</code> modules and these weights are random. Why re-initializing them?</p>
<p>Thank you <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:"></p>

petrell9: <p>Weights are initialized to small random numbers on creation (it actually depends on the type of layer you’re using but for argument’s sake we’ll just say small random numbers), usually this isn’t a huge deal as your model will eventually learn the correct weights through gradient descent. However, depending on the model complexity this may take a long time or get stuck in a local minimum. You can set the weights manually to “guide” your model to faster convergence if you have an idea on what would work better.</p>

shubhvachher: <p>I’m guessing you don’t mean initializing weights with those of a pretrained model but the different types of initializations in literature.</p>
<p>I’m not an expert on this but I think for learning from scratch, the initializations done by pytorch internally are good enough for most tasks. You could do these initializations yourself if subject matter experts in your learning task or information you gathered from your paper review suggests some initialization techniques for that task fair better than others.</p>

psychodrum: <aside class="quote no-group quote-modified" data-username="WeiQin_Chuah" data-post="1" data-topic="49677">
<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/weiqin_chuah/48/13348_2.png" class="avatar"> WeiQin_Chuah:</div>
<blockquote>
<p>iterable</p>
</blockquote>
</aside>
<p>You are not alone, there are no stupid questions, learning requires asking questions that are known to others , but not yourself. There was a starting point for all when we asked what a PC was. Personal Caretaker was popular prior to 90’s  LOL</p>

psychodrum: <p>I also have to say the brilliance and help on here is incredible. I browse to see if people are being helped and treated fairly, and I must say, out of hundreds of sites this is in the top 5 for active help. Kudos people!  A+</p>

Sarah_Salt: <p>Gentle reminder: when someone says they are new to the PyTorch community or NN architectures in general - be constructive not condescending with answers. I am on my 3rd year of building probabilistic NN models and I too haven’t memorized the Pytorch + Pyro handbooks.  Remember, it takes courage to ask questions!</p>
<p>-Sarah Amiri</p>