torch.permute()和np.transpose()

import torch
import numpy as np
a = np.arange(24).reshape(3,4,2)
print('before', a)
b = np.transpose(a,(1,0,2))
print('b',b)
c = torch.tensor(a)
d = c.permute(1,0,2)
print('d:',d)

out:

/usr/bin/python3 /home/thu/test_python/transpose_permute.py
before [[[ 0  1]
  [ 2  3]
  [ 4  5]
  [ 6  7]]

 [[ 8  9]
  [10 11]
  [12 13]
  [14 15]]

 [[16 17]
  [18 19]
  [20 21]
  [22 23]]]
b [[[ 0  1]
  [ 8  9]
  [16 17]]

 [[ 2  3]
  [10 11]
  [18 19]]

 [[ 4  5]
  [12 13]
  [20 21]]

 [[ 6  7]
  [14 15]
  [22 23]]]
d: tensor([[[ 0,  1],
         [ 8,  9],
         [16, 17]],

        [[ 2,  3],
         [10, 11],
         [18, 19]],

        [[ 4,  5],
         [12, 13],
         [20, 21]],

        [[ 6,  7],
         [14, 15],
         [22, 23]]])

Process finished with exit code 0

结论:转换效果一样,只不过transpose是对np操作,permute是对tensor操作

你可能感兴趣的:(torch.permute()和np.transpose())