Difference between nn.Module and Module from nn.Modules.Module

mahsa: <p>I’m reading a code which has a class for Model and a class for Layer definition. The BaseClass of the Model is nn.Module and the BaseClass of Layer is Module from nn.modules.module. What is the difference?<br>
This is the <a href="https://github.com/tkipf/pygcn/blob/master/pygcn/layers.py" rel="nofollow noopener">layer code</a> and this is the <a href="https://github.com/tkipf/pygcn/blob/master/pygcn/models.py" rel="nofollow noopener">model code</a><br>
Thanks</p>

KFrank: <p>Hi Mahsa!</p>
<aside class="quote no-group quote-modified" data-username="mahsa" data-post="1" data-topic="46631" data-full="true">
<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/mahsa/48/11812_2.png" class="avatar"> mahsa:</div>
<blockquote>
<p>I’m reading a code which has a class for Model and a class for Layer definition. The BaseClass of the Model is nn.Module and the BaseClass of Layer is Module from nn.modules.module. What is the difference?<br>
Thanks</p>
</blockquote>
</aside>
<p>If I understand python correctly (which I don’t) <code>nn.Module</code> and<br>
<code>nn.Modules.Module</code> are the same class.</p>
<p>As I understand it the module (in the python sense) <code>nn.Module</code><br>
in effect does:</p>
<pre data-code-wrap="python"><code class="lang-python">from torch.nn.Modules import Module
</code></pre>
<p>This makes the name <code>Module</code> (from <code>nn.Modules</code>) visible in <code>nn</code>.<br>
So, if in your code you have <code>import torch.nn as nn</code>, you can<br>
in your code then refer to the class <code>torch.nn.Modules.Module</code><br>
as <code>nn.Module</code>.</p>
<p>Best regards.</p>
<p>K. Frank</p>

mahsa: <p>Thanks for your reply. I got a little bit confused. I though in the Model class, nn.Module is from nn package (actually torch.nn) an in the Layer class Module is from nn.Modules.Module. How are the same? Module and Modules aren’t different classes in nn?</p>

KFrank: <p>Hello Mahsa!</p>
<aside class="quote no-group" data-username="mahsa" data-post="3" data-topic="46631" data-full="true">
<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/mahsa/48/11812_2.png" class="avatar"> mahsa:</div>
<blockquote>
<p>Thanks for your reply. I got a little bit confused. I though in the Model class, nn.Module is from nn package (actually torch.nn) an in the Layer class Module is from nn.Modules.Module. How are the same? Module and Modules aren’t different classes in nn?</p>
</blockquote>
</aside>
<p>I’m a little bit confused, too.</p>
<p>Let me revise what I said.</p>
<p>I don’t think there is a <code>torch.nn.Modules.Module</code>.  Compare the<br>
documentation for <code>torch.nn.Module</code>:</p>
<p><a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/nn.html#torch.nn.Module</a></p>
<p>with this code:</p>
<p><a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html" rel="noopener nofollow ugc">Source code for torch.nn.modules.module</a></p>
<p>From this I conclude that the linked code is the code for the<br>
class <code>torch.nn.Module</code>.  I’m guessing that the url that refers<br>
to “_modules/torch/nn/modules/module.html” is somehow<br>
indicating how pytorch organizes its code, but does not mean<br>
that the class’s full name is “torch.nn.Modules.Module”<br>
(or “torch.nn.modules.Module”).</p>
<p>If this doesn’t make sense, could you link to the <code>Layer</code> code you<br>
mentioned in your original post so we can see how it refers to<br>
<code>torch.nn.Modules.Module</code> and try to figure out what is going on?</p>
<p>Best.</p>
<p>K. Frank</p>

mahsa: <p>Thanks K Frank. I added the link of the codes.</p>

KFrank: <p>Hi Mahsa!</p>
<aside class="quote no-group" data-username="mahsa" data-post="5" data-topic="46631" data-full="true">
<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/mahsa/48/11812_2.png" class="avatar"> mahsa:</div>
<blockquote>
<p>Thanks K Frank. I added the link of the codes.</p>
</blockquote>
</aside>
<p>Thanks for the link to the layer code.  That clears things up.<br>
Yes, the two <code>Module</code> classes are indeed one and the same.</p>
<p>Look specifically at this line in layers.py</p>
<pre data-code-wrap="python"><code class="lang-python">from torch.nn.modules.module import Module
</code></pre>
<p>So let me go back to a version of my first reply to you – there is<br>
a class <code>torch.nn.modules.module.Module</code> and the package<br>
(python “module”) <code>torch.nn</code> imports it, making it visible as<br>
<code>torch.nn.Module</code>.</p>
<p>To see what’s going on, run this script:</p>
<pre data-code-wrap="python"><code class="lang-python">from torch.nn import Module as NnModule
print ('NnModule =', NnModule)
from torch.nn.modules.module import Module as MModule
print ('MModule =', MModule)
</code></pre>
<p>When I run this I get:</p>
<pre><code class="lang-plaintext">NnModule = &lt;class 'torch.nn.modules.module.Module'&gt;
MModule = &lt;class 'torch.nn.modules.module.Module'&gt;
</code></pre>
<p>So, the package <code>torch.nn</code> is, in effect, doing something like</p>
<pre data-code-wrap="python"><code class="lang-python">from torch.nn.modules.module import Module
</code></pre>
<p>and this makes <code>torch.nn.modules.module.Module</code> visible as<br>
<code>torch.nn.Module</code>.</p>
<p>(I was getting tangled up a little bit before with the<br>
<code>modules.module.Module</code> and the capitalization.)</p>
<p>Best regards.</p>
<p>K. Frank</p>

mahsa: <p>Amazing answer, thank u.</p>

phucdoitoan: <p>As <a class="mention" href="/u/kfrank">@KFrank</a> said, the two classes are indeed the same.</p>
<p>If you look at the “__ init__.py” <a href="https://github.com/pytorch/pytorch/blob/master/torch/nn/__init__.py" rel="noopener nofollow ugc">files</a> of the “nn” module, you see that the first line</p>
<pre><code class="lang-auto">from .modules import *
</code></pre>
<p>imports all the names defined in the “__ all__” list in “modules” 's “__ init__.py” <a href="https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/__init__.py" rel="noopener nofollow ugc">file</a> into the namespace of “nn”. And you can check that “Module”, which is imported from “module”</p>
<pre><code class="lang-auto">from .module import Module
</code></pre>
<p>, is contained in the “__ all__” list.</p>
<p>Hence, “nn.Module” is indeed “nn.modules.module.Module”</p>
<p>For details, you can see this python docs, 6.4. <a href="https://docs.python.org/3/tutorial/modules.html" rel="noopener nofollow ugc">Packages</a></p>