2017-05-24 50 views
0

我想做如何連接N個張量? N是運行時決定

M = tf.concat([tensor]*N, axix = 0) 

但現在什麼,N是一個張量,在運行時決定。

other_tensor = tf.placeholder(dtype=tf.int32, shape=[None, 2]) 
N = tf.shape(other_tensor)[0] # N is None, and it is decided in run time. 

那麼,該怎麼做?

回答

1

您應該使用tf.tile,而不是concat。爲了讓形狀,使用tensor.get_shape下面是一個例子:

import tensorflow as tf 

a = tf.constant([[1, 2], [3, 4]]) 
b = tf.constant([1, 2]) 
c = tf.tile(a, (1, int(a.get_shape()[0]))) 
with tf.Session() as sess: 
    print sess.run(c) 

如果您需要張量有略微不同的形狀,瞭解瓷磚函數的第二個參數,還可以使用tf.reshape

+0

感謝您回答。也許我沒有說清楚。在我的情況下,a = tf.placeholder(dtype = tf.int32,shape = [None,2]) –

+0

@ZehaoShi看着你以前的問題,答案是'不,它根本不清楚'。要回答您的後續問題,請轉至get_shape鏈接,查看動態形狀的情況。 –

+0

我使用tf.shape()獲取動態形狀。 tf.tile()正是我想要的。非常感謝你。爲了方便他人,我現在正在重寫這個問題。 –

相關問題