3

I'm trying to get repeatable results when running a session, but want to change the seed freely between sessions. Something like this:

a = tf.random_uniform([1])

#Set seed here to e.g. 123
with tf.Session() as sess:
   print(sess.run(a)) #Output: A1
   print(sess.run(a)) #Output: A2

#Set seed here to e.g. 42
with tf.Session() as sess:
   print(sess.run(a)) #Output: A3
   print(sess.run(a)) #Output: A4

#Set seed here to e.g. 123
with tf.Session() as sess:
   print(sess.run(a)) #Output: A1
   print(sess.run(a)) #Output: A2

If I understood the set_random_seed page correctly, the method seems to set the seed at the graph level, so between session the results will be the same. In fact, according to that page, it only seems possible to:

  • Make the run not reproducible
  • Make single operations reproducible, without possibility to change between sessions (by setting the seed of the operation directly)
  • Make all operations reproducible, without possibility to change between sessions (by using set_random_seed)

I could not find any way to flexibly change the seed without having to rebuild the graph. Any pointers to the right solution would be highly appreciated.

acester123
  • 125
  • 1
  • 5
  • Have you tried operation level seed? `tf.random.normal(..., seed=1))` without declaring seed at graph level. – Sharky Mar 14 '19 at 12:51
  • @Sharky Unfortunately, that creates a new operation. I want to keep the graph the same, because in my actual use case the random operation has lots of dependencies that I don't want to reconnect to the new random operation. – acester123 Mar 14 '19 at 12:57
  • Then I'm afraid you can't do this without `reset_default_graph()` – Sharky Mar 14 '19 at 13:07
  • 2
    There is just no way to do that currently. Random seeds are stored as op attributes, and they cannot be changed after the op is created. I have posted answers and comments in a few related questions, like [How to get and set random state (seed)](https://stackoverflow.com/q/49071514), [Printing out the random seed automatically chosen by tensorflow](https://stackoverflow.com/q/52333517) and [How to use random number in user defined tensorflow op?](https://stackoverflow.com/q/52600729), with some information about how random number generation works in TensorFlow, in case you find it useful. – jdehesa Mar 14 '19 at 13:08
  • 1
    This will probably change in TensorFlow 2.0 though, looking at the RFC [Random numbers in TensorFlow 2.0](https://github.com/tensorflow/community/blob/master/rfcs/20181217-tf2-random-numbers.md). – jdehesa Mar 14 '19 at 13:15
  • @acester123, Did you get a chance to try your case in 2.0, in line with jdehesa's comments, and is your issue resolved? –  Sep 13 '19 at 01:27

0 Answers0