I am working on a stock price prediction project where I am trying to use an LSTM to predict stock prices. Currently, I am using trying to use 10 days of data to predict the 11th day’s stock price. To generalize my question regarding the target variable, let’s say I have the following
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
...
as my feature data where each list shown is a datapoint with 5 timesteps. Should my target variable be
[6]
[7]
...
or
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
I’ve tried both and have gotten better results with the second, but I am unclear what is the best approach. In regards to my question about return_sequences
, I understand that when return_sequences=False
, the last timestep is outputted for each data point. As a result, if return_sequences=True
, the last timestep for each output should be equal to the output when return_sequences=False
. However, I am getting different results.
Below is my code:
keras.utils.set_random_seed(420)
num_features = len(features)
callback = keras.callbacks.EarlyStopping(monitor='val_loss',
patience=10,
restore_best_weights=True)
model = Sequential()
model.add(LSTM(units=128))
model.add(Dense(units=1))
model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit(X_train_final, y_train_final,
epochs=50,
batch_size=64,
verbose=2,
validation_data = [X_test_final, y_test_final],
callbacks=[callback])
test_predict = model.predict(X_test_final)
I then use
test_predict = test_predict[:,-1,:]
when return_sequences=True
otherwise I leave it out if return_sequences=False
. I get different results for each.
Any help would be much appreciated!