关于多输出的Keras回归问题中的损失加权

时间:2018-11-15 13:22:21

标签: python keras

我正在针对回归问题运行Hyperas优化,其中包含3个预测变量(X)和2个目标(Y)。

摄取原始数据后,我这样做了:

X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.2, random_state=111)

# Input layers and Hidden Layers
model = Sequential()
model.add(Dense({{choice([np.power(2,1),np.power(2,2),np.power(2,3),np.power(2,4),np.power(2,5)])}}, input_dim = X_train.shape[1]))
model.add(Activation({{choice(['tanh','relu', 'sigmoid'])}}))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([np.power(2,1),np.power(2,2),np.power(2,3),np.power(2,4),np.power(2,5)])}}))
model.add(Activation({{choice(['tanh','relu', 'sigmoid'])}}))
model.add(Dropout({{uniform(0, 1)}}))

# Output layer
model.add(Dense(Y_train.shape[1]))
model.add(Activation('linear'))

model.compile(loss='mae', metrics=['mae'],optimizer=optimizer, loss_weights=[0.6,0.4])

history = model.fit(X_train, Y_train,
          batch_size={{choice([16,32,64,128])}},
          epochs={{choice([20000])}},
          verbose=2,
          validation_data=(X_val, Y_val),
          callbacks=callbacks_list)

但是,运行此命令时会显示:

ValueError: When passing a list as loss_weights, it should have one entry per model output. The model has 1 outputs, but you passed loss_weights=[1, 1]

我猜是由于我的输入和输出格式所致。但是,我无法找出应该将其输入模型的正确格式。

请感谢您的建议,谢谢。

0 个答案:

没有答案