Error of MaxUnpool2d in ModuleList [ forward() takes 2 positional arguments but 3 were given]

Hello, I have a problem when my model is running.
I have searched for the solution but I didn’t find it anywhere.
So hopefully I would get the solution here.

I have a piece of code generating a ModuleList in the init() method.

            if pooling_type == 1:
                maxpool = nn.MaxPool2d(kernel, stride=stride, return_indices=True)
                module_block.add_module('maxpool2d_{}'.format(index), maxpool)
                maxunpool = nn.MaxUnpool2d(kernel, stride=stride)
                module_block.add_module('maxunpool2d_{}'.format(index), maxunpool)

Then in the forward(), I have this code.

                if pooling_type == 1:
                    x, return_ids[index] = self.module_list[index](x)
                else:           # unpooling layer must be supplied with ids.
                    ids_from = int(module['ids_from'])
                    ids = return_ids[ids_from]
                    x = self.module_list[index](x, ids)

self.module_list is a ModuleList generated in the init method.
I keep the return_indices in a dictionary called return_ids.
However when it came to unpooling layer, it threw an error that

TypeError: forward() takes 2 positional arguments but 3 were given


module_block is nn.Sequential().

I am not sure what is wrong with the code.

Has anyone experienced this error?

I solved it myself !

I realized that nn.Sequential allows only 1 parameter.
The work around solution is to access the maxunpooling layer by index.

For example

x = self.module_list[index][0](x, ids)

Hi baboonga,

You can replace nn.Sequential with a custom subclass like below.

class MultiPrmSequential(nn.Sequential):
    def __init__(self, *args):
        super(MultiPrmSequential, self).__init__(*args)

    def forward(self, *input):
        for module in self._modules.values():
            input = module(*input)
        return input