【问题标题】:How to get Tensorflow tensor dimensions (shape) as int values?如何将Tensorflow张量尺寸(形状)作为int值?
【发布时间】:2017-04-01 16:03:39
【问题描述】:

假设我有一个 Tensorflow 张量。如何将张量的尺寸(形状)作为整数值?我知道有两种方法,tensor.get_shape()tf.shape(tensor),但我无法将形状值作为整数int32 值。

例如,下面我创建了一个二维张量,我需要将行数和列数设为int32,这样我就可以调用reshape() 来创建形状为(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.

【问题讨论】:

    标签: python tensorflow machine-learning artificial-intelligence


    【解决方案1】:

    要将形状作为整数列表获取,请执行tensor.get_shape().as_list()

    要完成您的tf.shape() 通话,请尝试tensor2 = tf.reshape(tensor, tf.TensorShape([num_rows*num_cols, 1]))。或者直接tensor2 = tf.reshape(tensor, tf.TensorShape([-1, 1])) 可以推断出它的第一个维度。

    【讨论】:

    • 谢谢,这让我可以调用并完成tf.reshape(),但我真的很想将num_rowsnum_cols 作为整数用于其他操作。
    • 试试tensor.get_shape().as_list()
    • 是的,as_list() 有效。请将其添加到您的答案中,我会接受。
    • 为了完整起见,此代码有效:num_rows, num_cols = x.get_shape().as_list()
    • 不错!我正在使用 python int() 来转换 x.get_shape() 的结果。即 num_rows=int(x.get_shape()[1]), num_cols=int(x.get_shape()[2]) 等。是的,绕过那个讨厌的错误有点笨拙,但它奏效了。感谢您启发我找到更好的方法:-)
    【解决方案2】:

    解决这个问题的另一种方法是这样的:

    tensor_shape[0].value
    

    这将返回 Dimension 对象的 int 值。

    【讨论】:

      【解决方案3】:

      对于二维张量,您可以使用以下代码以 int32 形式获取行数和列数:

      rows, columns = map(lambda i: i.value, tensor.get_shape())
      

      【讨论】:

      • 非常不雅。这对已经提供的答案有何影响?
      【解决方案4】:

      在更高版本(使用 TensorFlow 1.14 测试)中,有一种更类似于 numpy 的方式来获取张量的形状。您可以使用tensor.shape 来获取张量的形状。

      tensor_shape = tensor.shape
      print(tensor_shape)
      

      【讨论】:

        【解决方案5】:

        2.0兼容答案:在Tensorflow 2.x (2.1)中,可以得到张量的维度(形状)为整数值,如下代码所示: p>

        方法一(使用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]
        

        方法二(使用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]
        

        【讨论】:

        【解决方案6】:

        另一个简单的解决方案是使用map(),如下所示:

        tensor_shape = map(int, my_tensor.shape)
        

        这会将所有Dimension 对象转换为int

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-18
          • 2018-03-31
          • 1970-01-01
          • 2017-01-20
          • 2019-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多