Are there any recommended methods to clone a model?

Can confirm, deepcopy does not work (changes to original still reflected in copy) but pickle does work.

2 Likes

classifier = pickle.loads(pickle.dumps(self.classifier))
TypeError: can’t pickle module objects

Using Adam’s suggestion:

threw:

TypeError: can't pickle dict_keys objects

for the model I am working with.

I am using python 3.7 and the model was trained on multiple GPUs.

Has anyone run into this issue with their models? Any ideas how to fix it?

Searching online, I found similar issue with deepcopy (but not in the context of PyTorch):

Apparently in python3 you have to wrap dict.keys() in list() — otherwise the deepcopy issue appears.

The answer turned out to be pretty simple. The instance attributes of your model have to be picklable. In my particular case, storing dict_keys caused the issue. Converting those to list, resolved the issue:

model.attribute = list(model.attribute)  # where attribute was dict_keys
model_clone = copy.deepcopy(model)

If I just want to copy the state dict then would temp = model.state_dict() work or do I need deep copy for state_dict as well? I later keep training so would the temp variable change?

Hi,

Yes you need to deepcopy it if you want a deep copy.
If you just do this, the temp value will be changed when you update the model.

1 Like

I use pytorch C++ interface. I need to do deep copy for modules. I think I am going to go with this route: 1) dump one module onto the disk using torch save, 2) load the dumped file into a new module class.

What’s the corresponding methods of C++ API?

Did you find any solution?

With Pytorch 1.7, I’m not finding copy.deepcopy(model) to work.

model_clone = copy.deepcopy(model)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-173-0885027ec90d> in <module>()
----> 1 model_clone = copy.deepcopy(model)

AttributeError: 'function' object has no attribute 'deepcopy'
2 Likes

…oh, woops. Have to
import copy
first. Now it works!

1 Like

Excuse, I just meet some problem and found problem in copy the model.
I am news to machine learn and pytorch, so my question maybe looks stupid. I am currently using version of python 3.11 and torch 2.0.1. I am trying to copy a model with copy.deepcopy() after the model training and try to reactivate the train. However, I meet an error message “AttributeError: ‘NoneType’ object has no attribute ‘data’”. It seems like there is not grad in param _group in optimizer. Then I found
`copy.deepcopy` does not copy gradients of nn.Parameter · Issue #95711 · pytorch/pytorch · GitHub
state out that the copy model would not contain the param gradient.
May I please ask is there any proper way to copy the model? Or any other method that I can just restore a new “grad” attribute to the copy model?