Best way to register modules in custom module

Hi there,

I have a custom module that creates several layers. All of the tutorials show the layers being added as attributes during __init__().
I like the idea of adding them as modules usint the add_modules() method instead (it seems more declarative considering these are sequential models).
Is this considered bad? and if so, why?

For example:

#this class has the modules added with add_module
class UpsampleLayer(nn.Module):
    def __init__(self, scale):
        upsample = nn.Upsample(scale_factor = scale, mode = "nearest")
        self.add_module("upsample", upsample)

 #this class does not
class UpsampleLayer(nn.Module):
    def __init__(self,scale):
        self.upsample = nn.Upsample(scale_factor = scale, mode = "nearest")

    def forward(self,x)
        return self.upsample(x)`
1 Like

Concrete small, illustrative example?

Thanks, i had not figured out how to use the block quoting yet… first post :slight_smile:

In my own opinion, if someone has never used pytorch before, they might prefer your version potentially. For anyone who has used at least one pytorch module using the second syntax, the second syntax might look more natural, familiar and compact. At least, for me, the second form looks natural and standard, whereas the first form makes me scratch my head and wonder what is .add_module, and wonder what is the difference compared to the second form :slight_smile:

I assume they will both work identically, from a functional point of view (an assumption though, not having ever seen/used the first form).