tf.shape() get wrong shape in tensorflow

Question:

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) )

I get Tensor(“Shape:0”, shape=(1,), dtype=int32), why the result of output should not be shape=(100)

Asked By: Nils Cao

||

Answers:

tf.shape(input, name=None) returns a 1-D integer tensor representing the shape of input.

You’re looking for: x.get_shape() that returns the TensorShape of the x variable.

Update: I wrote an article to clarify the dynamic/static shapes in Tensorflow because of this answer: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

Answered By: nessuno

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.

Answered By: Lazar Valkov

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.

Answered By: Salvador Dali

Just a quick example, to make things clear:

a = tf.Variable(tf.zeros(shape=(2, 3, 4)))
print('-'*60)
print("v1", tf.shape(a))
print('-'*60)
print("v2", a.get_shape())
print('-'*60)
with tf.Session() as sess:
    print("v3", sess.run(tf.shape(a)))
print('-'*60)
print("v4",a.shape)

Output will be:

------------------------------------------------------------
v1 Tensor("Shape:0", shape=(3,), dtype=int32)
------------------------------------------------------------
v2 (2, 3, 4)
------------------------------------------------------------
v3 [2 3 4]
------------------------------------------------------------
v4 (2, 3, 4)

Also this should be helpful:
How to understand static shape and dynamic shape in TensorFlow?

Answered By: mrgloom

Simply, use tensor.shape to get the static shape:

In [102]: a = tf.placeholder(tf.float32, [None, 128])

# returns [None, 128]
In [103]: a.shape.as_list()
Out[103]: [None, 128]

Whereas to get the dynamic shape, use tf.shape():

dynamic_shape = tf.shape(a)

You can also get the shape as you’d in NumPy with your_tensor.shape as in the following example.

In [11]: tensr = tf.constant([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])

In [12]: tensr.shape
Out[12]: TensorShape([Dimension(2), Dimension(5)])

In [13]: list(tensr.shape)
Out[13]: [Dimension(2), Dimension(5)]

In [16]: print(tensr.shape)
(2, 5)

Also, this example, for tensors which can be evaluated.

In [33]: tf.shape(tensr).eval().tolist()
Out[33]: [2, 5]
Answered By: kmario23

Tensorflow 2.0 Compatible Answer: Tensorflow 2.x (>= 2.0) compatible answer for nessuno’s solution is shown below:

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

print(x.get_shape())
Answered By: Tensorflow Support