i am working on tensorflow 2.x. I want to accept an Integer as one of the Multi-Inputs, and in some stage of the model, i need to do tf.reshape with shape set by the Integer of Input. It is more like:
...
x_h = Input((shape=(), dtype=tf.int32)
...
x=...
x = tf.reshape(x, [x_h, x_h*2, x_h*2, 64/x_h])
...
return Model(inputs=[..., x_h], outputs=x)
All of the model runs on tensorflowlite. Tensorflow reports me that shape can not be set by tensor. I also tried tensor.numpy, still got failure.
Below is my test code:
x_in = Input(shape=(None, None, 3))
x_h = Input(shape=(), dtype=tf.int32)
x = Conv2D(32, 3, padding='same')(x_in)
x = Reshape((x_h*2, x_h, 16))(x)
model = Model(inputs=[x_in, x_h], outputs=x)
model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
x = np.random.random((3, 2, 2, 3))
x_h = 2
y = np.random.random((3, 4, 2, 16))
model.fit([x, x_h], y)
And,tensorflow reports:
Traceback (most recent call last):
File "test.py", line 12, in <module>
x = Reshape((x_h*2, x_h, 16))(x)
File "/home/walter/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/home/walter/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer "tf.reshape" (type TFOpLambda).
Tried to convert 'shape' to a tensor and failed. Error: Shapes must be equal rank, but are 1 and 0
From merging shape 2 with other shapes. for '{{node tf.reshape/Reshape/packed}} = Pack[N=4, T=DT_INT32, axis=0](tf.reshape/Reshape/shape/reshape/strided_slice, Placeholder, Placeholder_1, tf.reshape/Reshape/packed/3)' with input shapes: [], [?], [?], [].
Call arguments received:
• tensor=tf.Tensor(shape=(None, None, None, 32), dtype=float32)
• shape=('tf.Tensor(shape=(), dtype=int32)', 'tf.Tensor(shape=(None,), dtype=int32)', 'tf.Tensor(shape=(None,), dtype=int32)', '16')
• name=None
Could anyone give me a hand? Appreciate so much!
Best Regards
Walter