I am fixing a global random seed with tensorflow, and trying to use @tf.function so that each function call can behave the same at each call. Tested on functions f
and g
below:
tf.random.set_seed(1234)
@tf.function
def g(i):
print ("round", i)
return tf.random.uniform([1])
@tf.function
def f():
return tf.random.uniform([1])
print(g(0))
print(g(1))
print (f())
print (f())
While I did get different results on calling g
, I got different results on calling f
. Why?
round 0
tf.Tensor([0.13047123], shape=(1,), dtype=float32)
round 1
tf.Tensor([0.13047123], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
CodePudding user response:
This is a known issue from tensorflow and its discussed in detail here-
You should use tf.random.stateless_uniform([1], seed=(2,3))
instead of tf.random.uniform([1])