Loading model weights layer by layer

I have a model definition , and i have its pre trained weights.
I have now done modifications to the original model , by adding some new layers.
now , i want to load the pre trained weights into the new model such that , the original layers , are loaded with their corresponding weights, while the new layers are randomly initialized.

making strict = False , is also not giving the correct weights.
can someone help me to manually initialize the weights accordingly.

Just get the state dict of your new model. It’s a dictionary in the end. Replace the layers that you need with the stored ones. Then just load the state dict normally.

2 Likes

thank you , its working now for me .