Is there a nice way to get all layer properties?

What’s a nice way to get all the properties for a given layer type, maybe in an iteratable way?

For example, for an nn.Linear layer, I am reading currently getting them as:

for name, layer in model.named_modules():
    if isinstance(layer, nn.Linear):
        in_features = model._modules[name].in_features
        out_features = model._modules[name].out_features
        weight = model._modules[name].weight
        bias = model._modules[name].bias

Not only am I accessing a protected member of the class but it’s also error prone, since I want to do this for other layer types as well. is there a nice way to get all the layer properties? I am trying to copy all these and set them for my own custom version of each layer!

You could check if the .__dict__ or dir() approach would work for you:

lin = nn.Linear(1, 1)
print(lin.__dict__)
print(dir(lin))

However, both approaches would still need some parsing, checks, etc. since a lot of internals would be printed.

from the snippet, it seems that you want parameters not properties (python attributes), that’s simply done with module.named_parameters([recurse=False perhaps])

Thank you for your amazing help as always, @ptrblck !

@googlebot thank you! But I wanted to include things such as ‘in_channels’ and ‘out_channels’ which are not parameters, if I understand correctly.

Right, these are not well formalized, though for built-in modules you can get relevant names from module.__constants__ (this attribute was added to reference module’s scalars in JIT mode, but mostly reflects constructor arguments)

Thank you. Yes, I might do a mix of __constants__ and __parameters__ to get all the details I want.