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():

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


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


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?