2017-05-24 45 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()正是我想要的。非常感谢你。为了方便他人,我现在正在重写这个问题。 –

相关问题