Copying parameters


suppose I have two models, A and B which share the identical architecture. I did something like modelA = modelB since I want to copy the parameters in B to A. I found this works, but is this the safe way to do so?

In your case, modelA and modelB are just references to the same underlying object representing the model. So there is no two models nor copying here. I don’t know if that is what you want, but that is what happens.

Thank you for your reply. assume A is a pretrained model and B and C are two newly constructed models. If I do modelA=modelB=modelC, did you mean that now A, B and C are the same thing?

After your construct the models, modelA, modelB and modelC are not the actual models, they are just references to the model objects. When you do modelA=modelB=modelC, you are changing the first two references to point to the third. The models they previously point to, if not referenced by other variables, are lost.

1 Like

Oh, I see. Thank you very much.

If you want, you can use deep copy from the copy library. You will be sure to have 2 independent objects.

modelB = copy.deepcopy(modelA)

1 Like

Thanks! That’s what I want.