Search code examples
pythontensorflowdeep-learningtime-seriesrecurrent-neural-network

Predicting values in time series for future periods using RNN in Tensorflow


I found one article about time series predicting using Recurrent Neural Networks (RNN) in Tensorflow.

In that article the test set is the last 20 values and the model predicts y_pred also for the last 20 values of the dataset and then calculates MSE of y_test and y_pred.

How can I extend the model to receive the prediction for next periods in the future (actual forecasting)?


Solution

  • In first step you should use real values. Then using predict value to replace last value as you want. Hope the following code could help you.

    with tf.Session() as sess:
        saver.restore(sess, './model_saved')
        preds = []
        X_batch = last_n_steps_value
        X_batch = X_batch.reshape(-1, n_steps, 1)
        for i in range(number_you_want_to_predict):
            pred = sess.run(outputs, feed_dict={X: X_batch})
            preds.append(pred.reshape(7)[-1])
            X_batch = X_batch[:, 1:]
            # Using predict value to replace real value
            X_batch = np.append(X_batch, pred[:, -1])
            X_batch = X_batch.reshape(-1, n_steps, 1)