Initialize network with a pretrained network that is trained together

We can use torch.load to load a pretrained model (e.g., xx.pth file). But how can we initialize a network with a pretrained network in the same loop?

For example, I have two networks with the same structure, net1 and net2. During training, I train net1 first, and then I want to load net1’s weight to net2 to initialize net2. Can I somehow do it without saving net1’s weight to a file and then load it back to net2?

Thank you.

model2.load_state_dict(model1.state_dict())

1 Like

Thank you so much! This is so convenient.