180424 tf.transpose()的使用方法

tf.transpose

tf.transpose():
perm是排列的意思,默认情况下使用,将颠倒axis的循序
指定perm可以根据需要对原来的array顺序进行变换

  • 代码
import tensorflow as tf
import numpy as np

data =  np.arange(1,25).reshape((2,3,4))

x1 = tf.constant(data)
x2 = tf.transpose(x1)
x3 = tf.transpose(x1,perm=[0,2,1])
with tf.Session() as sess:
    print('The array:\n',sess.run(x1))
    print('Default perm:\n',sess.run(x2))
    print('Keep axis-0 and transpose axis-1 and axis-2:\n',sess.run(x3))
  • 结果
The array:
 [[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[13 14 15 16]
  [17 18 19 20]
  [21 22 23 24]]]
Default perm:
 [[[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]

 [[ 4 16]
  [ 8 20]
  [12 24]]]
Keep axis-0 and transpose axis-1 and axis-2:
 [[[ 1  5  9]
  [ 2  6 10]
  [ 3  7 11]
  [ 4  8 12]]

 [[13 17 21]
  [14 18 22]
  [15 19 23]
  [16 20 24]]]

猜你喜欢

转载自blog.csdn.net/qq_33039859/article/details/80064164
今日推荐