pytorch 中 view() 地址连续的实验

在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]]])
>>> 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值