Access individual networks in transfer learning setting

Assume you have model 1 and model 2. We are in a transfer learning setting, so I want to evaluate model1 and then model2. This looks like the perfect application for nn.Sequential!

Except that we want to train model1 and model2 differently (Say, we want to freeze the weights of model1). To do this we need an easy way to identify model1 and model2, but nn.Sequential does not provide this (or does it?). You can list all the modules, but this will list also the layers inside model1 and model2, which I don’t want.

So for now I wrote a simple class like:

class TransferNetwork(nn.Module):
    def __init__(self, modules):
        super(TransferNetwork, self).__init__()
        self.modules_list = nn.ModuleList(modules)

    def forward(self, input):
        for module in self.modules_list:
            input = module(input)
        return input

so that the informations about the networks is easily accessible through obj.modules_list[idx]. Is there a way to do this with nn.Sequential directly? Or this is what I am supposed to be doing?

this is exactly what you are supposed to be doing. shoehorning it into nn.Sequential is not a great idea, Sequential is meant to be a very simple container that’s dumb.

1 Like