PyTorch in-place operator issue in NumPy conversion

I converted a PyTorch Tensor into a NumPy ndarray, and as it’s shown below ‘a’ and ‘b’ both have different ids so they point to different memory locations.

I know in PyTorch underscore indicates in-place operation so a.add_(1) does not change the memory location of a, but why does it change the content of the array b (due to the difference in id)?

import torch
a = torch.ones(5)
b = a.numpy()

print(id(a), id(b))


139851293933696 139851293925904
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]

PyTorch documentation:

Converting a torch Tensor to a NumPy array and vice versa is a breeze.
The torch Tensor and NumPy array will share their underlying memory
locations, and changing one will change the other.

But why?
(They have different IDs so must be independent) :frowning:

id() is inappropriate because python objects are not value objects, i.e. they link to other objects, and you just have multiple links here (see .storage().data_ptr() to reason about address identities)

1 Like

Thanks a lot for your reply