65

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)

kmario23
  • 57,311
  • 13
  • 161
  • 150
Nils Cao
  • 1,409
  • 2
  • 15
  • 23
  • I found [this](https://stackoverflow.com/a/41771268/1358091) answer very useful to analyze the shape of a tensor, despite it is not the accepted one. – Marco Carletti Jun 15 '17 at 09:02

6 Answers6

132

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/

nessuno
  • 26,493
  • 5
  • 83
  • 74
  • 48
    `x.get_shape().as_list()` is an often used form to convert the shape to a standard python list. Added here for reference. – David Parks Mar 08 '17 at 18:55
13

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.

Community
  • 1
  • 1
Lazar Valkov
  • 143
  • 1
  • 6
12

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?

mrgloom
  • 20,061
  • 36
  • 171
  • 301
5

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.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • What is `shape=(N,)` represent? Can you show example when static shape and dinamic shape differs? – mrgloom Aug 31 '17 at 14:44
  • @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 – Salvador Dali Aug 31 '17 at 17:16
2

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]
kmario23
  • 57,311
  • 13
  • 161
  • 150
0

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