[英]How to get Tensorflow tensor dimensions (shape) as int values?
假設我有一個 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.
要將形狀作為整數列表,請執行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]))
,其中可以推斷出其第一維。
解決這個問題的另一種方法是這樣的:
tensor_shape[0].value
這將返回 Dimension 對象的 int 值。
2.0 兼容答案:在Tensorflow 2.x (2.1)
,您可以獲得張量的尺寸(形狀)作為整數值,如下面的代碼所示:
方法 1(使用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]
方法 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]
對於二維張量,您可以使用以下代碼將行數和列數設為 int32:
rows, columns = map(lambda i: i.value, tensor.get_shape())
另一個簡單的解決方案是使用map()
如下:
tensor_shape = map(int, my_tensor.shape)
這會將所有Dimension
對象轉換為int
在更高版本中(使用 TensorFlow 1.14 測試),有一種更類似 numpy 的方式來獲取張量的形狀。 您可以使用tensor.shape
來獲取張量的形狀。
tensor_shape = tensor.shape
print(tensor_shape)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.