我使用TensorFlow2.0,并且我有一个在命令式API中定义的模型。在call方法中,我使用了类似这样的东西:
b, h, w, c = images.shape
k_h, k_w = kernels.shape[2], kernels.shape[3]
images = tf.transpose(images, [1, 2, 0, 3]) # (h, w, b, c)
new_shape = tf.TensorShape([1, h, w, b * c])
images = tf.reshape(images, new_shape)
当我用定制循环训练我的模型时--没问题。但是我想把我的模型移植到SavedModel格式。我使用以下函数:
tf.keras.experimental.export_saved_model(
model, file_path,
serving_only=True,
input_signature=[
tf.TensorSpec(shape=[None, None, None, 3], dtype=tf.float32),
]
)
我得到了一个错误:
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
此外,即使我指定了shape=1,None,None,3,我也不能这样做,因为我得到了:
ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor: (1, None, None, 3)
这意味着我根本不能重塑。但我需要它。我该怎么做呢?
发布于 2019-06-26 04:55:28
在图形模式下运行时,请使用tf.shape。在图形模式下运行时,tf.Tensor.shape无法自动推断形状。以下是进行了必要更改的代码。
image_shape = tf.shape(images)
kernel_shape = tf.shape(kernels)
b, h, w, c = image_shape[0], image_shape[1], image_shape[2], image_shape[3],
k_h, k_w = kernel_shape[2], kernel_shape[3]
images = tf.transpose(images, [1, 2, 0, 3]) # (h, w, b, c)
new_shape = tf.TensorShape([1, h, w, b * c])
images = tf.reshape(images, new_shape)
https://stackoverflow.com/questions/56756440
复制相似问题