关于使用Keras函数式API的错误

我有一个回归数据集:

X_train (float64) Size = (1616, 3) -> 即3个预测变量Y_train (float64) Size = (1616, 2) -> 即2个目标变量

我尝试使用函数式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 = adamelif choiceval == 'rmsprop':    optimizer = rmspropelse:    optimizer = sgdmodel.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....

回答:

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

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注