Hi,
Apologies if this is already a thing and I am just blind.
My use case is the following:
Assume you create a custom Block that subclasses nn.Module
. Something like a ResNet-Block.
Now assume that inside that Block, you create standardized but still dynamic layers, also subclassing nn.Module
.
You can manually include all the corresponding parameters using add_module()
, naming them with a string. Very handy, works like a charm.
But according to this - How can i use module.name - and a dir(BlockInstance)
search, any nn.Module
created in this way doesn’t have a .name
parameter, or at least I cannot find it. I figure it must have one, somewhere, because otherwise model.named_modules()
wouldn’t be able to give the name of my Blocks correctly.
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 self.name
would come in handy here.
The workaround is also fairly simple, I just do
for n, m in model.named_modules():
m.name = n
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?
If neither, would it make sense to add a .name
parameter to the child module automatically when it is added via add_module()
?
P.S.: Sorry for posting Uncategorized, I don’t know where it’d make sense to put this.