LL_*_*LL_ 5 python deep-learning deprecation-warning pytorch tensor
我目前正在使用tensor.resize()函数将张量调整为新的形状t = t.resize(1, 2, 3)
。
这给了我一个过时的警告:
不推荐使用非原位调整大小
因此,我想切换到该tensor.resize_()
功能,这似乎是适当的就地替换。但是,这给我留下了
无法调整需要毕业的变量的大小
错误。我可以回到
from torch.autograd._functions import Resize
Resize.apply(t, (1, 2, 3))
Run Code Online (Sandbox Code Playgroud)
tensor.resize()这样做是为了避免过时警告。这似乎不是一个合适的解决方案,但对我来说却是一个hack。tensor.resize_()
在这种情况下,我该如何正确利用?
您可以改为选择使用tensor.reshape(new_shape)
或torch.reshape(tensor, new_shape)
作为:
# a `Variable` tensor
In [15]: ten = torch.randn(6, requires_grad=True)
# this would throw RuntimeError error
In [16]: ten.resize_(2, 3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-16-094491c46baa> in <module>()
----> 1 ten.resize_(2, 3)
RuntimeError: cannot resize variables that require grad
Run Code Online (Sandbox Code Playgroud)
以上RuntimeError
可以通过使用解决或避免tensor.reshape(new_shape)
In [17]: ten.reshape(2, 3)
Out[17]:
tensor([[-0.2185, -0.6335, -0.0041],
[-1.0147, -1.6359, 0.6965]])
# yet another way of changing tensor shape
In [18]: torch.reshape(ten, (2, 3))
Out[18]:
tensor([[-0.2185, -0.6335, -0.0041],
[-1.0147, -1.6359, 0.6965]])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11843 次 |
最近记录: |