Retrieve paramter list of module

Is there a way to retrieve the parameter list of e.g. a nn.Conv2d module, i.e. both the arguments that were passed and the set default parameters, after the module has been created?

Thank you in advance!

I believe you can use python dunder method __dict__ on any python object. If I instantiate conv2d module like this:

conv2d = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3)

Calling conv2d.__dict__ gives me this:

{'_backward_hooks': OrderedDict(),
 '_buffers': OrderedDict(),
 '_forward_hooks': OrderedDict(),
 '_forward_pre_hooks': OrderedDict(),
 '_load_state_dict_pre_hooks': OrderedDict(),
 '_modules': OrderedDict(),
 '_non_persistent_buffers_set': set(),
 '_parameters': OrderedDict([('weight', Parameter containing:
               tensor([[[[-0.0343,  0.0877, -0.1922],
                         [-0.0998,  0.0335,  0.1608],
                         [-0.0976,  0.0042,  0.0078]],
               
                       ...
               
                        [[ 0.0101,  0.1680, -0.1120],
                         [-0.0862,  0.0624, -0.0306],
                         [ 0.0852, -0.0843, -0.0521]]]], requires_grad=True)),
              ('bias', Parameter containing:
               tensor([ 0.0879,  0.0465, -0.1899, -0.1272,  0.0014,  0.0306],
                      requires_grad=True))]),
 '_reversed_padding_repeated_twice': (0, 0, 0, 0),
 '_state_dict_hooks': OrderedDict(),
 'dilation': (1, 1),
 'groups': 1,
 'in_channels': 3,
 'kernel_size': (3, 3),
 'out_channels': 6,
 'output_padding': (0, 0),
 'padding': (0, 0),
 'padding_mode': 'zeros',
 'stride': (1, 1),
 'training': True,
 'transposed': False}

If you need, you can filter out parameters which are starts with _

thank you, but I was referring to the parameter list exclusively

I am not sure if I understand correctly, but here is couple of options:

  1. If you need just module arguments’ names (without values), you can just use __dict__, filtering out privates starting with _ and converting the returned dictionary keys into list or even more simply like this:
[arg_name for arg_name in conv2d.__dict__ if not arg_name.startswith('_')]
  1. If you need module arguments’ names and their respective values, it seams appropriate to me to use __dict__ as shown in my previous post (also filtering out privates starting with _)
1 Like

thank you, I will try that!