2017-02-07 25 views
2

我想通过Keras(theano后端)中的一些练习来了解CNNs。我无法适应下面的模型(错误:AttributeError:'Convolution2D'对象没有'get_shape'属性)。该数据集是来自MNIST数据的图像(28 * 28),最多连接5个图像。所以输入形状应该是1,28,140(灰度= 1,高度= 28,宽度= 28 * 5)Keras用于多位数识别

目标是预测数字序列。谢谢!!

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) 

回答

4

问题出在输入层。执行以下更改:

model_input=Input(shape=(img_channels, img_rows, img_cols)) 

提供您的image_dim_orderingth。从keras.layers导入Input层。

我还注意到有多个输出。所以你需要使用函数模型而不是Sequential。从keras.models

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

进口Model:只需将其更改为。

+0

感谢您的回复。我仍然觉得张量对象是不可迭代的。 –

+0

是'model.fit()'的错误。如果是的话,我的猜测是'data_labels'应该是长度为5的numpy数组列表。每个numpy数组应该是维数'dataset.shape [0] x nb_classes' – indraforyou

+0

嗨,错误发生在激活层。以下是完整代码的链接:https://gist.github.com/jdills26/ca69e59ef19d4993636f6b50a7cbe514感谢您的帮助!这里是数据源:http://yann.lecun.com/exdb/mnist/index.html –