Jonathan
Jonathan

Reputation: 1936

Pytorch variable changes numpy variable even though memory addresses are not same

I have the following bit of code:

a = torch.ones(10); b = a.numpy()
a[0] += 1
print(a, b)

Both variables essentially hold the same values even though I only modified a. However, I checked the memory addresses of a and b using hex(id(a)) and they're different. So in this case, is b a pointer to a? What's going on?

Upvotes: 0

Views: 128

Answers (1)

zihaozhihao
zihaozhihao

Reputation: 4475

Actually the raw data is at the same address. You can check like this,

a.storage().data_ptr()                    

Out[16]: 93866530123392

b.__array_interface__['data']             

Out[17]: (93866530123392, False)

Upvotes: 1

Related Questions