我试图通过 Keras(theano 后端)中的一些练习来理解 CNN。我无法拟合下面的模型(错误:AttributeError:“Convolution2D”对象没有属性“get_shape”)。此数据集是来自 MNIST 数据的图像 (28*28) 连接在一起最多五个图像。所以输入的shape应该是1,28,140(灰度=1,高度=28,宽度=28*5)
目标是预测数字序列。谢谢!!
batch_size = 128
nb_classes = 10
nb_epoch = 2
img_rows =28
img_cols=140
img_channels = 1
model_input=(img_channels, img_rows, img_cols)
x = Convolution2D(32, 3, 3, border_mode='same')(model_input)
x = Activation('relu')(x)
x = Convolution2D(32, 3, 3)(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
conv_out = Flatten()(x)
x1 = Dense(nb_classes, activation='softmax')(conv_out)
x2 = Dense(nb_classes, activation='softmax')(conv_out)
x3 = Dense(nb_classes, activation='softmax')(conv_out)
x4 = Dense(nb_classes, activation='softmax')(conv_out)
x5 = Dense(nb_classes, activation='softmax')(conv_out)
lst = [x1, x2, x3, x4, x5]
model = Sequential(input=model_input, output=lst)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(dataset, data_labels, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)
最佳答案
问题出在输入层。进行以下更改:
model_input=Input(shape=(img_channels, img_rows, img_cols))
前提是您的 image_dim_ordering
是 th
。从 keras.layers
导入 Input
层。
我还注意到有多个输出。所以你需要使用 Function 模型而不是 Sequential。只需将其更改为:
model = Model(input=model_input, output=lst)
从keras.models
导入模型
。
关于python - 用于多数字识别的 Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082068/