How to reuse nn.Module with different architecture


I have a problem with reusing an nn.Module with a new architecture.


class network(nn.Module)
def __init__(self, architecture):
     # architecture 1 details

def forward(self):
     # train network details

net = network(architecture)
# and optimizer and such

if __name__ == "__main__":
     # train architecture 1 
     output = net()

     architecture2 = create_new_arch()

     # create architecture 2
     del  net
     net = network(architecture2)

     # train architecture 2
     output = net()

Normally, this will train architecture 1 perfectly fine but when it comes to the second architecture, the error doesn’t seem to budge at all. I tried testing the architecture2 independently and it seems fine. How can I reuse the net object that I have with a new architecture?

Thank you.

Your new network has a new set of parameters. You need to re-create the optimizer too because the old optimizer isn’t tracking your new parameters.