pytorch --- tensor.permute()和torch.transpose()

tensor.permute (DIM1, to M2, DIM3, ...)

permute can be transposed to any high-dimensional matrix. But only tensor.permute () calls this way

x = torch.rand(2,3,4)
print("x.shape:", x.shape)
x = x.permute(2,1,0)
print("x.shape:", x.shape)

Output:

x.shape: torch.Size([2, 3, 4])
x.shape: torch.Size([4, 3, 2])
[Finished in 1.0s]

Example 2:

t.rand(2,3,4,5).permute(3,2,0,1).shape
Out[669]: torch.Size([5, 4, 2, 3])

Summary
incoming permute method parameter is a dimension, Dim before conversion is not performed [0, 1, 2] way, the conversion represents the first dimension and a second dimension 0 exchange

torch.transpose(tensor, dim1, dim2)

2D can transpose operation transpose matrix (transpose is the time to switch between only two dimensions, other dimensions remain unchanged). There are two ways to call. Continuous use transpose permute the effect can be achieved

torch.transpose(Tensor, 1, 0)
t.rand(2,3,4,5).transpose(3,0).transpose(2,1).transpose(3,2).shape
Out[672]: torch.Size([5, 4, 2, 3])
t.rand(2,3,4,5).transpose(1,0).transpose(2,1).transpose(3,1).shape
Out[670]: torch.Size([3, 5, 2, 4])

The difference between transpose and permute

The main difference is the transpose can only switch between two dimensions, permute it can convert several dimensions

Reference
PyTorch high dimensional matrix transpose Transpose and Permute

Published 33 original articles · won praise 1 · views 2617

Guess you like

Origin blog.csdn.net/orangerfun/article/details/103957890