## Permute() function of dimension exchange in Python

Wooden calyx 2020-11-13 08:47:20
permute function dimension exchange python

First step , Give the official document about permute The explanation of ：

https://pytorch.org/docs/stable/tensors.html?highlight=permute#torch.Tensor.permute

The second step , I'll explain the official documents in vernacular .

permute The Chinese meaning is ,“ Permutation and combination ”.permute() Any dimension of a tensor can be exchanged .

Let's look at one demo：

``````import torch
a = torch.randn(2,3,5)
b = a.permute(1,2,0)
print(b.shape)
``````

Be careful ,permute The parameter in is the tensor a Dimension index of . therefore ,permute The dimension of the input parameter of must be the same as a Agreement , And it can only be 0,1,2...,dim This value , Only in this way can we index to... One by one a Dimensions inside .

a.permute(1,2,0) It means , hold a The first dimension of is put at the end of .

### Conclusion

For the same tensor, Its total number of elements is fixed ,permute Its function is to turn m×n×c, Change to n×m×c or c×n×m And any combination .

For two dimensions tensor,permute(1,0) What we do is transpose , Equivalent to transpose().