How to load my pre-trained model into a modified new one model?

I’m using to save my model and re-load it these functions,


I want to add a new layer to my model and re-save it again as a new model instead of calling two models in my new model. how can I load the parameters only to the pre-trained part in the new model that matches the old model ?

model.load_state_dict(sd, strict=False)
This argument matches layers, you have to be sure to have clear names. If your new layer has the same name as an old layer you will probably have an error

1 Like

I’ll re-interpret.

you mean I should create my new model and assure matching the name of old layers in both models but new layer should have different names.
Once I use model.load_state_dict(sd, strict=False), trained layers will be loaded while new layers will not be altered ? and this is enough to migrate from old to new model preserving pre-trained layers ?

Yes. I mean, I guess this works when you add some layers. I guess state_dict is an ordered layer therefore if you do a very big change it’s easier this method fails.

Anyway state dic is a python dictionary, you can manage it like that. You can access each weight to check their values. You can also access model parameters and compare some test weights to check if loading worked as expected.