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)
5 个解决方案
#1
86
tf.shape(input, name=None) returns a 1-D integer tensor representing the shape of input.
tf.shape(input,name = None)返回表示输入形状的1-D整数张量。
You're looking for: x.get_shape()
that returns the TensorShape
of the x
variable.
您正在寻找:x.get_shape(),它返回x变量的TensorShape。
#2
9
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)创建一个op并返回一个对象,该对象代表构造的op的输出,这是您当前正在打印的内容。要获得形状,请在会话中运行该操作:
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.
信用:看了上面的答案后,我在Tensorflow中看到了tf.rank函数的答案,我发现它更有帮助,我在这里尝试过改写它。
#3
4
Just a quick example based on @Salvador Dali answer.
只是一个基于@Salvador Dali答案的简单例子。
a = tf.Variable(tf.zeros(shape=(2, 3, 4)))
print("a v1", tf.shape(a))
print("a v2", a.get_shape())
with tf.Session() as sess:
print("a v3", sess.run(tf.shape(a)))
Output will be:
输出将是:
a v1 Tensor("Shape:0", shape=(3,), dtype=int32)
a v2 (2, 3, 4)
a v3 [2 3 4]
#4
2
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_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 evaluatingtf.shape(t)
.在TensorFlow中,张量具有静态(推断)形状和动态(真实)形状。可以使用tf.Tensor.get_shape方法读取静态形状:此形状是从用于创建张量的操作推断出来的,并且可能部分完成。如果静态形状未完全定义,则可以通过评估tf.shape(t)来确定Tensor 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()提取静态形状,这可以在任何地方计算。
#5
1
Simply, use tensor.shape
to 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.shape
as in the following example.
您也可以使用your_tensor.shape获取NumPy中的形状,如下例所示。
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]
#1
86
tf.shape(input, name=None) returns a 1-D integer tensor representing the shape of input.
tf.shape(input,name = None)返回表示输入形状的1-D整数张量。
You're looking for: x.get_shape()
that returns the TensorShape
of the x
variable.
您正在寻找:x.get_shape(),它返回x变量的TensorShape。
#2
9
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)创建一个op并返回一个对象,该对象代表构造的op的输出,这是您当前正在打印的内容。要获得形状,请在会话中运行该操作:
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.
信用:看了上面的答案后,我在Tensorflow中看到了tf.rank函数的答案,我发现它更有帮助,我在这里尝试过改写它。
#3
4
Just a quick example based on @Salvador Dali answer.
只是一个基于@Salvador Dali答案的简单例子。
a = tf.Variable(tf.zeros(shape=(2, 3, 4)))
print("a v1", tf.shape(a))
print("a v2", a.get_shape())
with tf.Session() as sess:
print("a v3", sess.run(tf.shape(a)))
Output will be:
输出将是:
a v1 Tensor("Shape:0", shape=(3,), dtype=int32)
a v2 (2, 3, 4)
a v3 [2 3 4]
#4
2
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_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 evaluatingtf.shape(t)
.在TensorFlow中,张量具有静态(推断)形状和动态(真实)形状。可以使用tf.Tensor.get_shape方法读取静态形状:此形状是从用于创建张量的操作推断出来的,并且可能部分完成。如果静态形状未完全定义,则可以通过评估tf.shape(t)来确定Tensor 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()提取静态形状,这可以在任何地方计算。
#5
1
Simply, use tensor.shape
to 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.shape
as in the following example.
您也可以使用your_tensor.shape获取NumPy中的形状,如下例所示。
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]