Hello. I want to use keras for time series forecasting. When forecasting, I hope that the forecast result at time t will be used as the input at time t+1. So when I want to train, I hope to use the output at time t as the input at time t+1. May I ask how it can be implemented in keras. Or how can it be achieved with tensorflow. The desired effect is shown in the figure.https://user-images.githubusercontent.com/52521165/127980768-1e6ae109-5761-46ca-8c43-50ccd103d8c8.png

If you train LSTM model to predict future values one step ahead, you can use a deque object to obtain a forecast for several steps ahead. At first you put your latest available data to the deque and pass this same data to the model to get a predicted value for the next time step. Then you use popleft() to remove the earliest value from the deque and append the predicted next value to the right. Repeat this procedure as many times as necessary.

Other option is to create LSTM model which will output predicted values for several steps ahead. If your model contains one or several LSTM layers put a dense layer with several units at the end and train this model with targets representing equal number of future data points.

Take a look also to our examples:

Hello, Can you provide some pseudo code or anything else, I do not quite understand. thanks.

hello, I have seen it, and the effect has not improved.

For example, if you have monthly sales data and trained you model to predict next month based on the previous 12 values, you model could look something like this:

```
model = Sequential([
LSTM(8, activation='relu', recurrent_dropout=0.15, kernel_regularizer=regularizers.l2(0.01), return_sequences=True, input_shape=(12, 1)),
LSTM(8, activation='relu', recurrent_dropout=0.15, kernel_regularizer=regularizers.l2(0.01), return_sequences=False),
Dense(1)])
model.compile(optimizer='adam', loss='mse')
```

And now you want to make a prediction for 6 months ahead. You can use a for loop making consecutive predictions:

```
from collections import deque
last_data = deque(x[-12:]) # slice last 12 data points from your input data
for i in range(6):
model_input = np.array(last_data, dtype='float32')
model_input = model_input.reshape((1, 12, 1))
next_month = model.predict(model_input)
print(f'Prediction for period {i}: {next_month}')
last_data.append(next_month)
last_data.popleft()
```

Or just make the last layer Dense(6) and train the model with 6 target values for each of the 6 months ahead.

The way I predict the data is similar to what you said, as described in this picture. It’s just that I can’t find a good model to make such predictions better. So when I want to train, I also train as described in the figure, but I don’t know how to achieve it. I cannot get the result value of every training output. I don’t know how to modify my training data set in real time.

Hi,

Can you explain a little bit more what you’re trying to do?

usually, if you want to have access to the results at each epoch, you might need to write your own training loop.

- Custom training: walkthrough | TensorFlow Core
- Writing a training loop from scratch | TensorFlow Core

Is this some kind of simulation or time series related?

I hope to use lstm model for time series forecasting. The desired effect is shown in the figure.