5

The goal is to predict a timeseries Y of 87601 timesteps (10 years) and 9 targets. The input features X (exogenous input) are 11 timeseries of 87600 timesteps. The output has one more timestep, as this is the initial value. The output Yt at timestep t depends on the input Xt and on the previous output Yt-1.

Hence, the model should look like this: Model layout

I could only find this thread on this: LSTM: How to feed the output back to the input? #4068. I tried to implemented this with Keras as follows:

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)

This works, but is it not what I want to achieve, as there is no connection between input and output. Hence, the model doesn't learn how to correct for an error in the fed-back output, which results in poor accuracy when predicting as the error on the output is accumulated at every timestep.

Is there a way in Keras to implement the output-input feed-back during training stage? Also, as the initial value of Y is always known, I want to feed this to the network as well.

Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55
Astrid
  • 351
  • 1
  • 15
  • Did you manage to do it? – Sam Apr 04 '18 at 17:46
  • 1
    @Sam I haven't found a way to do this, but I did manage to get accurate prediction resuts, see [here](https://stackoverflow.com/questions/48929272/non-linear-multivariate-time-series-response-prediction-using-rnn) how. – Astrid Apr 05 '18 at 07:30
  • @Astrid You find how to manage it??? – Jonathan Roy Jul 03 '20 at 19:10
  • Like I said above, I didn't, though I found another way to make it work (see link in previous comment). – Astrid Jul 04 '20 at 17:33

0 Answers0