Python 如何将 Tensorflow 张量维度(形状)作为 int 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40666316/
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
How to get Tensorflow tensor dimensions (shape) as int values?
提问by stackoverflowuser2010
Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I know there are two methods, tensor.get_shape()
and tf.shape(tensor)
, but I can't get the shape values as integer int32
values.
假设我有一个 Tensorflow 张量。如何将张量的尺寸(形状)作为整数值?我知道有两种方法tensor.get_shape()
和tf.shape(tensor)
,但我无法将形状值作为整int32
数值获取。
For example, below I've created a 2-D tensor, and I need to get the number of rows and columns as int32
so that I can call reshape()
to create a tensor of shape (num_rows * num_cols, 1)
. However, the method tensor.get_shape()
returns values as Dimension
type, not int32
.
例如,下面我创建了一个二维张量,我需要获取行数和列数,int32
以便我可以调用reshape()
来创建一个 shape 的张量(num_rows * num_cols, 1)
。但是,该方法tensor.get_shape()
将值作为Dimension
类型返回,而不是int32
。
import tensorflow as tf
import numpy as np
sess = tf.Session()
tensor = tf.convert_to_tensor(np.array([[1001,1002,1003],[3,4,5]]), dtype=tf.float32)
sess.run(tensor)
# array([[ 1001., 1002., 1003.],
# [ 3., 4., 5.]], dtype=float32)
tensor_shape = tensor.get_shape()
tensor_shape
# TensorShape([Dimension(2), Dimension(3)])
print tensor_shape
# (2, 3)
num_rows = tensor_shape[0] # ???
num_cols = tensor_shape[1] # ???
tensor2 = tf.reshape(tensor, (num_rows*num_cols, 1))
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1750, in reshape
# name=name)
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 454, in apply_op
# as_ref=input_arg.is_ref)
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 621, in convert_to_tensor
# ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
# return constant(v, dtype=dtype, name=name)
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
# tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 353, in make_tensor_proto
# _AssertCompatible(values, dtype)
# File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 290, in _AssertCompatible
# (dtype.name, repr(mismatch), type(mismatch).__name__))
# TypeError: Expected int32, got Dimension(6) of type 'Dimension' instead.
回答by yuefengz
To get the shape as a list of ints, do tensor.get_shape().as_list()
.
要将形状作为整数列表,请执行tensor.get_shape().as_list()
.
To complete your tf.shape()
call, try tensor2 = tf.reshape(tensor, tf.TensorShape([num_rows*num_cols, 1]))
. Or you can directly do tensor2 = tf.reshape(tensor, tf.TensorShape([-1, 1]))
where its first dimension can be inferred.
要完成tf.shape()
通话,请尝试tensor2 = tf.reshape(tensor, tf.TensorShape([num_rows*num_cols, 1]))
。或者你可以直接tensor2 = tf.reshape(tensor, tf.TensorShape([-1, 1]))
在可以推断出它的第一维的地方做。
回答by tijmen Verhulsdonck
Another way to solve this is like this:
解决这个问题的另一种方法是这样的:
tensor_shape[0].value
This will return the int value of the Dimension object.
这将返回 Dimension 对象的 int 值。
回答by Anna
for a 2-D tensor, you can get the number of rows and columns as int32 using the following code:
对于二维张量,您可以使用以下代码将行数和列数设为 int32:
rows, columns = map(lambda i: i.value, tensor.get_shape())
回答by Tensorflow Support
2.0 Compatible Answer: In Tensorflow 2.x (2.1)
, you can get the dimensions (shape) of the tensor as integer values, as shown in the Code below:
2.0 兼容答案:在 中Tensorflow 2.x (2.1)
,您可以获得张量的尺寸(形状)作为整数值,如下面的代码所示:
Method 1 (using tf.shape
):
方法一(使用tf.shape
):
import tensorflow as tf
c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
Shape = c.shape.as_list()
print(Shape) # [2,3]
Method 2 (using tf.get_shape()
):
方法2(使用tf.get_shape()
):
import tensorflow as tf
c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
Shape = c.get_shape().as_list()
print(Shape) # [2,3]
回答by thushv89
In later versions (tested with TensorFlow 1.14) there's a more numpy-like way to get the shape of a tensor. You can use tensor.shape
to get the shape of the tensor.
在更高版本(使用 TensorFlow 1.14 测试)中,有一种更类似 numpy 的方式来获取张量的形状。您可以使用tensor.shape
来获取张量的形状。
tensor_shape = tensor.shape
print(tensor_shape)