NotImplementedError in forward while implementing NAS using nn.ModuleList()

Hello, I could really use some expertise.

I am getting a NotImplementedError with this code:

class Network(nn.Module):

    def __init__(self, layers):

        super(Network, self).__init__()
        
        model = nn.ModuleList()
        
        # NAS SETUP
        for layer in layers:
            if len(model) == 0:
                input_dim = 4
            model.append(nn.Conv2d(input_dim, layer['output_dim'], kernel_size=layer['kernel']))
            model.append(nn.SiLU(inplace=True))
            model.append(nn.MaxPool2d(4))
            model.append(nn.BatchNorm2d(layer['output_dim']))
            input_dim = layer['output_dim']
        
        #TODO: fully connected layer
        #model.append(nn.Flatten())
        #model.append(nn.Linear(1408, 5))

        self.model = model


    def forward(self, flow):
        output = self.model(flow)
        return output

I have no idea why. I could not find any support topics online to help either.

nn.ModuleList acts like a Python list with the advantage to properly register all modules inside the parent model.
However, you can’t call it directly and might want to use nn.Sequential instead or index the modules:

for m in self.model:
    x = m(x)