Allen Qin
Allen Qin

Reputation: 19947

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?

Upvotes: 2

Views: 207

Answers (1)

kmario23
kmario23

Reputation: 61365

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     # <========= 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      # <========== 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.

Upvotes: 3

Related Questions