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

"""
Defining two sets of inputs
Input_A: input from the features
Input_B: input from images
my train_features has (792,192) shape
my train_images has (792,28,28) shape
"""

input_A = Input(shape=(192,), name="wide_input")
input_B = Input(shape=(28,28), name="deep_input")
#connecting the first layer with the input from the features
x= Dense(600, activation='relu')(input_A)
x=Dense(100, activation='relu')(x)
x=Dense(28, activation='relu')(x)
x=keras.Model(inputs=input_A, outputs=x)

#connecting the second layer using the input from the images
         
y= Dense(28, activation='relu')(input_B)
y=Dense(7, activation='relu')(y)
y=Dense(28, activation='relu')(y)
y=keras.Model(inputs=input_B, outputs=y)

concat =  concatenate([x.output, y.output])

output = Dense(1, name="output")(concat)

model = keras.Model(inputs=[x.input, y.input], outputs=[output])

it keeps throwing this error: ValueError: A Concatenate layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(None, 28), (None, 28, 28)]



Solution 1:[1]

I could able to reproduce the issue

from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, Input
from tensorflow import keras
input_A = Input(shape=(192,), name="wide_input")
input_B = Input(shape=(28,28), name="deep_input")
#connecting the first layer with the input from the features
x= Dense(600, activation='relu')(input_A)
x=Dense(100, activation='relu')(x)
x=Dense(28, activation='relu')(x)
x=keras.Model(inputs=input_A, outputs=x)

#connecting the second layer using the input from the images
         
y= Dense(28, activation='relu')(input_B)
y=Dense(7, activation='relu')(y)
y=Dense(28, activation='relu')(y)
y=keras.Model(inputs=input_B, outputs=y)

concat =  concatenate([x.output, y.output])

Output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-85b2c2988a05> in <module>()
     18 y=keras.Model(inputs=input_B, outputs=y)
     19 
---> 20 concat =  concatenate([x.output, y.output])

2 frames
/usr/local/lib/python3.7/dist-packages/keras/layers/merge.py in build(self, input_shape)
    517       ranks = set(len(shape) for shape in shape_set)
    518       if len(ranks) != 1:
--> 519         raise ValueError(err_msg)
    520       # Get the only rank for the set.
    521       (rank,) = ranks

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

As error suggests, A Concatenate layer requires inputs with matching shapes but in this case shape is different shape=(192,) and shape=(28,28). If you make shape=(28,), error disappears.

Working sample code

from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, Input
from tensorflow import keras
input_A = Input(shape=(192,), name="wide_input")
input_B = Input(shape=(28,), name="deep_input")
#connecting the first layer with the input from the features
x= Dense(600, activation='relu')(input_A)
x=Dense(100, activation='relu')(x)
x=Dense(28, activation='relu')(x)
x=keras.Model(inputs=input_A, outputs=x)

#connecting the second layer using the input from the images
         
y= Dense(28, activation='relu')(input_B)
y=Dense(7, activation='relu')(y)
y=Dense(28, activation='relu')(y)
y=keras.Model(inputs=input_B, outputs=y)

concat =  concatenate([x.output, y.output])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer