Adding Convolutional Layers using For-Loop

Hi there,
I wanted to build a neural network which accepts the number of convolutional blocks [ conv layer + relu + maxpool ] as input from the user and then build the model accordingly. I thought of using a for loop for this purpose and the following is the code i tried to implement :

self.model = nn.Module
        for i in range(conv_blocks):
            name = "conv_{}".format(i+1)
            n_feats = 64*(2**i)
            name = "relu_{}".format(i+1)
            name = "maxpool_{}".format(i+1)

However I’m getting the following error :

TypeError: add_module() missing 1 required positional argument: 'module'

Please help me with this one.


self.model = nn.Module

doesn’t really look like you’re supposed to do it. You could use nn.Module().
An even cleaner way might be to use a ModuleList or Sequential.

Best regards


Thanks alot !
It works :slight_smile: