reshape、permute和transpose的区别

69 篇文章 8 订阅

直接上代码:

import torch
a = torch.arange(12).reshape(3, 2, 2)
print(a)
print(a.reshape(3, 4))  # 按照现在要求的格式进行重新排序
print("-----------")
c = a.permute(2, 1, 0)  # 将原先维度的数据挑选出来按照现在要求的维度进行重新组合
print(c)
print(c.shape)
print("-----------")
d = a.transpose(2, 1)  # 将第三维和第二维进行转置 
print(d)
print(d.shape)

输出结果

tensor([[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]],

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

        [[ 1,  5,  9],
         [ 3,  7, 11]]])
torch.Size([2, 2, 3])
-----------
tensor([[[ 0,  2],
         [ 1,  3]],

        [[ 4,  6],
         [ 5,  7]],

        [[ 8, 10],
         [ 9, 11]]])
torch.Size([3, 2, 2])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值