tf.transpose多维函数的用法讲解 tf.transpose函数的用法讲解(多维情况,看似复杂,其实也简单)

tf.transpose函数的用法讲解(多维情况,看似复杂,其实也简单)
[html] view plain copy
  1. tf.transpose(a, perm=Nonename='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.  

本文主要讨论高维度的情况:

为了形象理解高维情况,这里以矩阵组合举例:

先定义下: 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])代表将三位数组的高和行进行转置。

我们写个测试程序如下:

[python] view plain copy
  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, [012])  
  7. b=tf.transpose(x, [021])  
  8. c=tf.transpose(x, [102])  
  9. d=tf.transpose(x, [120])  
  10. e=tf.transpose(x, [210])  
  11. f=tf.transpose(x, [201])  
  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

运行脚本,结果一致,如下:

[html] view plain copy
  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]是正常显示,那么交换哪两个数字,就是把对应的输入张量的对应的维度对应交换即可。

猜你喜欢

转载自blog.csdn.net/u013550000/article/details/80420344
今日推荐