Keras:将输出作为下一个时间步的输入

Ast*_*rid 5 time-series output keras recurrent-neural-network

目标是预测 87601 个时间步长(10 年)和 9 个目标的时间序列 Y。输入特征 X(外源输入)是 87600 个时间步长的 11 个时间序列。输出还有一个时间步长,因为这是初始值。时间步 t 处的输出 Yt 取决于输入 Xt 和先前的输出 Yt-1。

因此,模型应如下所示:模型布局

我只能找到这个线程:LSTM: How to feed the output back to the input? 第4068章 我尝试使用 Keras 实现此功能,如下所示:

def build_model():
    # Input layers
    input_x = layers.Input(shape=(features,), name='input_x')
    input_y = layers.Input(shape=(targets,), name='input_y-1')

    # Merge two inputs
    merge = layers.concatenate([input_x,input_y], name='merge')

    # Normalise input
    norm = layers.Lambda(normalise, name='scale')(merge)

    # Hidden layers
    x = layers.Dense(128, input_shape=(features,))(norm)

    # Output layer
    output = layers.Dense(targets, activation='relu', name='output')(x)

    model = Model(inputs=[input_x,input_y], outputs=output)
    model.compile(loss='mean_squared_error', optimizer=Adam())

    return model

def make_prediction(model, X, y):
    y_pred = [y[0,None,:]]
    for i in range(len(X)):
        y_pred.append(model.predict([X[i,None,:],y_pred[i]]))
    y_pred = np.asarray(y_pred)
    y_pred = y_pred.reshape(y_pred.shape[0],y_pred.shape[2])
    return y_pred

# Fit
model = build_model()
model.fit([X_train, y_train[:-1]], [y_train[1:]]], epochs=200, 
          batch_size=24, shuffle=False)

# Predict
y_hat = make_prediction(model, X_train, y_train)
Run Code Online (Sandbox Code Playgroud)

这是可行的,但这不是我想要实现的目标,因为input 和 output 之间没有连接。因此,模型不会学习如何纠正反馈输出中的错误,这会导致预测时的准确性较差,因为输出上的错误会在每个时间步上累积。

Keras 有没有办法在训练阶段实现输出输入反馈?另外,由于 Y 的初始值总是已知的,我也想将其提供给网络。