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

2024-03-29 14:13:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个回归问题,我试图预测一个模型的单一输出。我有两个独立的输入。不过,我有两个主要问题,在模型拟合之前是否有必要分割数据,以获得正确的预测?模型建筑与(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,)

输入2

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]])>

形状(10002000)

数据分割

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)

Tags: ofthe模型modeltrainoutputsarrayactivation