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.

1 Like

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'
1 Like

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

1 Like