I am trying to learn about LSTM cells. I have read a few articles and am now trying to write my own code using tensorflow.
I came across this code from this tutorial. So I copied the lstm.py file and the data file sp500.csv. The code below is what I written.
Everything works fine. Each time I run the model I get very different predictions. I understand that because this model is very basic (trying to predict stock prices using just the closing price) this is to be expected. Also I believe in the background tensorflow uses some randomisation process to initiate the variables.
I wanted though to run the model and get the same results each time. So I read that to do this I need to use set_random_seed.
I have added this to my code below but when I re-run my code I get very different predictions. How am I supposed to use set_random_seed and why is the parameter 1234?
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, LSTM, Dropout, Activation
import lstm, time
import tensorflow as tf
def RunML():
tf.set_random_seed(1234)
X_train, y_train, X_test, y_test = lstm.load_data('sp500.csv', 50, True)
model = Sequential()
model.add(LSTM(50,
input_shape=(50, 1),
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(1))
model.add(Activation('linear'))
start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print ('compilation time : ', time.time() - start)
#Step 3 Train the model
model.fit(
X_train,
y_train,
batch_size=512,
nb_epoch=1,
validation_split=0.05)
predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
lstm.plot_results_multiple(predictions, y_test, 50)