Calling N models of the same architecture from one model

I have an architecture that’s composed of 2 components.

  • N encoders
  • N decoders
    def __init__(self, N, **kwargs):
        super(MODEL, self).__init__()
        encoders = [Encoder(**kwargs)] * N
        decoders = [Decoder(**kwargs)] * N

    def forward(self, *input):

I’m not sure how do I call each (encoder/decoder)'s forward method properly (each will take input) .
Knowing that I also use a custom loss function (will I need to override backward as well?).

Also, am I constructing the encoders properly? is there a better/convenient way?

Hi, there exists torch.nn. ModuleList in pytorch. You need to instantiate your model N times and append them to that list. Then, you can operate as if a python list were.

If you use python lists itself, you will be able to call each model but they won’t be properly registered in the parent module.