我对以下具有多个输出的 Keras 架构进行了实验:
def create_model(conv_kernels = 32, dense_nodes = 512):
model_input=Input(shape=(img_channels, img_rows, img_cols))
x = Convolution2D(conv_kernels, (3, 3), padding ='same', kernel_initializer='he_normal')(model_input)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Convolution2D(conv_kernels, (3, 3), kernel_initializer='he_normal')(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
conv_out = (Dense(dense_nodes, activation='relu', kernel_constraint=maxnorm(3)))(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)
lst = [x1, x2, x3, x4]
model = Model(inputs=model_input, outputs=lst)
sgd = SGD(lr=lrate, momentum=0.9, decay=lrate/nb_epoch, nesterov=False)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
当我尝试以这种方式应用网格搜索时:
model = KerasClassifier(build_fn=create_model1, epochs=nb_epoch, batch_size=batch_size, verbose=0)
param_grid = dict(conv_kernels = [16, 32, 64], dense_nodes = [128, 256, 512])
grid = GridSearchCV(estimator=model, cv=4, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X_train, Y_train)
我收到以下错误消息:
ValueError:发现样本数量不一致的输入变量:[9416, 4]
9416是训练样例数,4是模型输出数。
这里有什么问题?网格搜索对多个输出不可用?如果是这样,应用参数搜索的最佳方法是什么(除了纯手动方法)?