Difference between tf.shape(x) and x.get_shape()

14,789 views
Skip to first unread message

Junhao Wen

unread,
Dec 28, 2016, 9:20:56 AM12/28/16
to Discuss
Hello: TF experts:
I have a stupid question: 
Imagine I have a tensor 
input_shape = [None, 28, 28, 1]
x
= tf.placeholder( tf.float32, input_shape, name='x')

and the result from tf.shape(x) is [4,]

the result from x.get_shape() is [?, 28, 28, 1]

So what are the differences between this two??? I checked out the documentation, but still confused,

Any idea will be appreciated

Hao

Yaroslav Bulatov

unread,
Dec 28, 2016, 11:16:05 AM12/28/16
to Junhao Wen, Discuss

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/afd87f0f-015c-4f71-8351-bcfa5e56b32a%40tensorflow.org.

Junhao Wen

unread,
Dec 28, 2016, 11:32:06 AM12/28/16
to Discuss

Thanks for you reply, I understand the diff right now, actually, my question is that, after we have the result from tf.shape(x) is [4,], i will feed this to other tensors:
corrupt_attr = tf.random_uniform(shape=shape,
                                           minval=0,
                                           maxval=2,
                                           dtype=tf.int32)  ### TODO fix the bug for 4-d tensor corrupt
casted_attr = tf.cast(corrupt_attr, tf.float32)
corrupt_tensor
= tf.mul(x, casted_attr)


and the shape fed for  tf.random_uniform is a 1-D tensor, but with the final result tensor, corrupt_tensor, the result is (?,28,28,?), we lost the forth dimension which should be 1, here is the screen short in Pycharm, this will better explain my question:

Robert Tien

unread,
Apr 3, 2019, 10:02:02 PM4/3/19
to Discuss
TF folks,
tensorflow documentation ought to improve itself. 
the page has nothing about dynamic or static info AT ALL. 
One has to search and figure out this. 
And this info is in FAQ. Very disorganized. 
On the manual page, at least some sort of link should be provided. 

Eugene Brevdo

unread,
Apr 3, 2019, 10:08:29 PM4/3/19
to Robert Tien, Discuss
tf.shape(x) is a tensor which is a vector containing the shape of x.  the length of this vector is 4, because the length of [?, 28, 28, 1] is 4.

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

Khan Engr zubair

unread,
Aug 6, 2019, 5:37:52 AM8/6/19
to Discuss

tf.Tensor.get_shape(), can be used to infer output using the operation that created it, means we can infer it without using sess.run() (running the operation), as hinted by the name, static shape. For example,

c=tf.random_uniform([1,3,1,1])

is a tf.Tensor, and we want to know its shape at any step in the code, before running the graph, so we can use

c.get_shape()

The reason of tf.Tensor.get_shape unable to be dynamic (sess.run()) is because of the output type TensorShape instead of tf.tensor, outputting the TensorShape restricts the usage of sess.run().

sess.run(c.get_shape())

if we do we get an error that TensorShape has an invalid type it must be a Tensor/operation or a string.

On the other hand, the dynamic shape needs the operation to be run via sess.run() to get the shape

sess.run(tf.shape(c))

Output: array([1, 3, 1, 1])

or

sess.run(c).shape

(1, 3, 1, 1) # tuple

Hope it helps to clarify tensorflow concepts.

Reply all
Reply to author
Forward
0 new messages