Compute0283
Compute0283

Reputation: 23

One step prediction of time series using LSTM

I want to predict stock prices using LSTM. I have successfully trained my model and have it saved. Now that I've loaded it back in, how would I use model.predict() to predict stock prices that do not have a corresponding value in the dataset as currently I can only 'predict' known values that are already in my dataset.

Senario: My model is already trained (with high enough accuracy) and I have it saved. I want to apply my model (load_model()). My time steps is set to 30 days so I've loaded in 30 days of data (eg. 9 Mar - 8 Apr) in the appropriate format but obviously I don't have the 'expected' output. How would I use model.predict() to predict the future value. Or I'm I missing something?

enter image description here

The graph above is when I use model.predict() to predict prices that are already in the dataset (dataset has 1150 data points). The graph ends on the 1150th day. How would I predict the 1151th day?

Model summary

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (50, 60, 100)             42400     
_________________________________________________________________
dropout_1 (Dropout)          (50, 60, 100)             0         
_________________________________________________________________
lstm_2 (LSTM)                (50, 60)                  38640     
_________________________________________________________________
dropout_2 (Dropout)          (50, 60)                  0         
_________________________________________________________________
dense_1 (Dense)              (50, 20)                  1220      
_________________________________________________________________
dense_2 (Dense)              (50, 1)                   21        
=================================================================
Total params: 82,281
Trainable params: 82,281
Non-trainable params: 0

Upvotes: 0

Views: 3089

Answers (1)

pastaleg
pastaleg

Reputation: 1838

So, assuming you have defined, trained, and saved a model somewhat like the following:

# define model
model = Sequential()
model.add(LSTM(...))

# compile model
model.compile(...)

# fit model
model.fit(...)

# save model
model.save('lstm_model.h5')

To predict new values with that model, load the model and run predict with a new set of input. For example, assume you predict Y based on X. It would look something like the following:

from keras.models import load_model
# load model
model = load_model('lstm_model.h5')

# define input
X = ...

# make predictions
yhat = model.predict(X, verbose=0)
print(yhat) 

It looks like you are working on a sequence regression problem where you define the time step and the LSTM predicts that value. The input X is therefore only the data/sequence necessary for making the prediction yhat. It does not include all training data before it. For example, if your input to training the LSTM is between 1...1500, then X would be 1501.

Remember to use any data preparation process you used on the training data on the inference data as well.

Upvotes: 1

Related Questions