神经网络中的多个输入和单个输出

时间:2020-10-14 09:16:27

标签: python tensorflow keras neural-network

我有一个回归问题,我试图在其中预测模型的单个输出。我有两个单独的输入。不过,我有两个主要问题,是否需要在模型拟合之前先对数据进行分割,以便获得正确的预测?模型的构建与(Creating a multi-channel network: 'Concatenate' object has no attribute 'shape')类似,但其余的并不是很多。

模型制作

def build_model(input1, input2):

    input1= np.expand_dims(input1,1)

    # define two sets of inputs for models
    input1= Input(shape = (input1.shape[1], ))
    input2= Input(shape = (input1.shape[1],))

    # The first branch 
    x = Dense(units = 128, activation="relu")(input1)
    x = Dense(units = 128, activation="relu")(x)
    x = Model(inputs=input1, outputs=x)

    # The second branch 
    y = Dense(units = 128, activation="relu")(input2)
    y = BatchNormalization()(y)
    # y =Flatten()(y)
    y = Model(inputs=input2, outputs=y)
    
    # combine the output of the two branches
    combined = Concatenate()([x.output, y.output])

    outputs = Dense(128, activation='relu')(combined)
    #out = Dropout(0.5)(out)
    outputs = Dense(1)(outputs)

    # The model will accept the inputs of the two branches and then output a single value
    model = Model(inputs = [x.input, y.input], outputs = outputs)

    #model = Model(inputs=[x.input, y.input], outputs=z)

    # Compile the ANN
    model.compile(loss='mse', optimizer = Adam(lr = 0.001), metrics = ['mse'])

    # ANN Summary
    model.summary()
    
    return model

拟合模型

model.fit([x_input1_train, x_input2_train], [y_input1_train, y_input2_train], 
          validation_data = ([x_input1_valid, x_input2_valid], [y_input1_valid, y_input2_valid]))

错误

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 1 array(s), for inputs ['dense_61'] but instead got the following list of 2 arrays: [array([[0.  ],
       [24],
       [84],
       ...,
       [0],
       [45],
       [61]]), array([[45],
       [60],
       [40],
       ...,
       [0],
       [45],
       [...

数据

输入

输入1

array([406, 505, 545, ..., 601, 605, 450])

形状 :( 1000个)

Input2

array([[-2.00370455, -2.35689664, -1.96147382, ...,  2.11014128,
         2.59383321,  1.24209607],
       [-1.97130549, -2.19063663, -2.02996445, ...,  2.32125568,
         2.27316046,  1.48600614],
       [-2.01526666, -2.40440917, -1.94321752, ...,  2.15266657,
         2.68460488,  1.23534095],
       ...,
       [-2.1359458 , -2.52428007, -1.75701785, ...,  2.25480819,
         2.68114281,  1.75468981],
       [-1.95868206, -2.23297167, -1.96401751, ...,  2.07427239,
         2.60306072,  1.28556955],
       [-1.80507278, -2.62199521, -2.08697271, ...,  2.34080577,
         2.48254585,  1.52028871]])>

形状 :( 1000年,2000年)

数据拆分

y = target_data

x_input1_train, x_input1_valid, y_input1_train, y_input1_valid = train_test_split(input1, y, test_size = 0.2, random_state = 6)

x_input2_train, x_input2_valid, y_input2_train, y_input2_valid = train_test_split(input2, y, test_size = 0.2, random_state = 6)

0 个答案:

没有答案