tensor.reshape、transpose、flip、unbind、rot90的变形操作

在这里插入图片描述

reshape transpose

import torch
a = torch.rand(2, 3)
print('a\n', a)
# 变形
out = torch.reshape(a, (3, 2))
print('out\n', out)
# 转置
print(torch.t(out))
# 也是转置,意思就是将0和1的维度进行交换
print(torch.transpose(out, 0, 1))

输出结果:

a
 tensor([[0.1375, 0.3036, 0.2777],
        [0.7585, 0.9363, 0.4981]])
out
 tensor([[0.1375, 0.3036],
        [0.2777, 0.7585],
        [0.9363, 0.4981]])
tensor([[0.1375, 0.2777, 0.9363],
        [0.3036, 0.7585, 0.4981]])
tensor([[0.1375, 0.2777, 0.9363],
        [0.3036, 0.7585, 0.4981]])

unbind

import torch
a = torch.rand(1, 2, 3)
print('a\n', a)
# 从输出结果来看,是对a在维度为1上进行切分
out = torch.unbind(a, dim=1)
print('out1\n', out)
print('out1.shape\n', out[0].shape)
out = torch.unbind(a, dim=2)
print('out\n', out)
print('out.shape\n', out[0].shape)

输出结果:


a
 tensor([[[0.3706, 0.0505, 0.0693],
         [0.2060, 0.7625, 0.0705]]])
out1
 (tensor([[0.3706, 0.0505, 0.0693]]), tensor([[0.2060, 0.7625, 0.0705]]))
out1.shape
 torch.Size([1, 3])
out
 (tensor([[0.3706, 0.2060]]), tensor([[0.0505, 0.7625]]), tensor([[0.0693, 0.0705]]))
out.shape
 torch.Size([1, 2])

flip


a = torch.rand(1, 2, 3)
print('a\n', a)
# 翻转,维度为0 相当于不翻转
print('dims=0\n', torch.flip(a, dims=[0]))
#
print('dims=1\n', torch.flip(a, dims=[1]))
print('dims=1 shape\n', torch.flip(a, dims=[1]).shape)
print('a\n', a)
print('dims=2\n', torch.flip(a, dims=[2]))
print('dims=2 shape\n', torch.flip(a, dims=[2]).shape)
print('a\n', a)
# 先写1,再写2和先写2,再写1的结果是一样的
print('dims=1, 2\n', torch.flip(a, dims=[1, 2]))
print('dims=1, 2 shape\n', torch.flip(a, dims=[1, 2]).shape)
out = torch.flip(a, dims=[1])
out2 = torch.flip(out, dims=[2])
# 从结果来看,转两次和写在一个【】中的效果是一样的
print('out2\n', out2)

输出结果:


a
 tensor([[[0.7758, 0.6811, 0.1429],
         [0.2851, 0.7585, 0.1430]]])
dims=0
 tensor([[[0.7758, 0.6811, 0.1429],
         [0.2851, 0.7585, 0.1430]]])
dims=1
 tensor([[[0.2851, 0.7585, 0.1430],
         [0.7758, 0.6811, 0.1429]]])
dims=1 shape
 torch.Size([1, 2, 3])
a
 tensor([[[0.7758, 0.6811, 0.1429],
         [0.2851, 0.7585, 0.1430]]])
dims=2
 tensor([[[0.1429, 0.6811, 0.7758],
         [0.1430, 0.7585, 0.2851]]])
dims=2 shape
 torch.Size([1, 2, 3])
a
 tensor([[[0.7758, 0.6811, 0.1429],
         [0.2851, 0.7585, 0.1430]]])
dims=1, 2
 tensor([[[0.1430, 0.7585, 0.2851],
         [0.1429, 0.6811, 0.7758]]])
dims=1, 2 shape
 torch.Size([1, 2, 3])
out2
 tensor([[[0.1430, 0.7585, 0.2851],
         [0.1429, 0.6811, 0.7758]]])

rot90

import torch
a = torch.rand(1, 2, 3)
print('a\n', a)
# 默认逆时针旋转90度
out = torch.rot90(a)
print('out90\n', out)
print('out.shape\n', out.shape)
# 指定旋转180度
out = torch.rot90(a, 2)
print('out180\n', out)
print('out.shape\n', out.shape)
# 指定旋转360度
out = torch.rot90(a, 4)
print('out360\n', out)
print('out.shape\n', out.shape)
# 指定旋转-90度 并指定维度
out = torch.rot90(a, -1, [0, 2])
print('out-90\n', out)
print('out.sha pe\n', out.shape)

输出结果:

a
 tensor([[[0.8526, 0.7337, 0.5586],
         [0.7688, 0.5023, 0.9327]]])
out90
 tensor([[[0.7688, 0.5023, 0.9327]],

        [[0.8526, 0.7337, 0.5586]]])
out.shape
 torch.Size([2, 1, 3])
out180
 tensor([[[0.7688, 0.5023, 0.9327],
         [0.8526, 0.7337, 0.5586]]])
out.shape
 torch.Size([1, 2, 3])
out360
 tensor([[[0.8526, 0.7337, 0.5586],
         [0.7688, 0.5023, 0.9327]]])
out.shape
 torch.Size([1, 2, 3])
out-90
 tensor([[[0.8526],
         [0.7688]],

        [[0.7337],
         [0.5023]],

        [[0.5586],
         [0.9327]]])
out.sha pe
 torch.Size([3, 2, 1])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值