zell
zell

Reputation: 10204

Why does a fixed random seed have no effects for a tensorflow function that has no parameters?

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)

Upvotes: 0

Views: 130

Answers (1)

Vijay Mariappan
Vijay Mariappan

Reputation: 17191

This is a known issue from tensorflow and its discussed in detail here- https://github.com/tensorflow/tensorflow/issues/33297

Tensorflow guide recommends not to use tf.normal. functions as well: https://www.tensorflow.org/guide/random_numbers enter image description here

You should use tf.random.stateless_uniform([1], seed=(2,3)) instead of tf.random.uniform([1])

Upvotes: 1

Related Questions