Pytorch中View和Permute函数比较

View和Permute函数

描述

View和Permute函数都可以用来reshape(调整矩阵形状),本文主要用来测试其不同点。

实验

测试代码:

def test_view_permute():
    origin = torch.arange(12).view(3, 4)
    print(origin)
    dest1 = origin.view(4, 3)
    dest2 = origin.permute(1, 0)
    print(f'dest1, {dest1}')
    print(f'dest2, {dest2}')

输出结果:

tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
dest1, tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])
dest2, tensor([[ 0,  4,  8],
        [ 1,  5,  9],
        [ 2,  6, 10],
        [ 3,  7, 11]])

在控制台输出详细信息:

dest1.is_contiguous()
True
dest2.is_contiguous()
False
dest1.stride()
(3, 1)
dest2.stride()
(1, 4)

结论

使用View时,是在连续数据上进行reshape(原始数据origin[1][2]!=dest1[2][1]);
使用Permute时,是在维度上做了变换,类似于转置T,(原始数据origin[1][2]==dest1[2][1])。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值