How to get the module names of nn.Sequential

For example,

model = nn.Sequential()
model.add_module('conv0', conv0)
model.add_module('norm0', norm0)

Is there a way to get the names of these added modules? Thanks

1 Like

You could print all names and sub-modules using:

for name, module in model.named_modules():
    print(name)

If you want to directly access these modules, you can just use:

print(model.conv0)
7 Likes

note that model.children() is not recursive as outlined by fmass in Module.children() vs Module.modules()

1 Like

Hi,

i am getting following error:

TypeError: named_modules() missing 1 required positional argument: ‘self’

Did you override this method in your nn.Model? If not, could you post an executable code snippet to reproduce this issue, please?