python - 用于多数字识别的 Keras

标签 python keras conv-neural-network mnist

我试图通过 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_orderingth。从 keras.layers 导入 Input 层。

我还注意到有多个输出。所以你需要使用 Function 模型而不是 Sequential。只需将其更改为:

model = Model(input=model_input, output=lst)

keras.models导入模型

关于python - 用于多数字识别的 Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082068/

相关文章:

python - 如何使用环境变量建立 psycopg2 连接?

python - 在 Python 中编写自定义矩阵类,__setitem__ 问题

python - Keras 对 python 的预测

machine-learning - 全局池操作的池大小是多少?

python - 如何使用selenium获取 'href'链接?

python - 从数组的每一行创建一个对象

numpy - 为什么 Keras 中的 to_categorical() 使用 float64 而不是 float32?

python - 无法在 'swish' 中导入 'tensorflow.python.keras.activations'

python - 在预训练模型中加载我的训练模型与在未预训练模型中加载之间的区别?

python - 继续使用 keras 中保存的模型训练 CNN