I was going through the basic tutorials of PyTorch and came across conversion between NumPy arrays and Torch tensors. The documentation says:
The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other.
But, this does not seem to be the case in the below code:
import numpy as np
a = np.ones((3,3))
b = torch.from_numpy(a)
np.add(a,1,out=a)
print(a)
print(b)
In the above case I see the changes automatically reflected in the output:
[[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]]
tensor([[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]], dtype=torch.float64)
But same doesn't happen when I write something like this:
a = np.ones((3,3))
b = torch.from_numpy(a)
a = a + 1
print(a)
print(b)
I get the following output:
[[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]]
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
What am I missing here?