在 Keras 中使用 LSTM 进行多元多时间序列回归的恒定输出值

Constant output value for multi-variate multi-timeseries regression with LSTMs in Keras

我目前正在使用 Tensorflow 后端处理 Keras 中相似机器的多个时间序列和多个功能的回归问题。

目标是在每个时间步预测一个值,它给出了机器的剩余寿命。

我对所有值进行了标准化,并在所有时间序列前面填充了输入和输出,使它们与最长的一样长。 然后我添加了屏蔽层来屏蔽那些零。

我遇到的问题是网络为我提供了输出变量的常量值。

我已经玩过隐藏神经元、批次、激活函数、epoch,但没有真正改变。 损失缓慢减少,但所有时间步长的输出保持不变。 哦,它有点适用于一个时间序列。

对于所有零填充行,我得到一些值,如 4.88323085e-02(我猜这很好?),对于所有其他行,输出如下所示:

[...
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01,   8.72270355e+01,
8.72270355e+01,   8.72270355e+01]

我的数据格式如下:

[n_machines, n_timesteps, n_features]

输出如下:

[n_machines, n_timesteps, remaining_life]

我现在的模型是这样的:

model = Sequential()
model.add(Masking(mask_value=0., input_shape=(None, n_features)))
model.add(LSTM(1000, return_sequences = True, unit_forget_bias=True))
model.add(TimeDistributed(Dense(1)))

model.compile(loss="mse", optimizer="adam", metrics=[RMSE])

model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=100, batch_size=1, shuffle=True)

遗憾的是,使用某些东西代替神经网络不是一种选择,所以我需要完成这项工作。 如果有人能帮助我,我会很高兴。

经过多天的测试和尝试,我找到了一个可行的配置。显然我需要的是有状态的 LSTM。这是我的工作解决方案:

model = Sequential()
model.add(Masking(mask_value=0., batch_input_shape=(1, 362, 105)))
model.add(
    LSTM(100, return_sequences=True, unit_forget_bias=True, stateful=True))
model.add(TimeDistributed(Dense(1, activation="linear")))

model.compile(loss="mse", optimizer="adam", metrics=[RMSE])

# Train the model
for epoch in range(200):
    print('Epoch %s' % epoch)
    train_loss=[]

    # Train
    for i in range(x_train.shape[0]):
        tr_loss, tr_RMSE = model.train_on_batch(x_train[i, :, :].reshape((1, x_train.shape[1], x_train.shape[2])), y_train[i, :, :].reshape(((1, y_train.shape[1], y_train.shape[2]))))
        train_loss.append(tr_loss)
        model.reset_states()
print('loss training = {}'.format(np.mean(train_loss)))