1

I want to combine the four multiple inputs into the single keras model, but it requires inputs with matching shapes:

import tensorflow as tf

input1 = tf.keras.layers.Input(shape=(28, 28, 1))
input2 = tf.keras.layers.Input(shape=(28, 28, 3))
input3 = tf.keras.layers.Input(shape=(128,))
input4 = tf.keras.layers.Input(shape=(1,))

x = tf.keras.layers.Concatenate(axis=1)([input1, input2, input3, input4])

x = tf.keras.layers.Dense(2)(x)

model = tf.keras.models.Model(inputs=[input1, input2, input3, input4], outputs=x)

Here is the output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_3447/2584043467.py in <cell line: 6>()
      4 input4 = tf.keras.layers.Input(shape=(1,))
      5 
----> 6 x = tf.keras.layers.Concatenate(axis=1)([input1, input2, input3, input4])
      7 
      8 x = tf.keras.layers.Dense(2)(x)

/usr/local/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.8/site-packages/keras/layers/merging/concatenate.py in build(self, input_shape)
    112       ranks = set(len(shape) for shape in shape_set)
    113       if len(ranks) != 1:
--> 114         raise ValueError(err_msg)
    115       # Get the only rank for the set.
    116       (rank,) = ranks

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(None, 28, 28, 1), (None, 28, 28, 3), (None, 128), (None, 1)]

How to combine the above inputs in the single model?

1 Answer 1

1

The error message is actually telling you what the problem is. All dimensions except the one you want to concatenate have to be the same and they are not. You can try something like this:

import tensorflow as tf

input1 = tf.keras.layers.Input(shape=(28, 28, 1))
input2 = tf.keras.layers.Input(shape=(28, 28, 3))
input3 = tf.keras.layers.Input(shape=(128,))
input4 = tf.keras.layers.Input(shape=(1,))

input1 = tf.keras.layers.Flatten()(input1)
input2 = tf.keras.layers.Flatten()(input2)
x = tf.keras.layers.Concatenate(axis=-1)([input1, input2, input3, input4])

x = tf.keras.layers.Dense(2)(x)

model = tf.keras.models.Model(inputs=[input1, input2, input3, input4], outputs=x)
Sign up to request clarification or add additional context in comments.

9 Comments

I want to use four 4 inputs with different shapes, and finally split the training into 4 diff output shapes (the 4 output shape is same as input). Do I need to write a custom layer for this 4 outputs?
@stackbiz I am a bit confused.. did you not just want to concatenate all inputs into one tensor? Or why are you using the Concatenate layer? Your question here does not seem to have anything to do with your original question?
My purpose is to test the multiple inputs with different shapes, and finally multiple outputs with different shapes. The post example is for testing how to add multiple inputs to the same model. The original Concatenate layer is just an example as I don't know how to do this.
Ok. I think your original question was answered and I think your next question can only be answered if you provide more details (in a new question) and show what you have tried already.
I think I can try to create new layer based on your answer by modifying the Concatenate layer. Thanks for your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.