Tensor with unspecified dimension in tensorflow

前端 未结 3 785
广开言路
广开言路 2020-12-14 02:03

I\'m playing around with tensorflow and ran into a problem with the following code:

def _init_parameters(self, input_data, labels):

    # the input shape is         


        
3条回答
  •  生来不讨喜
    2020-12-14 02:28

    Similar question is nicely explained in TF FAQ:

    In TensorFlow, a tensor has both a static (inferred) shape and a dynamic (true) shape. The static shape can be read using the tf.Tensor.get_shape method: this shape is inferred from the operations that were used to create the tensor, and may be partially complete. If the static shape is not fully defined, the dynamic shape of a Tensor t can be determined by evaluating tf.shape(t).

    So tf.shape() returns you a tensor, will always have a size of shape=(N,), and can be calculated in a session:

    a = tf.Variable(tf.zeros(shape=(2, 3, 4)))
    with tf.Session() as sess:
        print sess.run(tf.shape(a))
    

    On the other hand you can extract the static shape by using x.get_shape().as_list() and this can be calculated anywhere.

提交回复
热议问题