多输入多输出模型返回单个值

时间:2019-03-28 09:30:45

标签: python-3.x keras deep-learning lstm keras-layer

我正在尝试使用Keras中的多输入模型使用时间序列(即过去n个月的收入)和时不变的客户数据(即性别,职业)来预测客户收入。为此,我使用LSTM模型预测了时间(n + 1)的收入,然后将客户数据作为辅助输入。

第一个模型(LSTM)产生了不错的结果,但是添加辅助层似乎造成了一个悖论,因为每个客户的预测都是相同的值。有什么建议为什么会这样吗?

已经尝试了多种批处理大小并转换了一些数字变量,但问题仍然存在。

from keras.layers import Input, LSTM, Dense, concatenate
from keras.models import Model


'''
train_X1: time series data, customer revenues, 1 to (n-1)
train_X2: customer demographics
'''


train_X1 = np.asarray(X1.drop('id', axis = 1))
train_X1 = train_X1.reshape(train_X1.shape[0], train_X1.shape[1], 1)
print(train_X1.shape)

train_X2 = np.asarray(X2.drop('id', axis = 1))
print(train_X2.shape)


main_input = Input(shape = (20,1), name = "main_input")
hidden = Dense(10, activation = 'tanh')(main_input)
hidden = LSTM(10)(hidden)
aux_output = Dense(1, name = 'aux_output')(hidden)

train_X2 = np.asarray(X2.drop('id', axis = 1))
train_X2 = train_X2.reshape(train_X2.shape[0], train_X2.shape[1])
print(train_X2.shape)

aux_input = Input(shape = (2, ), name = "aux_input")
hidden = concatenate([aux_output, aux_input])
hidden = Dense(2, activation = 'relu')(hidden)
main_output = Dense(1, name = "main_output")(hidden)

model = Model(inputs = [main_input, aux_input], outputs = [main_output, aux_output])

model.compile(optimizer ='adam',
              loss = {'main_output': 'mean_squared_error', 'aux_output': 'mean_squared_error'},
              loss_weights = {'main_output': 1, 'aux_output': 0.2})
model.fit({'main_input': train_X1, 'aux_input': train_X2},
                {'main_output': y, 'aux_output': y},
                epochs = 50, batch_size = 5)

期望变化的预测,但是此代码仅导致单个值错误。 (参见图片)

注意:我目前仅在很小的数据集中模拟这项工作,然后才能将其扩展为更大的集合。这可能是数据问题吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

完成时

preds = model.predict([train_X1, train_X2])

您将获得两个numpy数组的列表。

如果train_X1的形状为(100, 20, 1),而train_X2的形状为(100, 2),则您的preds将是两个numpy数组的列表,其中每个数组形状为(100, 1)

您可以像这样访问测试样本i的预测

preds[0][i], preds[1][i]

您可以像这样遍历数组以获取每个样本的输出,以进行视觉验证

preds = model.predict([train_X1, train_X2])
for i in range(len(preds[1])):
    print(f'Main prediction for sample {i} = {preds[0][i]} --- Aux Prediction for sample {i} = {preds[1][i]}')


# Output

Main prediction for sample 0 = [-5.820766e-09] --- Aux Prediction for sample 0 = [-9.313226e-09]
Main prediction for sample 1 = [-5.820766e-09] --- Aux Prediction for sample 1 = [-9.313226e-09]
Main prediction for sample 2 = [-5.820766e-09] --- Aux Prediction for sample 2 = [-9.313226e-09]
Main prediction for sample 3 = [-5.820766e-09] --- Aux Prediction for sample 3 = [-9.313226e-09]
Main prediction for sample 4 = [-5.820766e-09] --- Aux Prediction for sample 4 = [-9.313226e-09]
..................................................................................................