I have a use case where I need to concatenate a 2D tensor to a 3D tensor in Keras. The dimensions of the 3D tensor are dynamic (for example, the 3D tensor could be the output of an LSTM layer with shape [batch_size, num_timesteps, num_features],
where batch_size
and num_timesteps
are dynamic).
I have used the RepeatVector
to repeat the values of the 2D tensor before the "merge" operation with the 3D tensor.
But, in my case, the "merge" operation throws an error (error details below). I've shared below a representative code for the operations I'm trying to achieve, along with the error.
I suspect the problem here is RepeatVector for a dynamic shape. Or, am I missing something more fundamental? Is there a way I can achieve this correctly?
I'm using Keras v2.1.6 with Tensorflow backend v1.8.0.
import keras
from keras.layers import *
input_3D = Input(shape=(None,100,), dtype='int32', name='input_3D')
input_2D = Input(shape=(100,), dtype='int32', name='input_2D')
input_2D_repeat = RepeatVector(K.shape(input_3D)[1])(input_2D)
merged = merge([input_3D, input_2D_repeat], name="merged", mode='concat')
The above code throws below error for the "merge" operation:
ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, None, 100), (None, , 100)]
I can see that the second dimension in input_3D
is None
, but the second dimension in input_2D_repeat
is tf.Tensor 'strided_slice:0' shape=() dtype=int32
.
How can I fix this?