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