2016-11-05 153 views
1

我使用TensorFlow,我有以下矩阵:TensorFlow - 张量重塑

U2 = tf.constant([[1,3],[0,1],[1,0]],dtype=tf.float32) 

[[ 1. 3.] 
[ 0. 1.] 
[ 1. 0.]] 

什么是重塑这种矩阵,使其结果在张量的最简单的方法:

tf.constant([[[1,1],[0,0],[1,1]],[[3,3],[1,1],[0,0]]],dtype=tf.float32) 

[[[ 1. 1.] 
    [ 0. 0.] 
    [ 1. 1.]] 

[[ 3. 3.] 
    [ 1. 1.] 
    [ 0. 0.]]] 

回答

1

这是一个快速的方法使用TensorFlow API创建的第一子矩阵:

U2 = tf.constant([[1,3],[0,1],[1,0]],dtype=tf.float32) 
first_col = tf.unpack(U2, axis=1)[0] 
repeated_cols = tf.tile(first_col, [2]) 
repeated_cols = tf.reshape(repeated_cols, [3,2]) 

这将创建

[[ 1. 1.] 
    [ 0. 0.] 
    [ 1. 1.]]