张量维度变换后,size重构报错
构建神经网络训练时,张量维度变换和size重构是常用的方法,但当它们一起使用时可能会因为数据不连续导致报错。
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces).
首先复现一下报错情况:
In [81]:a = torch.randn(1,2,3)
In [82]:b = a.transpose(1,2)
In [83]: a
Out[83]:
tensor([[[ 0.9346, 0.2563, -0.0076],
[-1.2805, -0.9691, 1.0943]]])
In [84]:b
Out[84]:
tensor([[[ 0.9346, -1.2805],
[ 0.2563, -0.9691],
[-0.0076, 1.0943]]])
可以看到,维度变换正常,但是当执行b.view()时:
b.view(1,2*3)
Traceback (most recent call last):
line 1, in <module>
b.view(1,2*3)
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
解决办法:
##1.使用.reshape()方法
In [93]:b.reshape(1,6)
Out[93]:tensor([[ 0.9346, -1.2805, 0.2563, -0.9691, -0.0076, 1.0943]])
##2.使用.contiguous()方法将不连续的数据转换成连续数据,再变换size
In [94]:b.contiguous().view(1,6)
Out[94]:tensor([[ 0.9346, -1.2805, 0.2563, -0.9691, -0.0076, 1.0943]])