Add_module with automatic name attribute

username: FreddyJ
cooked: <p>Hi,</p>
<p>Apologies if this is already a thing and I am just blind.</p>
<p>My use case is the following:<br>
Assume you create a custom Block that subclasses <code>nn.Module</code>. Something like a ResNet-Block.<br>
Now assume that inside that Block, you create standardized but still dynamic layers, also subclassing <code>nn.Module</code>.<br>
You can manually include all the corresponding parameters using <code>add_module()</code>, naming them with a string. Very handy, works like a charm.</p>
<p>But according to this - <a href="https://discuss.pytorch.org/t/how-can-i-use-module-name/84585" class="inline-onebox">How can i use module.name</a> - and a <code>dir(BlockInstance)</code> search, any <code>nn.Module</code> created in this way doesn’t have a <code>.name</code> parameter, or at least I cannot find it. I figure it must have one, somewhere, because otherwise <code>model.named_modules()</code> wouldn’t be able to give the name of my Blocks correctly.</p>
<p>The use case in my case is figuring out where in the network I am by checking the name of everything involved and printing it, a <code>self.name</code> would come in handy here.</p>
<p>The workaround is also fairly simple, I just do</p>
<pre><code class="lang-auto">for n, m in model.named_modules():
    m.name = n
</code></pre>
<p>but I bet I am missing something. Is there a more straightforward/intended way for doing this, or is referring to layers by their names explicitly discouraged for some reason?<br>
If neither, would it make sense to add a <code>.name</code> parameter to the child module automatically when it is added via <code>add_module()</code>?</p>
<p>P.S.: Sorry for posting Uncategorized, I don’t know where it’d make sense to put this.</p>

username: JuanFMontesinos
cooked: <p>If you inspect pytorch code you will find submodules and names are computed recursively.<br>
It’s a tree-like structure. Your parent class is only aware of the childs but nothing else farther in the tree.<br>
Hashing by name would imply a tree search. So in short it would end up being the same as what you can currently do by iterating over the model paramters.</p>
<p>Also note that the name depends from which module you call the named_modules, i.e,<br>
if you call model.subnetwork3.named_modules() then all the names would be different.</p>

username: FreddyJ
cooked: <p>Yeah, I noticed that, but it was not really an issue, since I was only printing the names for debugging and not using them for anything.</p>
<p>However, I was trying to go up the tree, so to speak, not down. Finding the parent module’s name from the child module.</p>

username: JuanFMontesinos
cooked: <p>I don’t understand what do you mean.<br>
You cannot access the parent module from the child (thus you are not aware of the name of the parent).</p>
<p>If you want to hash the root node given the name of any child you can implement so. At some point of my life i borrowed the pytorch’s tree machinery to use it for directory ops <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: FreddyJ
cooked: <p>“You cannot access the parent module from the child”.</p>
<p>Ah, ok, this would have been what I wanted. It’s all cleared up then.</p>