How to set seed the LSTM Model

Viewed 331

I would like to know whether anyone knows how to set seed the LSTM model so that we can get the reproducible model? In this case, my MSE and RMSE always changes over and over every time when I ran the code.

So far, my code is such the follows:

######################################################################################
import tensorflow as tf
import random as rn

os.environ['PYTHONHASHSEED'] = '0'

# Setting the seed for numpy-generated random numbers
np.random.seed(37)

# Setting the seed for python random numbers
rn.seed(1254)

# Setting the graph-level random seed.
tf.random.set_seed(89)

from keras import backend as K

session_conf = tf.compat.v1.ConfigProto(
      intra_op_parallelism_threads=1,
      inter_op_parallelism_threads=1)

#Force Tensorflow to use a single thread
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)

tf.compat.v1.keras.backend.set_session(sess)
######################################################################################

# define model
#reset_seeds()
model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

# fit model
#reset_seeds()
model.fit(X, y, epochs=50, verbose=0, shuffle=False)
model.reset_states()

#reset_seeds()
yhat = model.predict(X, verbose=0)

m = yhat.shape[0]
print("MSE: ", (1/m) * np.sum(np.square(np.subtract(y, yhat))))

print("RMSE: ", np.sqrt((1/m) * np.sum(np.square(np.subtract(y, yhat)))))

I have followed many guidelines on google but I just couldn't find one which works to get the my LSTM model reproducible. Thank you!

0 Answers
Related