Pytorch: Updating numpy array not updating the corresponding tensor
When I run the following code,

import numpy as np
a = np.ones(3)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)


Both a and b are 2s.

However, When I run:

import numpy as np
a = np.ones(3)
b = torch.from_numpy(a)
a = a+1
print(a)
print(b)


b remains as 1s while a has been updated to 2s.

Is this an expected behaviour? 

Yes, as @hpaulj pointed out in his comment, the operation

a = a + 1


creates copy of the original array a and adds 1 using broadcasting. And after addition, since we assign it to a, so a gets updated to the result of addition operation. But, b still shares the memory of the original array a (i.e. the array a that was created before updation.)

So, we see result like this:

In [75]: a = np.ones(3)
    ...: b = torch.from_numpy(a)
    ...: a = a+1     # &lt;========= creates copy of `a` and modifies it
    ...: print(a)
    ...: print(b)
    ...: 
[ 2.  2.  2.]

 1
 1
 1
[torch.DoubleTensor of size 3]




But, see what happens when you rather do:

In [72]: a = np.ones(3)
    ...: b = torch.from_numpy(a)
    ...: a += 1      # &lt;========== in-place modification of `a`
    ...: print(a)
    ...: print(b)
    ...:

[ 2.  2.  2.]

 2
 2
 2
[torch.DoubleTensor of size 3]


Observe how the += operation is making modification to the original array in-place whereas somearr = somearr + 1 creates copy of the array somearray and then makes modification to it.
