view和reshape
两者用法完全相同
x=torch.rand(4,3,28,28)
a=x.view(4,3,28*28)
print(a.shape)
一定要记住原始数据的存储方式,不然无法恢复数据 如果维度变换后数据量出现变化,则会提示不能变换维度
squeeze和unsqueeze
squeeze——减少维度 unsqueeze——增加维度
x=torch.rand(4,3,28,28)
print(x.unsqueeze(0).shape)
print(x.unsqueeze(-1).shape)
print(x.unsqueeze(-3).shape)
x=torch.rand(1,32,1,1)
print(x.squeeze().shape)
print(x.squeeze(1).shape)
print(x.squeeze(-1).shape)
print(x.squeeze(-3).shape)
expand和repeat
expend——不会主动重复数据 repeat——会主动重复数据
x=torch.rand(1,32,1,1)
print(x.expand(4,32,4,4).shape)
print(x.expand(4,-1,3,-1).shape)
x=torch.rand(1,32,1,1)
print(x.repeat(4,32,1,1).shape)
转置
x=torch.randn(2,3)
print(x)
print(x.t())
维度交换
x=torch.randn(4,3,32,32)
print(x.transpose(1,2).shape)
print(x.permute(0,3,1,2).shape)
x.contiguous()
|