将张量形状提取为具有未知第一维的整数
Extract tensor shape as integers with unknown first dimension
我有一个函数,它将张量 X 作为输入。我想从第一维未知的张量中提取第二维、第三维和第四维大小:
我写了下面的代码,但它不起作用:
x = Input(shape=(128, 128, 1))
def function1(x):
sz = tf.shape(x)
row = int(sz[1].numpy())
col = int(sz[2].numpy())
我收到以下错误:
AttributeError: 'Tensor' object has no attribute 'numpy'
我也试过:
sz = tf.TensorShape(x).as_list()
但错误是:
TypeError: Cannot iterate over a tensor with unknown first dimension.
我正在使用 Tensorflow 2.1
您可以使用 get_shape()
.
print(x.get_shape().as_list())
您可以拨打 x.shape
:
x = tf.keras.Input(shape=(128, 128, 1))
n, row, col, channels = x.shape
这就是x.shape
returns:
TensorShape([None, 128, 128, 1])
或者像这样:
row, col, channels = x.shape[1:]
我有一个函数,它将张量 X 作为输入。我想从第一维未知的张量中提取第二维、第三维和第四维大小:
我写了下面的代码,但它不起作用:
x = Input(shape=(128, 128, 1))
def function1(x):
sz = tf.shape(x)
row = int(sz[1].numpy())
col = int(sz[2].numpy())
我收到以下错误:
AttributeError: 'Tensor' object has no attribute 'numpy'
我也试过:
sz = tf.TensorShape(x).as_list()
但错误是:
TypeError: Cannot iterate over a tensor with unknown first dimension.
我正在使用 Tensorflow 2.1
您可以使用 get_shape()
.
print(x.get_shape().as_list())
您可以拨打 x.shape
:
x = tf.keras.Input(shape=(128, 128, 1))
n, row, col, channels = x.shape
这就是x.shape
returns:
TensorShape([None, 128, 128, 1])
或者像这样:
row, col, channels = x.shape[1:]