在view()的使用官方文档中, view()要求被操作的Tensor是地址连续的, 可以通过tensor.is_contiguous()来查看它是否连续,如果不连续,就需要使用Tensor.contiguous()来把它转化为地址连续的张量.或者使用Tensor.reshape()代替.
这里我们来做两组实验:
>>> import torch
>>> a = torch.randn(2,3,4)
>>> a.shape
torch.Size([2, 3, 4])
>>> a = a.permute(1,0,2)
>>> a.shape
torch.Size([3, 2, 4])
>>> a.view(2,3,4) # 报错地址不连续
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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.
>>> a.view(3,2,4) # 注意这里之所以可以是因为和a.permute之后的shape相同
tensor([[[ 0.1874, 0.3489, -0.9825, -0.3453],
[-0.0242, -0.1042, 0.3636, -0.6031]],
[[ 0.5304, 0.5788, 0.2269, -1.1097],
[-0.4714, 1.5973, -0.7571, 0.3635]],
[[-0.2278, 0.6185, 0.3474, -0.2274],
[-0.1819, 2.3646, 1.2536, 0.2955]]])
>>> a.view(3,4,2) # 报错地址不连续
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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.
>>> b = a.reshape(3,4,2) # 使用reshape代替或者转为地址连续的张量
>>> a = a.contiguous() # 转化为地址连续的Tensor
>>> a.view(3,4,2)
tensor([[[ 0.1874, 0.3489],
[-0.9825, -0.3453],
[-0.0242, -0.1042],
[ 0.3636, -0.6031]],
[[ 0.5304, 0.5788],
[ 0.2269, -1.1097],
[-0.4714, 1.5973],
[-0.7571, 0.3635]],
[[-0.2278, 0.6185],
[ 0.3474, -0.2274],
[-0.1819, 2.3646],
[ 1.2536, 0.2955]]])
>>>