首页 文章

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

提问于
浏览
0

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

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

    您需要使用 a.get_shape() 来获取会话的结果 outside (它将给出 inferred 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])
    

相关问题