请问为什么每次resize时pytorch.tensor对应的storage都变了?
I wonder why the corresponding storage of pytorch.tensor changes every time it is resized?
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()))
运行第一次
2356950450056
2356950450056
2356950450056
2356950450056
运行第二次
2206021857352
2206301638600
2206021857352
2206301638600
为什么id有时变有时不变,网上找了半天。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。 (为我糟糕的英语道歉)
提前致谢。
您不需要查看对象或调整对象大小来观察此行为,对同一对象调用 storage
可以 return 不同的 ID:
a = torch.tensor([3,2,3,4])
print(id(a.storage()))
print(id(a.storage()))
>>> 2308579152
>>> 2308422224
这是因为构造了python存储对象when calling.storage()
.
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()))
运行第一次
2356950450056
2356950450056
2356950450056
2356950450056
运行第二次
2206021857352
2206301638600
2206021857352
2206301638600
为什么id有时变有时不变,网上找了半天。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。 (为我糟糕的英语道歉) 提前致谢。
您不需要查看对象或调整对象大小来观察此行为,对同一对象调用 storage
可以 return 不同的 ID:
a = torch.tensor([3,2,3,4])
print(id(a.storage()))
print(id(a.storage()))
>>> 2308579152
>>> 2308422224
这是因为构造了python存储对象when calling.storage()
.