0

Why does the following TensorFlow example generate a different result every time it is run? My understanding is that the seed should guarantee reproducibility here.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.set_random_seed(1234)
generate = tf.random_uniform(())
with tf.Session() as sess:
  print(generate.eval())

A more complex problem led me to this example.

Ryan
  • 1
  • 1

1 Answers1

0

I tried your code and it does give me the same results every time. It probably has to do how you are running it. Since it does matter where you run the code. For example if you search for tensorflow random number generator you get: CPU, GPU and TPU with the same algorithm and seed will generate the same integer random numbers. So, with different hard and software there will be differences.

  • Strangely enough, it works neither locally nor on Google Colab for me. Thanks for your insight – Ryan Mar 16 '23 at 08:31
  • It does seem to produce the same result if I restart the Python runtime. I.e. two new runtimes with the same seed produce the same result. It does not give reproducibility within a single runtime though. Perhaps I have misunderstood how it's supposed to work – Ryan Mar 16 '23 at 08:43
  • That makes sense, check also this answer: https://stackoverflow.com/questions/57305909/tensorflow-keras-reproducibility-problem-on-google-colab – Harmen Dijkstra Mar 16 '23 at 09:02