' Concatenate '层要求输入具有匹配的形状,除了连接轴.收到:input_shape=[(None, 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])

它一直抛出这个错误:ValueError: Concatenate层需要具有匹配形状的输入,除了连接轴。收到:input_shape=[(None, 28), (None, 28,28)]

我可以重现这个问题

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])

---------------------------------------------------------------------------
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)]

如误差所示,A级联层需要形状匹配的输入,但在这种情况下,形状是不同的shape=(192,)shape=(28,28)。如果您使shape=(28,),错误消失。

工作样例代码

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])

相关内容

  • 没有找到相关文章

最新更新