多位数预测中的Keras数组错误

时间:2016-10-01 23:44:16

标签: keras

我正在尝试实现这种架构来预测来自SVHN数据集的图像中的数字,我需要从图像中预测多个数字。 architecture

我有以下准备充分的数据形状:

  • X_train:(188602,32,32,1)
  • y_train_dummy:(188602,5,11)
  • X_val:(47151,32,32,1)
  • y_val_dummy:(47151,5,11)

然而,在我的代码中,当我尝试适合时,我发现了这个错误(之前一切正常)。

    ---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-136-c031c9f027b5> in <module>()
----> 1 model.fit(X_train, y_train_dummy, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val_dummy))

/Users/home/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight)
   1032                                                            class_weight=class_weight,
   1033                                                            check_batch_dim=False,
-> 1034                                                            batch_size=batch_size)
   1035         # prepare validation data
   1036         if validation_data:

/Users/home/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_dim, batch_size)
    963                                    output_shapes,
    964                                    check_batch_dim=False,
--> 965                                    exception_prefix='model target')
    966         sample_weights = standardize_sample_weights(sample_weight,
    967                                                     self.output_names)

/Users/home/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in standardize_input_data(data, names, shapes, check_batch_dim, exception_prefix)
     74             raise Exception('The model expects ' + str(len(names)) +
     75                             ' input arrays, but only received one array. '
---> 76                             'Found: array with shape ' + str(data.shape))
     77         arrays = [data]
     78 

Exception: The model expects 5 input arrays, but only received one array. Found: array with shape (188602, 5, 11)

我的代码:

batch_size = 128
nb_classes = 10
nb_epoch = 2

_, img_rows, img_cols, img_channels = X_train.shape

model_input = Input(shape=(img_rows, img_cols, img_channels))
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 = Model(input=model_input, output=lst)

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(X_train, y_train_dummy, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val_dummy))

0 个答案:

没有答案