有没有办法使用张量作为tf.reshape或tf.constant等函数的输入

时间:2016-06-19 18:37:43

标签: tensorflow

我试图使用张量作为另一个张量流函数的形状输入。有没有办法让这个工作?

实施例

import tensorflow as tf

a = tf.constant([1,2,3,4,5])

b = tf.size(a)

c = tf.constant([b,1])

d = tf.reshape(a, [b,1])

sess = tf.Session()

print sess.run([b,c])

上述两种方法都不起作用,因为tensorflow将输入维度解释为张量列表并给出错误,其底线是:

TypeError: List of Tensors when single Tensor expected

我假设一个解决方法是打开一个会话,评估' b'得到一个numpy浮点数,并将其转换为整数,但希望尽可能避免这种情况。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要使用a.get_shape()来获得会话 的结果(它会提供推断形状):

a = tf.constant([1,2,3,4,5])
b = a. get_shape().as_list()[0]
d = tf.reshape(a, [b, 1])

您还可以将-1tf.reshape一起使用,它会使总大小保持不变。

d = tf.reshape(a, [-1, 1])