1
import torch

a = torch.tensor([3,2,3,4])
b = a.view(2,2)
c = a.resize(2,2)
d = a.resize_(2,2)
print(id(a.storage()))
print(id(b.storage()))
print(id(c.storage()))
print(id(d.storage()))

run at first time

2356950450056
2356950450056
2356950450056
2356950450056

run at second time

2206021857352
2206301638600
2206021857352
2206301638600

Why does id change sometimes but not change sometimes, I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. (apologies for my poor English) Thanks in advance.

1 Answers1

0

You don't need to view or resize the object to observe this behaviour, calling storage on the same object can return different id's:

a = torch.tensor([3,2,3,4])
print(id(a.storage()))
print(id(a.storage()))
>>> 2308579152
>>> 2308422224

This is because the python storage object is constructed when calling .storage().

iacob
  • 20,084
  • 6
  • 92
  • 119