2021-11-02

张量维度变换后,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]])

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值