tensorflow中的reshape与transpose

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/SPESEG/article/details/102664871

这俩函数要弄清楚,很重要的,其中一个用法是:axis重排的问题,reshape的结果是不同的。

import tensorflow as tf
import numpy as np

xx=np.random.randn(1,6,3)
xxtf=tf.constant(xx,dtype=tf.float32)
with tf.Session() as sess:
    xxtf_reshape=tf.reshape(xxtf,[1,3,6])
    xxtf_transpose=tf.transpose(xxtf,[0,2,1])
    xxtf_reshape,xxtf_transpose = sess.run([xxtf_reshape,xxtf_transpose])
    print(xx,xxtf_reshape,xxtf_transpose,sep='\n')

我的本意也就是想重排下axis,而不是reshape,reshape的结果是拼接的结果。

[[[ 0.77403113  1.21476616  0.60511668]
  [ 0.58202963  0.37541983 -0.76856027]
  [ 0.10879649 -0.68860221 -0.98868981]
  [ 0.35907672  0.01088391 -1.127845  ]
  [-0.93021183 -1.08421861  1.53445572]
  [ 0.5705503  -0.31313591  0.17674343]]]
[[[ 0.77403116  1.2147661   0.60511667  0.58202964  0.37541983
   -0.7685603 ]
  [ 0.10879649 -0.6886022  -0.98868984  0.35907674  0.01088391
   -1.127845  ]
  [-0.93021184 -1.0842186   1.5344558   0.5705503  -0.3131359
    0.17674343]]]
[[[ 0.77403116  0.58202964  0.10879649  0.35907674 -0.93021184
    0.5705503 ]
  [ 1.2147661   0.37541983 -0.6886022   0.01088391 -1.0842186
   -0.3131359 ]
  [ 0.60511667 -0.7685603  -0.98868984 -1.127845    1.5344558
    0.17674343]]]

但要明白一点,transpose本质上还是reshape,只不过稍有不同。

注意:大多情况下unstack和transpose一起使用,而reshape真的是用来reshape的。

另外有相关问题可以加入QQ群讨论,不设微信群

QQ群:868373192 

语音深度学习群

猜你喜欢

转载自blog.csdn.net/SPESEG/article/details/102664871