How to get layer names in a network?

I have a model defined as

class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.cl1 = nn.Linear(25, 60)
        self.cl2 = nn.Linear(60, 84)
        self.fc1 = nn.Linear(84, 10)

        self.feed_back()

    def feed_back(self):

        self.params_list_a = nn.ParameterList([
            nn.Parameter(torch.randn(60, 25)),
            nn.Parameter(torch.randn(84, 60)),
            nn.Parameter(torch.randn(84, 10))
        ])

        self.params_list_b = nn.ParameterList([
            self.cl1.weight,
            self.cl1.bias,
            self.cl2.weight,
            self.cl2.bias,
            self.fc1.weight,
            self.fc1.bias
        ])

    def forward(self, x):
        x = F.relu(self.cl1(x))
        x = F.relu(self.cl2(x))

        return self.fc1(x)

How can I get an iterator/list/generator with the name of all layers, namely, ['cl1', 'cl2', 'fc1']?

I think simply printing the model should work here:

net = MyModel()
print(net)

if you are using a notebook - you don’t even need the print command

here is what you get:

MyModel(
(cl1): Linear(in_features=25, out_features=60, bias=True)
(cl2): Linear(in_features=60, out_features=84, bias=True)
(fc1): Linear(in_features=84, out_features=10, bias=True)
(params_list_a): ParameterList(
(0): Parameter containing: [torch.FloatTensor of size 60x25]
(1): Parameter containing: [torch.FloatTensor of size 84x60]
(2): Parameter containing: [torch.FloatTensor of size 84x10]
)
(params_list_b): ParameterList(
(0): Parameter containing: [torch.FloatTensor of size 60x25]
(1): Parameter containing: [torch.FloatTensor of size 60]
(2): Parameter containing: [torch.FloatTensor of size 84x60]
(3): Parameter containing: [torch.FloatTensor of size 84]
(4): Parameter containing: [torch.FloatTensor of size 10x84]
(5): Parameter containing: [torch.FloatTensor of size 10]
)
)

print(net.cl2)
Linear(in_features=60, out_features=84, bias=True)

but this assumes you know the layer names and does not provide for iteration

for name, module in net.named_children():
if not name.startswith(‘params’):
print(name)
print(module)
print(’------’)

cl1
Linear(in_features=25, out_features=60, bias=True)

cl2
Linear(in_features=60, out_features=84, bias=True)

fc1
Linear(in_features=84, out_features=10, bias=True)

so now you can create a list:
layers_list=[]
for name, module in net.named_children():
if not name.startswith(‘params’):
layers_list.append(name)

layers_list = [‘cl1’, ‘cl2’, ‘fc1’]

model = MyModel()

you can get the dirct children (but it also contains the ParameterList/Dict, because they are also nn.Modules internally):

print([n for n, _ in model.named_children()])

If you want all submodules recursively (and the main model with the empty string), you can use named_modules instead of named_children.

Best regards

Thomas

1 Like