Transfer part of the weights from one network to another

I have a pre-trained VAE. I want to transfer the weights of the decoder to the Generator.

I’ve searched all over the internet for a method to copy parts a Neural Network. None of them really helped what I am trying to do.

I have made sure both the generator and the decoder of the network to have the exact same architecture to make sure everything is implemented correctly.

However the way it is programmed in PyTorch is still unclear to me

Any kind of help is appreciated. Thanks in advanced

You can iterate over the layers (module.children) - for the decoder from start to end and for the encoder from end to start, and assign the .weight parameter from decoder to encoder

Do you mind to elaborate on this please?

You can create two different nn.modules
One for encoder and second for decoder
After that you can iterate over the layers (for later in module.children) and for each layer you can override its weights (an attribute of the layer)