是否可以使用 tf.keras.models.clone_model 来改变数据输入的形状?

Is it possible to use tf.keras.models.clone_model to change shape of data input?

我想使用 tf.keras 的方法 clone_model 并更改由函数 API 创建的 tensorflow/keras 模型的输入形状。因此,我尝试使用参数 input_tensor 来改变形状。但是,它似乎没有使用提供的 input_tensors 并且名称和形状与原始模型保持一致。参数 input_tensors 的用途是什么?

代码:

import tensorflow as tf
from tensorflow.keras import layers

inputs_small = layers.Input((64, 64, 3), name="small")

outputs = layers.Conv2D(32, 1)(inputs_small)
model_small = tf.keras.models.Model(inputs=inputs_small, outputs=outputs)


inputs_large = layers.Input((128, 128, 3), name="large")
model_large = tf.keras.models.clone_model(model_small, input_tensors=inputs_large)

model_large.summary()

结果:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
small (InputLayer)           [(None, 64, 64, 3)]       0         
_________________________________________________________________
conv2d (Conv2D)              (None, 64, 64, 32)        128       
=================================================================

但我喜欢:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
large (InputLayer)           [(None, 128, 128, 3)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 128, 128, 32)      128       
=================================================================

我使用 TensorFlow 2.4.1。我简化了我的问题。在我的代码中,我还使用 clone_model 的参数 clone_function 来替换图层。

我进一步调查并发现了一个 Keras 错误: https://github.com/keras-team/keras/issues/14937