设置张量默认形状Tensorflow

时间:2018-07-11 04:01:49

标签: python tensorflow graph default tensor

伙计,

如何设置默认张量形状?例如,我尝试了此操作,但收到一个讨厌的错误:

      default_batch_size = tf.placeholder_with_default(1, shape=(), \
          name="default_batch_size")
      X = tf.placeholder(tf.float32, \
          [default_batch_size, n_steps, n_inputs], name="x_input")

错误:

TypeError: Error converting shape to a TensorShape: int() argument must be a string or a number, not 'Tensor'.

1 个答案:

答案 0 :(得分:1)

您使用此占位符是错误的。当placeholder_with_default没有输入任何默认值时,可以将其作为输出默认值。一个例子:

import tensorflow as tf

# output [1., 1.] if nothing is fed
default = tf.ones([1, 2])

# define the placeholder
input_ = tf.placeholder_with_default(default, shape=[None, 2])

# do something
result = 3 * input_

with tf.Session() as sess:

    # print result when feeding something
    print(sess.run(result, feed_dict={input_:[[2., 2.]]}))

    # print result when feeding nothing
    print(sess.run(result))

您应该将此作为控制台输出:

[[6. 6.]]

[[3. 3.]
 [3. 3.]]

定义默认值时,其形状必须与占位符的形状保持一致。