无法在Keras中添加两层

时间:2018-09-10 14:58:59

标签: machine-learning keras

我正在尝试在keras中添加两个简单的图层。下面是我的代码。

inputLayer = Input(shape=(28,28,1)) 
conv1 = Conv2D(32,(3,3),strides=1, padding="same", activation='relu') (inputLayer)

conv1 = Conv2D(32,(3,3),strides=1, padding="same", activation='relu') (conv1)
conv1 = Conv2D(49,(25,25),strides=1, padding="valid", activation='relu') (conv1)

# Now I would like to add INputLayer and the last conv layer.

flat = Flatten(name='FlattenLayerName')(conv1)
flat1 = Flatten (name = 'flat1')(inputLayer)


m = keras.layers.add([flat, flat1])

flat = Flatten(name='FlattenLayerName') (m)

dense1 = Dense(10,activation='sigmoid') (flat)
model = Model(input=inputLayer, output=dense1)
print model.summary()

我遇到打击错误。

Traceback (most recent call last):
  File "myresNet.py", line 37, in <module>
    flat = Flatten(name='FlattenLayerName') (m)
  File "/home/jils/anaconda2/envs/testML/lib/python2.7/site-packages/keras/engine/base_layer.py", line 414, in __call__
    self.assert_input_compatibility(inputs)
  File "/home/jils/anaconda2/envs/testML/lib/python2.7/site-packages/keras/engine/base_layer.py", line 327, in assert_input_compatibility
    str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer FlattenLayerName: expected min_ndim=3, found ndim=2

1 个答案:

答案 0 :(得分:0)

正如评论中提到的那样,问题不在于添加两个图层时,而在于尝试使已平坦的结果图层变平时。

删除此行:

flat = Flatten(name='FlattenLayerName') (m)