Python tf.shape() 在张量流中得到错误的形状

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37085430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:48:15  来源:igfitidea点击:

tf.shape() get wrong shape in tensorflow

pythonpython-3.xtensorflowtensor

提问by Nils Cao

I define a tensor like this:

我定义了一个这样的张量:

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

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

But when I try to print shape of tensor :

但是当我尝试打印张量的形状时:

print( tf.shape(x) )

print( tf.shape(x) )

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

我得到Tensor("Shape:0", shape=(1,), dtype=int32),为什么输出的结果不应该是 shape=(100)

回答by nessuno

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

tf.shape(input, name=None)返回一个表示输入形状的一维整数张量。

You're looking for: x.get_shape()that returns the TensorShapeof the xvariable.

你要找的:x.get_shape()它返回TensorShape的的x变量。

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/

更新:由于这个答案,我写了一篇文章来阐明 Tensorflow 中的动态/静态形状:https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

回答by Lazar Valkov

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:

tf.shape(x) 创建一个操作并返回一个代表构造操作的输出的对象,这就是您当前正在打印的内容。要获取形状,请在会话中运行操作:

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 Tensorflowwhich I found more helpful and I have tried rephrasing it here.

信用:在查看上述答案后,我在 Tensorflow 中看到了tf.rank 函数的答案,我发现它更有帮助,我尝试在此处重新措辞

回答by mrgloom

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?

这也应该有帮助: 如何理解 TensorFlow 中的静态形状和动态形状?

回答by Salvador Dali

Similar question is nicely explained in TF FAQ:

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_shapemethod: 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).

在 TensorFlow 中,张量具有静态(推断)形状和动态(真实)形状。可以使用该tf.Tensor.get_shape方法读取静态形状 :该形状是从用于创建张量的操作中推断出来的,并且可能是部分完整的。如果静态形状没有完全定义,张量 t 的动态形状可以通过评估 来确定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:

因此tf.shape()返回一个张量,其大小始终为shape=(N,),并且可以在会话中计算:

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.

另一方面,您可以使用提取静态形状x.get_shape().as_list(),这可以在任何地方计算。

回答by kmario23

Simply, use tensor.shapeto get the static shape:

简单地说,用于tensor.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():

而要获得动态形状,请使用tf.shape()

dynamic_shape = tf.shape(a)


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

您还可以像在 NumPy 中your_tensor.shape一样获取形状,如下例所示。

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.

同样,这个例子,对于可以使用的张量eval

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

回答by Tensorflow Support

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

Tensorflow 2.0 兼容答案Tensorflow 2.x (>= 2.0)nessuno 解决方案的兼容答案如下所示:

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

print(x.get_shape())