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()
a.add_(1)

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

Results:
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) :(

If you use numpy.ndarray.__array_interface__ and torch.Tensor.data_ptr(), you can find the memory location of their data_array.
a = torch.ones(5)
b = a.numpy()
a.add_(1)

print(b.__array_interface__)
# &gt; {'data': (1848226926144, False),
#    'strides': None,
#    'descr': [('', '&lt;f4')],
#    'typestr': '&lt;f4',
#    'shape': (5,),
#    'version': 3}

print(a.data_ptr())
# &gt; 1848226926144

a.data_ptr() == b.__array_interface__[&quot;data&quot;][0]
# &gt; True

Both the array and the tensor share the same data_array.
We can reasonably conclude that the method torch.Tensor.numpy() creates a numpy array from the Tensor by passing it the reference to its data_array.
b[0] = 4
print(a)
# &gt; tensor([4., 2., 2., 2., 2.])

