When we build a model using tf.keras
, I want the output to have a consistent value. That is to say, I do not want the output to randomly change. For example, if I run the following code, the output is always different, and I guess this may be due to the random initialization of parameters.
inputs1 = tf.keras.layers.Input(shape=(3, 1))
lstm1 = tf.keras.layers.LSTM(1)(inputs1)
model = tf.keras.models.Model(inputs = inputs1, outputs = lstm1)
data = np.array([0.1, 0.2, 0.3]).reshape(1, 3, 1)
print (model.predict(data))
To resolve this problem, I observe that adding the following line at the beginning of the file solves the problem.
tf.random.set_random_seed(0)
I have two questions regarding this.
-
Is the above approach a right approach when I build a model using tf.keras?
-
If I have two models in a file, and if I want to set the random seed only for one of them, then how could I do this?
Thank you!
Aucun commentaire:
Enregistrer un commentaire