python - KerasRegressor 和多输出问题

标签 python keras deep-learning

我有 3 个输入和 3 个输出。我正在尝试使用 KerasRegressor 和 cross_val_score 来获得我的预测分数。

我的代码是:

# Function to create model, required for KerasClassifier
def create_model():

    # create model
    # #Start defining the input tensor:
    input_data = layers.Input(shape=(3,))

    #create the layers and pass them the input tensor to get the output tensor:
    layer = [2,2]
    hidden1Out = Dense(units=layer[0], activation='relu')(input_data)
    finalOut = Dense(units=layer[1], activation='relu')(hidden1Out)

    u_out = Dense(1, activation='linear', name='u')(finalOut)   
    v_out = Dense(1, activation='linear', name='v')(finalOut)   
    p_out = Dense(1, activation='linear', name='p')(finalOut)   

    #define the model's start and end points
    model = Model(input_data,outputs = [u_out, v_out, p_out])    

    model.compile(loss='mean_squared_error', optimizer='adam')

    return model

#load data
...

input_var = np.vstack((AOA, x, y)).T
output_var = np.vstack((u,v,p)).T

# evaluate model
estimator = KerasRegressor(build_fn=create_model, epochs=num_epochs, batch_size=batch_size, verbose=0)
kfold = KFold(n_splits=10)

我试过:
results = cross_val_score(estimator, input_var, [output_var[:,0], output_var[:,1], output_var[:,2]], cv=kfold)


results = cross_val_score(estimator, input_var, [output_var[:,0:1], output_var[:,1:2], output_var[:,2:3]], cv=kfold)


results = cross_val_score(estimator, input_var, output_var, cv=kfold)

我收到错误消息,如:

细节:
ValueError:检查模型目标时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 3 个数组,但得到了以下 1 个数组的列表: [array([[ 0.69945297, 0.13296847, 0.06292328],

或者

ValueError:发现样本数量不一致的输入变量:[72963, 3]

那么我该如何解决这个问题呢?

谢谢。

最佳答案

问题是层的输入维度Input不是 3 ,但是 3*feature_dim .下面是一个工作示例

import numpy as np
import tensorflow as tf 
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,Dense,Concatenate
from sklearn.model_selection import cross_val_score,KFold
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor


def create_model():

    feature_dim = 10
    input_data = Input(shape=(3*feature_dim,))

    #create the layers and pass them the input tensor to get the output tensor:
    layer = [2,2]
    hidden1Out = Dense(units=layer[0], activation='relu')(input_data)
    finalOut = Dense(units=layer[1], activation='relu')(hidden1Out)

    u_out = Dense(1, activation='linear', name='u')(finalOut)   
    v_out = Dense(1, activation='linear', name='v')(finalOut)   
    p_out = Dense(1, activation='linear', name='p')(finalOut)   

    output = Concatenate()([u_out,v_out,p_out])
    #define the model's start and end points
    model = Model(inputs=input_data,outputs=output)    

    model.compile(loss='mean_squared_error', optimizer='adam')

    return model

x_0 = np.random.rand(100,10)
x_1 = np.random.rand(100,10)
x_2 = np.random.rand(100,10)
input_val = np.hstack([x_0,x_1,x_2])

u = np.random.rand(100,1)
v = np.random.rand(100,1)
p = np.random.rand(100,1)
output_val = np.hstack([u,v,p])

estimator = KerasRegressor(build_fn=create_model,nb_epoch=3,batch_size=8,verbose=False)
kfold = KFold(n_splits=3, random_state=0)
results = cross_val_score(estimator=estimator,X=input_val,y=output_val,cv=kfold)
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))
如您所见,由于输入维度是 10 , 内部 create_model , 我指定了 feature_dim .

关于python - KerasRegressor 和多输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61030977/

相关文章:

keras - 类型错误 : ('Not JSON Serializable:' , 尺寸(2048))

python-3.x - 遇到以下问题 : build_tensor_flow is not supported in Eager Mode

python - 在 Python 中使用新样式属性“无法设置属性”

python - 计算列表中的连续零

python /Django : correct way of calling subclass method

python - 如何将keras网络更改为channels_last而不是channels_first?

python - 带有 TensorFlow 后端的 Keras 的自定义损失函数问题

python - 如何使用由 "tensorflow.keras"构建的两层基本 CNN 算法来编码残差 block ?

python - Tensorflow:损失和准确度曲线表现出相似的行为

python - 在 Go (Golang) 中编写 Python 扩展