1. 程式人生 > >tf.transpose函數解析

tf.transpose函數解析

輸出 con .sh shape 矩陣 tensor flow 函數 port

import tensorflow as tf
import numpy as np

x = tf.constant([1,2,3,4])
y = tf.transpose(x)
with tf.Session() as sess:
print(sess.run(y),sess.run(y).shape)

‘‘‘
輸出:[1 2 3 4] (4,)
tf.transpose(x)在1階張量上使用,不變
‘‘‘

x = tf.constant([[1, 2], [3, 4]])
y1 = tf.transpose(x,[0,1])
y2 = tf.transpose(x,[1,0])
with tf.Session() as sess:
print(sess.run(y1), sess.run(y1).shape)
print(sess.run(y2), sess.run(y2).shape)

‘‘‘
輸出:
[[1 2]
[3 4]] (2, 2)
[[1 3]
[2 4]] (2, 2)

y1 = tf.transpose(x,[0,1]): y1=x
y2 = tf.transpose(x,[1,0]): y2=x進行矩陣轉置y2[i][j]=x[j][i]
‘‘‘


x = tf.constant([[[1, 2], [3, 4]], [[11, 12], [13, 14]]])
y1 = tf.transpose(x,[0,1,2])
y2 = tf.transpose(x,[1,0,2])
y3 = tf.transpose(x,[2,1,0])
with tf.Session() as sess:
print(sess.run(y1), sess.run(y1).shape)
print(sess.run(y2), sess.run(y2).shape)
print(sess.run(y3), sess.run(y3).shape)
‘‘‘
[[[ 1 2]
[ 3 4]]

[[11 12]
[13 14]]] (2, 2, 2)

y1 = tf.transpose(x,[0,1,2]): 不變
y2 = tf.transpose(x,[1,0,2]):y2[i][j][k]=x[j][i][k]
y3 = tf.transpose(x,[2,1,0]):y3[i][j][k]=x[k][j][i]

‘‘‘

tf.transpose函數解析