pytorch中维度扩展和维度交换-expand,repeat,permute,transpose

维度扩展

expand

a=torch.randn(1,32,1,1)
a.expand(4,32,14,14)
a.shape
torch.Size([4,32,14,14])

expand操作是在将该维度扩展成相应的维度,基于原来数据的基础上,推荐使用!注意,要扩展的维度必须是1维的!

repeat

a=torch.randn(1,32,1,1)
a.repeat(4,32,14,14).shape
a.shape

torch.Size([4,1024,14,14])
torch.Size([1,32,1,1])

repeat操作是将原有数据复制出来,在相应的维度上将数据复制多少份,原有的数据不变,耗内存,不建议使用!

维度交换

permute:

a=torch.rand(4,3,28,32)
a.permute(0,2,3,1).shape

torch.Size([4,28,32,3])

permute中的参数是要进行维度交换的tensor中的维度索引,就是将第几维度放在第0,1,2,3维度上

transpose

a=torch.rand(4,3,28,32)
a.transpose(1,3).shape

torch.Size([4,32,28,3])

transpose中只能是2个参数,表示哪两个维度进行交换!

你可能感兴趣的:(Python)