Set Tensor Default Shape Tensorflow


How may I set a default tensor shape? For example, I tried this, but I get a nasty error:

      default_batch_size = tf.placeholder_with_default(1, shape=(), \
      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 answer

  • answered 2018-07-11 08:11 DocDriven

    Your use of this placeholder is wrong. Consider placeholder_with_default as outputting the default value when nothing is fed to it. An example:

    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(, feed_dict={input_:[[2., 2.]]}))
        # print result when feeding nothing

    You should get this as console output:

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

    When defining a default, its shape has to be consistent with the shape of your placeholder.