How to get layer names in a network?

blade: <p>I have a model defined as</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>How can I get an iterator/list/generator with the name of all layers, namely, <code>['cl1', 'cl2', 'fc1']</code>?</p>

OrielBanne: <p>I think simply printing the model should work here:</p>
<p>net = MyModel()<br>
print(net)</p>
<p>if you are using a notebook - you don’t even need the print command</p>
<p>here is what you get:</p>
<p>MyModel(<br>
(cl1): Linear(in_features=25, out_features=60, bias=True)<br>
(cl2): Linear(in_features=60, out_features=84, bias=True)<br>
(fc1): Linear(in_features=84, out_features=10, bias=True)<br>
(params_list_a): ParameterList(<br>
(0): Parameter containing: [torch.FloatTensor of size 60x25]<br>
(1): Parameter containing: [torch.FloatTensor of size 84x60]<br>
(2): Parameter containing: [torch.FloatTensor of size 84x10]<br>
)<br>
(params_list_b): ParameterList(<br>
(0): Parameter containing: [torch.FloatTensor of size 60x25]<br>
(1): Parameter containing: [torch.FloatTensor of size 60]<br>
(2): Parameter containing: [torch.FloatTensor of size 84x60]<br>
(3): Parameter containing: [torch.FloatTensor of size 84]<br>
(4): Parameter containing: [torch.FloatTensor of size 10x84]<br>
(5): Parameter containing: [torch.FloatTensor of size 10]<br>
)<br>
)</p>
<p>print(net.cl2)<br>
Linear(in_features=60, out_features=84, bias=True)</p>
<p>but this assumes you know the layer names and does not provide for iteration</p>
<p>for name, module in net.named_children():<br>
if not name.startswith(‘params’):<br>
print(name)<br>
print(module)<br>
print(’------’)</p>
<h2>
<a name="cl1-linearin_features25-out_features60-biastrue-1" class="anchor" href="#cl1-linearin_features25-out_features60-biastrue-1"></a>cl1<br>
Linear(in_features=25, out_features=60, bias=True)</h2>
<h2>
<a name="cl2-linearin_features60-out_features84-biastrue-2" class="anchor" href="#cl2-linearin_features60-out_features84-biastrue-2"></a>cl2<br>
Linear(in_features=60, out_features=84, bias=True)</h2>
<h2>
<a name="fc1-linearin_features84-out_features10-biastrue-3" class="anchor" href="#fc1-linearin_features84-out_features10-biastrue-3"></a>fc1<br>
Linear(in_features=84, out_features=10, bias=True)</h2>
<p>so now you can create a list:<br>
layers_list=[]<br>
for name, module in net.named_children():<br>
if not name.startswith(‘params’):<br>
layers_list.append(name)</p>
<p>layers_list = [‘cl1’, ‘cl2’, ‘fc1’]</p>

tom: <pre><code class="lang-python">model = MyModel()
</code></pre>
<p>you can get the dirct children (but it also contains the ParameterList/Dict, because they are also <code>nn.Module</code>s internally):</p>
<pre><code class="lang-auto">print([n for n, _ in model.named_children()])
</code></pre>
<p>If you want all submodules recursively (and the main model with the empty string), you can use <code>named_modules</code> instead of <code>named_children</code>.</p>
<p>Best regards</p>
<p>Thomas</p>