VGG Keras中的尺寸不匹配

时间:2017-06-15 07:51:20

标签: python tensorflow deep-learning keras

我想用Keras创建VGG模型。

但显示以下错误,

预计lstm_input_2有4个维度,但是有阵列形状(60000,10)

我创建了以下顺序模型。

model = Sequential()
model.add(Conv2D(16, kernel_size=(3, 3),
                 padding='same',
                 input_shape=input_shape)) 
model.add(Activation('relu'))
model.add(Conv2D(16, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dense(50, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Dropout(0.5))
model.add(Activation('softmax'))

请告诉我为什么会出现这个错误。

1 个答案:

答案 0 :(得分:0)

You just need to add a Flatten layer like so:

…
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten()) # <-- this layer is missing in your code

model.add(Dense(50, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Dropout(0.5))
model.add(Activation('softmax'))
…

This transforms your last 2d layer (MaxPooling2D) to a 1-dimensional shape that you than can feed into your Dense layer.