question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

model.deepcopy() does not do a deep copy

See original GitHub issue

Description

A deep copy should lose any link to the original object, so that one can modify the copy with no side effects on the original object. In a Jupyter notebook, something happens that makes this fail.

Expected behavior

I would expect that a deepcopy would allow to modify the copy without any change happening to the original model.

Actual behavior

The original model gets changed when I change the parameters of the new model in place (e.g. with _fitter_to_model_params). It turns out that, when I run the method in a Jupyter notebook, the parameter array in the copied version is not a deepcopy, but a reference to the original parameter array, as demonstrated below. I couldn’t reproduce this by adding the lines below to the existing unit tests

Steps to Reproduce

# define power law component
pl = models.PowerLaw1D()
pl2 = pl.deepcopy()
id(pl) != id(pl2)   #---> True
id(pl.parameters) != id(pl2.parameters) #--> False!!

System Details

macOS-10.16-x86_64-i386-64bit Python 3.8.8 (default, Feb 24 2021, 13:46:16) [Clang 10.0.0 ] Numpy 1.20.1 astropy 4.2 and 4.3.dev754+gba283e72d Scipy 1.6.1 Matplotlib 3.3.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
perrygreenfieldcommented, Apr 7, 2021

If we documented it for this case, there are many other cases where it would need to be documented to be consistent. That seems like a rather large job. It is essentially true for any computed property (though most properties are not computed).

1reaction
perrygreenfieldcommented, Apr 7, 2021

I think we tracked it down. It really isn’t a bug, but it sure is an unexpected behavior. The parameters attribute is actually a property, and the resulting array is regenerated every time that attribute is accessed. Apparently what is going on is that the first id() call returns a result (the memory address of the array), that result is stored in a temporary location, and the array memory is released immediately. The second id() call results in the reuse of the same memory location (which apparently doesn’t happen when I call them on separate lines), and thus the same id value, confusing all of us.

The moral is: don’t trust the identity of properties.

Read more comments on GitHub >

github_iconTop Results From Across the Web

copy — Shallow and deep copy operations — Python 3.11.1 ...
Source code: Lib/copy.py Assignment statements in Python do not copy objects, ... needs to make a deep copy of a component, it should...
Read more >
copy in Python (Deep Copy and Shallow Copy) - GeeksforGeeks
The copy() returns a shallow copy of the list, and deepcopy() returns a deep copy of the list. As you can see that...
Read more >
Shallow and deep copy in Python: copy(), deepcopy()
Use copy.copy() if you want to make a shallow copy of an object for which the copy() method is not provided. Deep copy: ......
Read more >
Python copy.deepcopy() function not working properly
The problem is that you are actually copying the class definition and not an instance of the class. Another problem of the code...
Read more >
Can I deepcopy a model? - PyTorch Forums
You can deepcopy a model: model = nn.Linear(1, 1) model_copy = copy.deepcopy(model) with torch.no_grad(): model.weight.fill_(1.) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found