How could I use one dimension index to visit the parameter of model?


Does there exist some function to iterate the parameter in one dimension index?

I’m afraid the way it is coded modules contains submodules which contains submodules etcetera… Therefore the parent nn.Module is not aware of it.

Besides parameters is an iterable.
The poor solution is saving the iterable into a list like:
list(model.parameters())[1]
The elegant solution is filter by name:

for name,param in model.parameters():
    if name == condition:
       do something

The last option is to directly query for it if you know the name:
model.decoder.weight (for example)

1 Like

Thank you for your reply. I did badly in explaining my meaning. I actually want to visit every single parameter. With model.parameters()[1], I expect to get the second parameter among the 11181642 parameters instead of a tensor with sizes of more than one. Do you know any ways to reach that?