Use self.named_modules() return name is empty

kellenf: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/b/b7309d563f97b235d411707676b7fbf56446bf6a.png" data-download-href="https://discuss.pytorch.org/uploads/default/b7309d563f97b235d411707676b7fbf56446bf6a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/b/b7309d563f97b235d411707676b7fbf56446bf6a.png" alt="image" data-base62-sha1="q8zx4kWrm8EKW3Dc9k5XuA6rbdg" width="690" height="475" data-dominant-color="0C0C0C"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1139×785 19.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
as show in the image,when I want to finetune the net,met the problem,however ,the same code is good before finetune.Thanks for your help</p>

ptrblck: <p>I assume you’ve manipulated the model somehow before executing the mentioned line of code?<br>
If so, could you post the code snippet?</p>

kellenf: <p>It may be a bug in pytorch,when I use self.mode._modules(), it works…</p>

ptrblck: <p>Could you post a reproducible code snippet, so that we can debug it?</p>

kellenf: <p>It’s a big project.<br>
the way  to build model is [backbone+neck+head]<br>
(1)the set params code in backbone</p>
<pre><code class="lang-python">    def set_params(self, cfg): # cfg is a BatchNorm and Conv2d parameters
        """Backbone is using &lt;layer type&gt; to set lr and wd"""
        self.params = []
        arranged_names = set()
        for name, module in self.named_modules():
            for key in cfg:
                if isinstance(module, eval(key)):
                    self.params.append({'params': name + ".weight", 'lr': cfg[key][0],
                                        'weight_decay': cfg[key][1]})
                    arranged_names.add(name + '.weight')
                    if not isinstance(module, nn.PReLU):
                        if module.bias is not None and len(cfg[key]) == 4:
                            self.params.append({'params': name + ".bias",
                                                'lr': cfg[key][2], 'weight_decay': cfg[key][3]})
                            arranged_names.add(name + '.bias')

        for name, param in self.named_parameters():
            if name in arranged_names:
                continue
            else:
                self.params.append({'params': name})

        self.param_setted = True
</code></pre>
<p>(2) the build model part<br>
build order: backbone-&gt;neck-&gt;head</p>
<pre><code class="lang-python">    def build(self, mtype, kwargs):
        module_name, cls_name = mtype.rsplit('.', 1) # Some way to import 'class module'
        module = importlib.import_module(module_name)
        cls = getattr(module, cls_name)
        return cls(**kwargs)

    def forward(self, input):
        for submodule in self.children():
            output = submodule(input)
            input.update(output)
        return input
</code></pre>
<p>It’s so strange meet the problem, thanks for help!<br>
Or can you explain what the diffence between self.named_modules() and self._modules ???<br>
Thanks!!</p>

kellenf: <p>OH,I find the result. Actrually, in the code</p>
<pre><code class="lang-python">for name, module in self.named_modules():
</code></pre>
<p>only in first time, name return ‘’ and module return the whole model. But in next iteration, the name return model’s layer name and module is the layer. Maybe it’s a special result in pytoch.</p>