Deep Learning/PyTorch

PyTorch-permute vs transpose

Naranjito 2022. 12. 5. 14:51
  • permute

Returns a view of the original tensor input with its dimensions permuted.

It can exchange all dimentions.

torch.randn(235) --> permute(2,0,1) --> 5, 2, 3

x = torch.randn(2, 3, 5)

>>>
tensor([[[-1.7663,  1.2394, -0.4237, -2.3218,  0.1908],
         [ 1.6466,  0.8089, -2.2276, -0.5936,  2.8272],
         [ 0.8317,  0.9335, -1.3993, -0.3371,  1.3174]],

        [[ 0.5403, -0.4540, -0.5958,  0.4685,  0.5951],
         [ 0.9191,  0.3957, -0.9435,  0.4904,  0.5757],
         [-1.2492,  0.4371,  1.1425,  1.4849,  0.6856]]])
         
x.permute(2,0,1)

>>>
tensor([[[-1.7663,  1.6466,  0.8317],
         [ 0.5403,  0.9191, -1.2492]],

        [[ 1.2394,  0.8089,  0.9335],
         [-0.4540,  0.3957,  0.4371]],

        [[-0.4237, -2.2276, -1.3993],
         [-0.5958, -0.9435,  1.1425]],

        [[-2.3218, -0.5936, -0.3371],
         [ 0.4685,  0.4904,  1.4849]],

        [[ 0.1908,  2.8272,  1.3174],
         [ 0.5951,  0.5757,  0.6856]]])
         
(x.permute(2,0,1)).size()

>>>
torch.Size([5, 2, 3])

 

  • transpose

It can exchange only two dimensions.

x = torch.randn(2, 3)

>>>
tensor([[ 1.0028, -0.9893,  0.5809],
        [-0.1669,  0.7299,  0.4942]])

torch.transpose(x, 0, 1)

>>>
tensor([[ 1.0028, -0.1669],
        [-0.9893,  0.7299],
        [ 0.5809,  0.4942]])

'Deep Learning > PyTorch' 카테고리의 다른 글

ERROR: Failed building wheel for pytorch  (0) 2022.12.29
Pytorch-contiguous  (0) 2022.12.05
RNN  (0) 2022.08.22
Word2Vec VS Neural networks Emedding  (0) 2022.08.21
PyTorch-randint, scatter_, log_softmax, nll_loss, cross_entropy  (0) 2022.08.10