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

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?
This is the layer code and this is the model code

Hi Mahsa!

If I understand python correctly (which I don’t) nn.Module and
nn.Modules.Module are the same class.

As I understand it the module (in the python sense) nn.Module
in effect does:

from torch.nn.Modules import Module

This makes the name Module (from nn.Modules) visible in nn.
So, if in your code you have import torch.nn as nn, you can
in your code then refer to the class torch.nn.Modules.Module
as nn.Module.

Best regards.

K. Frank

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?

Hello Mahsa!

I’m a little bit confused, too.

Let me revise what I said.

I don’t think there is a torch.nn.Modules.Module. Compare the
documentation for torch.nn.Module:


with this code:

Source code for torch.nn.modules.module

From this I conclude that the linked code is the code for the
class torch.nn.Module. I’m guessing that the url that refers
to “_modules/torch/nn/modules/module.html” is somehow
indicating how pytorch organizes its code, but does not mean
that the class’s full name is “torch.nn.Modules.Module”
(or “torch.nn.modules.Module”).

If this doesn’t make sense, could you link to the Layer code you
mentioned in your original post so we can see how it refers to
torch.nn.Modules.Module and try to figure out what is going on?


K. Frank

1 Like

Thanks K Frank. I added the link of the codes.

Hi Mahsa!

Thanks for the link to the layer code. That clears things up.
Yes, the two Module classes are indeed one and the same.

Look specifically at this line in layers.py

from torch.nn.modules.module import Module

So let me go back to a version of my first reply to you – there is
a class torch.nn.modules.module.Module and the package
(python “module”) torch.nn imports it, making it visible as

To see what’s going on, run this script:

from torch.nn import Module as NnModule
print ('NnModule =', NnModule)
from torch.nn.modules.module import Module as MModule
print ('MModule =', MModule)

When I run this I get:

NnModule = <class 'torch.nn.modules.module.Module'>
MModule = <class 'torch.nn.modules.module.Module'>

So, the package torch.nn is, in effect, doing something like

from torch.nn.modules.module import Module

and this makes torch.nn.modules.module.Module visible as

(I was getting tangled up a little bit before with the
modules.module.Module and the capitalization.)

Best regards.

K. Frank


Amazing answer, thank u.

As @KFrank said, the two classes are indeed the same.

If you look at the “__ init__.py” files of the “nn” module, you see that the first line

from .modules import *

imports all the names defined in the “__ all__” list in “modules” 's “__ init__.py” file into the namespace of “nn”. And you can check that “Module”, which is imported from “module”

from .module import Module

, is contained in the “__ all__” list.

Hence, “nn.Module” is indeed “nn.modules.module.Module”

For details, you can see this python docs, 6.4. Packages

1 Like