Вместо этого вы можете выбрать tensor.reshape
или torch.reshape
, например:
# 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
# RuntimeError can be resolved by using `tensor.reshape`
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]])