tf.transpose函式的用法講解(多維情況,看似複雜,其實也簡單)

Candy_GL發表於2018-10-08

版權宣告:本文為博主原創文章,歡迎轉載,請標明出處。 https://blog.csdn.net/cc1949/article/details/78422704

tf.transpose函式中文意思是轉置,對於低維度的轉置問題,很簡單,不想討論,直接轉置就好(大家看下面文件,一看就懂)。

 


 
  1. tf.transpose(a, perm=None, name='transpose')

  2.  
  3. Transposes a. Permutes the dimensions according to perm.

  4.  
  5. The returned tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.

  6.  
  7. For example:

  8. # 'x' is [[1 2 3]

  9. # [4 5 6]]

  10. tf.transpose(x) ==> [[1 4]

  11. [2 5]

  12. [3 6]]

  13.  
  14. # Equivalently

  15. tf.transpose(x perm=[1, 0]) ==> [[1 4]

  16. [2 5]

  17. [3 6]]

  18.  
  19. # 'perm' is more useful for n-dimensional tensors, for n > 2

  20. # 'x' is [[[1 2 3]

  21. # [4 5 6]]

  22. # [[7 8 9]

  23. # [10 11 12]]]

  24. # Take the transpose of the matrices in dimension-0

  25. tf.transpose(b, perm=[0, 2, 1]) ==> [[[1 4]

  26. [2 5]

  27. [3 6]]

  28.  
  29. [[7 10]

  30. [8 11]

  31. [9 12]]]

  32.  
  33. Args:

  34. •a: A Tensor.

  35. •perm: A permutation of the dimensions of a.

  36. •name: A name for the operation (optional).

  37.  
  38. Returns:

  39.  
  40. A transposed Tensor.

  41.  

 

本文主要討論高維度的情況:

為了形象理解高維情況,這裡以矩陣組合舉例:

先定義下: 2 x (3*4)表示2個3*4的矩陣,(其實,它是個3維張量)。

x = [[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[21,22,23,24],[25,26,27,28],[29,30,31,32]]]

輸出:

---------------
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[21 22 23 24]
  [25 26 27 28]
  [29 30 31 32]]]
---------------

 

重點來了:

tf.transpose的第二個引數perm=[0,1,2],0代表三維陣列的高(即為二維陣列的個數),1代表二維陣列的行,2代表二維陣列的列。
tf.transpose(x, perm=[1,0,2])代表將三位陣列的高和行進行轉置。

我們寫個測試程式如下:

 


 
  1. import tensorflow as tf

  2.  
  3. #x = tf.constant([[1, 2 ,3],[4, 5, 6]])

  4. x = [[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[21,22,23,24],[25,26,27,28],[29,30,31,32]]]

  5. #a=tf.constant(x)

  6. a=tf.transpose(x, [0, 1, 2])

  7. b=tf.transpose(x, [0, 2, 1])

  8. c=tf.transpose(x, [1, 0, 2])

  9. d=tf.transpose(x, [1, 2, 0])

  10. e=tf.transpose(x, [2, 1, 0])

  11. f=tf.transpose(x, [2, 0, 1])

  12.  
  13. # 'perm' is more useful for n-dimensional tensors, for n > 2

  14. # 'x' is [[[1 2 3]

  15. # [4 5 6]]

  16. # [[7 8 9]

  17. # [10 11 12]]]

  18. # Take the transpose of the matrices in dimension-0

  19. #tf.transpose(b, perm=[0, 2, 1])

  20. with tf.Session() as sess:

  21. print ('---------------')

  22. print (sess.run(a))

  23. print ('---------------')

  24. print (sess.run(b))

  25. print ('---------------')

  26. print (sess.run(c))

  27. print ('---------------')

  28. print (sess.run(d))

  29. print ('---------------')

  30. print (sess.run(e))

  31. print ('---------------')

  32. print (sess.run(f))

  33. print ('---------------')


我們期待的結果是得到如下矩陣:

a: 2 x 3*4

b: 2 x 4*3

c: 3 x 2*4

d: 3 x 4*2

e: 4 x 3*2

f: 4 x 2*2

執行指令碼,結果一致,如下:

 


 
  1. ---------------

  2. [[[ 1 2 3 4]

  3. [ 5 6 7 8]

  4. [ 9 10 11 12]]

  5.  
  6. [[21 22 23 24]

  7. [25 26 27 28]

  8. [29 30 31 32]]]

  9. ---------------

  10. [[[ 1 5 9]

  11. [ 2 6 10]

  12. [ 3 7 11]

  13. [ 4 8 12]]

  14.  
  15. [[21 25 29]

  16. [22 26 30]

  17. [23 27 31]

  18. [24 28 32]]]

  19. ---------------

  20. [[[ 1 2 3 4]

  21. [21 22 23 24]]

  22.  
  23. [[ 5 6 7 8]

  24. [25 26 27 28]]

  25.  
  26. [[ 9 10 11 12]

  27. [29 30 31 32]]]

  28. ---------------

  29. [[[ 1 21]

  30. [ 2 22]

  31. [ 3 23]

  32. [ 4 24]]

  33.  
  34. [[ 5 25]

  35. [ 6 26]

  36. [ 7 27]

  37. [ 8 28]]

  38.  
  39. [[ 9 29]

  40. [10 30]

  41. [11 31]

  42. [12 32]]]

  43. ---------------

  44. [[[ 1 21]

  45. [ 5 25]

  46. [ 9 29]]

  47.  
  48. [[ 2 22]

  49. [ 6 26]

  50. [10 30]]

  51.  
  52. [[ 3 23]

  53. [ 7 27]

  54. [11 31]]

  55.  
  56. [[ 4 24]

  57. [ 8 28]

  58. [12 32]]]

  59. ---------------

  60. [[[ 1 5 9]

  61. [21 25 29]]

  62.  
  63. [[ 2 6 10]

  64. [22 26 30]]

  65.  
  66. [[ 3 7 11]

  67. [23 27 31]]

  68.  
  69. [[ 4 8 12]

  70. [24 28 32]]]

  71. ---------------

 

最後,總結下:

[0, 1, 2]是正常顯示,那麼交換哪兩個數字,就是把對應的輸入張量的對應的維度對應交換即可。

--------------------- 本文來自 cc19 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/cc1949/article/details/78422704?utm_source=copy 

相關文章