我想知道为什么pytorch.tensor的相应存储在每次调整大小时都会发生变化?

2024-03-29 06:55:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

为什么我有时会变,但有时不会变,我在网上搜索了很长时间。但是没有用。请帮助或尝试给出一些如何实现这一点的想法。(为我糟糕的英语道歉) 提前感谢。


1条回答
网友
1楼 · 发布于 2024-03-29 06:55:50

您无需查看或调整对象大小即可观察此行为,对同一对象调用storage可以返回不同的id:

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

这是因为python存储对象是when calling.storage()构造的

相关问题 更多 >