3-24Pytorch与张量变形

在这里插入图片描述

import torch
a = torch.rand((2,3))
print(a)
out = a.reshape(3,2)
print(out)
tensor([[0.9521, 0.0812, 0.1984],
        [0.2137, 0.3824, 0.9961]])
tensor([[0.9521, 0.0812],
        [0.1984, 0.2137],
        [0.3824, 0.9961]])
#转置
print(torch.t(out))
tensor([[0.9521, 0.1984, 0.3824],
        [0.0812, 0.2137, 0.9961]])
#指定交换维度
print(torch.transpose(out,0,1))
tensor([[0.9521, 0.1984, 0.3824],
        [0.0812, 0.2137, 0.9961]])
a = torch.rand(1,2,3)
out = torch.transpose(a,1,0)
print(a)
print(out)
print(out.shape)
tensor([[[0.5815, 0.3386, 0.0553],
         [0.1253, 0.3207, 0.9636]]])
tensor([[[0.5815, 0.3386, 0.0553]],

        [[0.1253, 0.3207, 0.9636]]])
torch.Size([2, 1, 3])
out = torch.squeeze(a)
print(out)
print(out.shape)
out = torch.unsqueeze(a,-1)
print(out.shape)
tensor([[0.5815, 0.3386, 0.0553],
        [0.1253, 0.3207, 0.9636]])
torch.Size([2, 3])
torch.Size([1, 2, 3, 1])
out = torch.unbind(a,dim = 1)
print(out)
(tensor([[0.5815, 0.1253]]), tensor([[0.3386, 0.3207]]), tensor([[0.0553, 0.9636]]))
print(a)
print(torch.flip(a,dims=[1,2]))
tensor([[[0.5815, 0.3386, 0.0553],
         [0.1253, 0.3207, 0.9636]]])
tensor([[[0.9636, 0.3207, 0.1253],
         [0.0553, 0.3386, 0.5815]]])
a = torch.rand((2,3))
print(a)
print(torch.rot90(a))
tensor([[0.9152, 0.1834, 0.8199],
        [0.8542, 0.5633, 0.1858]])
tensor([[0.8199, 0.1858],
        [0.1834, 0.5633],
        [0.9152, 0.8542]])
print(torch.rot90(a,2))#默认逆时针
tensor([[0.1858, 0.5633, 0.8542],
        [0.8199, 0.1834, 0.9152]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值