tf.shape() get wrong shape in tensorflow

后端 未结 6 582
南笙
南笙 2020-12-04 10:21

I define a tensor like this:

x = tf.get_variable(\"x\", [100])

But when I try to print shape of tensor :

print( tf.shape(x) )

6条回答
  •  广开言路
    2020-12-04 11:10

    Clarification:

    tf.shape(x) creates an op and returns an object which stands for the output of the constructed op, which is what you are printing currently. To get the shape, run the operation in a session:

    matA = tf.constant([[7, 8], [9, 10]])
    shapeOp = tf.shape(matA) 
    print(shapeOp) #Tensor("Shape:0", shape=(2,), dtype=int32)
    with tf.Session() as sess:
       print(sess.run(shapeOp)) #[2 2]
    

    credit: After looking at the above answer, I saw the answer to tf.rank function in Tensorflow which I found more helpful and I have tried rephrasing it here.

提交回复
热议问题