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]])

猜你喜欢

转载自blog.csdn.net/weixin_46815330/article/details/113109848