2017-02-07 31 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 –