# Copy.deepcopy() vs clone()

**URL:** <https://discuss.pytorch.org/t/copy-deepcopy-vs-clone/55022>\
**Category:** Uncategorized\
**Created:** [September 3, 2019, 7:33am UTC](https://discuss.pytorch.org/t/copy-deepcopy-vs-clone/55022 "2019-09-03T07:33:29Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![pinocchio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pinocchio/32/22093_2.png) [@pinocchio](https://discuss.pytorch.org/u/pinocchio)\
**Post date:** [June 17, 2020, 7:01pm UTC](https://discuss.pytorch.org/t/copy-deepcopy-vs-clone/55022/10 "2020-06-17T19:01:16Z")

</div>

Let me see if I understand (it seems the accepted answer here is outdated, `.data` is not in the library or going to be removed according to what I’ve read in other answers with from albanD).

`.clone()` produces a new tensor instance with a new memory allocation to the tensor data. In addition it remembers the history of the original tensor and is connected to the earlier graph and appears as `CloenBackward`. The main advantage it seems is that its safer wrt in-place ops afaik.  
`deepcopy` make a deep copy of the original tensor meaning it creates a new tensor instance with a new memory allocation to the tensor data (it definitively does this part correctly from my tests). I assume it also does a complete copy of the history too, either pointing to the old history or create a brand new deep copy history. I’m unsure how to test this but I believe if it is to behave as a proper deep copy method then it should create a new history that is a mirror of the earlier (instead of just pointing to it).

Test I did wrt memory allocation:

```auto
def clone_vs_deepcopy():
    import copy
    import torch

    x = torch.tensor([1,2,3.])
    x_clone = x.clone()
    x_deep_copy = copy.deepcopy(x)
    #
    x.mul_(-1)
    print(f'x = {x}')
    print(f'x_clone = {x_clone}')
    print(f'x_deep_copy = {x_deep_copy}')

```

output

```auto
x = tensor([-1., -2., -3.])
x_clone = tensor([1., 2., 3.])
x_deep_copy = tensor([1., 2., 3.])

```

since neither changed it must be a different memory. I just realized I could have checked it with `id` or something…alas.

I am still seeking clarification on the history part. Is it a deep copy of that or a pointer copy if we use deep copy?

I know for know for clone it is a pointer copy to the original history and not a complete deep copy.

* * *

related:

> <https://stackoverflow.com/questions/62437509/what-is-the-difference-between-detach-clone-and-deepcopy-in-pytorch-tensors-in>

---

_[View the full topic](https://discuss.pytorch.org/t/copy-deepcopy-vs-clone/55022)._
