【问题标题】:Regarding error using Keras functional API关于使用 Keras 功能 API 的错误
【发布时间】:2018-11-15 14:49:32
【问题描述】:

我有一个回归数据集:

X_train (float64) Size = (1616, 3) -> i.e. 3 predictors
Y_train (float64) Size = (1616, 2) -> i.e. 2 targets

我尝试使用功能 API 做 Hyperas(我的主要目的是在编译期间使用 loss_weights 选项):

inputs1 = Input(shape=(X_train.shape[0], X_train.shape[1]))

x  = Dense({{choice([np.power(2,1),np.power(2,2),np.power(2,3),np.power(2,4),np.power(2,5)])}}, activation={{choice(['tanh','relu', 'sigmoid'])}})(inputs1)
x  = Dropout({{uniform(0, 1)}})(x)

x  = Dense({{choice([np.power(2,1),np.power(2,2),np.power(2,3),np.power(2,4),np.power(2,5)])}}, activation={{choice(['tanh','relu', 'sigmoid'])}})(x)
x  = Dropout({{uniform(0, 1)}})(x)

x  = Dense({{choice([np.power(2,1),np.power(2,2),np.power(2,3),np.power(2,4),np.power(2,5)])}}, activation={{choice(['tanh','relu', 'sigmoid'])}})(x)
x  = Dropout({{uniform(0, 1)}})(x)

if conditional({{choice(['three', 'four'])}}) == 'four':
    x  = Dense({{choice([np.power(2,1),np.power(2,2),np.power(2,3),np.power(2,4),np.power(2,5)])}}, activation={{choice(['tanh','relu', 'sigmoid'])}})(x)
    x  = Dropout({{uniform(0, 1)}})(x)

output1 = Dense(1,  activation='linear')(x)
output2 = Dense(1,  activation='linear')(x)

model = Model(inputs = inputs1, outputs = [output1,output2])

adam    = keras.optimizers.Adam(lr={{choice([10**-3,10**-2, 10**-1])}})
rmsprop = keras.optimizers.RMSprop(lr={{choice([10**-3,10**-2, 10**-1])}})
sgd     = keras.optimizers.SGD(lr={{choice([10**-3,10**-2, 10**-1])}})

choiceval = {{choice(['adam', 'rmsprop','sgd'])}}
if choiceval == 'adam':
    optimizer = adam
elif choiceval == 'rmsprop':
    optimizer = rmsprop
else:
    optimizer = sgd

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

earlyStopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=50, verbose=0, mode='auto')
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=2, save_best_only=True, mode='max')
lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.5, cooldown=1, patience=10, min_lr=1e-4,verbose=2)
callbacks_list = [earlyStopping, checkpoint, lr_reducer]

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: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (1616, 3)

如果有人能指出这里出了什么问题的方向,我将不胜感激。我怀疑输入(即X_trainY_train)以及输入形状可能有问题。在此不胜感激。

更新

好吧,确实是输入行出错了:

我改成了:inputs1 = Input(shape=(X_train.shape[1],))

但是,现在我收到另一个错误:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[0.19204772, 0.04878049],
   [0.20226056, 0.        ],
   [0.12029842, 0.04878049],
   ...,
   [0.45188627, 0.14634146],
   [0.26942276, 0.02439024],
   [0.12942418, 0....

【问题讨论】:

    标签: python machine-learning keras hyperas


    【解决方案1】:

    由于您的模型有两个输出层,您需要在调用 fit() 方法时传递 一个列表两个数组 作为真正的目标(即 y) .比如这样:

    model.fit(X_train, [Y_train[:,0:1], Y_train[:,1:]], ...)
    

    【讨论】:

    • 谢谢,我做到了,我得到了这个:Epoch 1/20000 - 1s - loss: 0.2504 - dense_4_loss: 0.3083 - dense_5_loss: 0.1925 - dense_4_mean_absolute_error: 0.3083 - dense_5_mean_absolute_error: 0.1925 - val_loss: :0.1793 - val_dense_5_loss:0.0657 - val_dense_4_mean_absolute_error:0.1793 - val_dense_5_mean_absolute_error:0.065
    • 好吧,它的损失是组合的一个,以及 2 个输出层。
    • @Corse 组合损失、每个输出层的损失和每个输出层的度量值。但是,由于您也使用 mae 作为损失函数,因此您可以将其作为指标删除(或使用不同的指标)。同样的事情也适用于验证。
    • 顺便说一句,我假设我也应该这样做得分,acc = model.evaluate(X_val, [epidist_train,mw_train], verbose=2)。我收到了这个奇怪的错误:ValueError: Input arrays should have the same number of samples as target arrays.
    • @corse 那是因为X_val 中的样本数不等于epidist_trainmw_train。根据模型的定义,确保目标数组的形状为(n_samples, 1)
    猜你喜欢
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2019-05-07
    • 2023-03-27
    • 2021-01-11
    • 2018-07-31
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多