Usage of nn.ModuleList

Hi, I wrote a simple script here, utilizing the new nn.ModuleList.

 module = [nn.Conv2d(1,64, 3, stride = (1,1), padding = (1,1)), nn.BatchNorm2d(
 self.module_list = nn.ModuleList(module)
 self.module_list += module

Each “module”, is composed of a Conv2d, and a BatchNorm. What I would like self.module_list to be, is: Conv2d -> BatchNorm -> Conv2d -> BatchNorm

Is this the right way to go about it? I worry that I am not really composing a function, but re-using the same set of weights twice… which I don’t want of course. I want each Conv2d, BN to be unique of course.

Is this the right way to go about it?

Thanks!!

No, you’re adding the same modules, with the same weights twice. You need to create a new list, and append that after construction.

def make_sequence():
    return [nn.Conv2d(1,64, 3, stride = (1,1), padding = (1,1)), ...]

self.module_list = nn.ModuleList()
for i in range(5):
    self.module_list += make_sequence()