vendredi 22 mai 2020

Why is RNG different for TensorFlow 2 and 1?

import numpy as np
np.random.seed(1)
import random
random.seed(2)
import tensorflow as tf
tf.compat.v1.set_random_seed(3)  # graph-level seed
if tf.__version__[0] == '2':
    tf.random.set_seed(4)  # global seed
else:
    tf.set_random_seed(4)  # global seed

from tensorflow.keras.initializers import glorot_uniform as GlorotUniform
from tensorflow.keras import backend as K

init = GlorotUniform(seed=5)(shape=(4, 4))
print(K.eval(init))
[[-0.75889236  0.5744677   0.82025963 -0.26889956]
 [ 0.0180248  -0.24747121 -0.0666492   0.23440498]
 [ 0.61886185  0.05548459  0.39713246  0.126324  ]
 [ 0.6639387  -0.58397514  0.39671892  0.67872125]]  # TF 2

[[ 0.2515846  -0.41902617 -0.7859829   0.41573995]
 [ 0.8099498  -0.6861247  -0.46198446 -0.7579694 ]
 [ 0.29976922  0.0310365   0.5031274   0.314076  ]
 [-0.62062943 -0.01889879  0.7725797  -0.65635633]]  # TF 1

Why the difference? This is creating severe reproducibility problems between the two versions - and this or something else, within the same version's (TF2) Graph vs. Eager. More importantly, can TF1's RNG sequence be used in TF2?




Aucun commentaire:

Enregistrer un commentaire