## tf.shape() get wrong shape in tensorflow

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)

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/

**tf.shape,** 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� tf.shape() get wrong shape in tensorflow How to count total number of trainable parameters in a Tensorflow model? Probably the official documentation is not so clear about this aspect of the framework: I hope this post help to clarify this aspect.

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.

**[SOLVED] tf.shape() get wrong shape in tensorflow,** This article will guide you through the concept of tensor's shape in tf.shape() get wrong shape in tensorflow � How to count total number of� As discussed with @strin IRL, the confusion was that in tf.slice(a, b), b specifies size and -1 means "take all elements", whereas in tf.striced_slice(a, b), b specifies end and -1 means "take elements up to the last one, not including the last one".

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?

**Understanding Tensorflow's tensors shape: static and dynamic – P ,** tf.shape output is wrong when net input shape is changed during import #21185 import tensorflow as tf batch_size = 128 x = tf.placeholder(tf.float32, the shapes of tensors that have been explicitly set via set_shape(). If t has shape (?,), a call to t.set_shape((478, 717, 3)) will fail, because TensorFlow already knows that t is a vector, so it cannot have shape (478, 717, 3). If you want to make a new Tensor with that shape from the contents of t , you can use reshaped_t = tf.reshape(t, (478, 717, 3)) .

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.

**tf.shape output is wrong when net input shape is changed during ,** x.shape and x.get_shape() can be used separately to get the shape of tensor x when it is .com/questions/37085430/tf-shape-get-wrong-shape-in-tensorflow� Pre-trained models and datasets built by Google and the community

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 `eval`

uated.

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

**The difference between tf.shape, x.shape, x.get_shape ,** 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).

Returns the shape of tensor or variable as a tuple of int or None entries. Install Learn Introduction TensorFlow Extended for end-to-end ML components

tensorflow / tensorflow. Watch 8.4k Star 144k Fork 81.2k Code. Issues 3,410. I should have been using tf.shape() instead of foo.get_shape().as_list(). Thanks

Code to get the shape as a list of ints. tensor.get_shape().as_list() To complete your. tf.shape() function is incomplete, Add the following code to complete: tensor2 = tf.reshape(tensor, tf.TensorShape([num_rows*num_cols, 1])) Or . tensor2 = tf.reshape(tensor, tf.TensorShape([-1, 1])) Hope this answer helps. If you want to learn more about TensorFlow then enroll for the Machine Learning Course.

##### Comments

- I found this answer very useful to analyze the shape of a tensor, despite it is not the accepted one.
`x.get_shape().as_list()`

is an often used form to convert the shape to a standard python list. Added here for reference.- What is
`shape=(N,)`

represent? Can you show example when static shape and dinamic shape differs? - @mrgloom shape=(n,) represents a vector of size n. It is not easy to show such an example because you need to confuse TF enough to lose control of the shape