0

In this tutorial, it explains the graph-level seed and the operation-level seed. It's confusing that after setting the graph-level seed, in the same session print the same variable but got different values as shown in the code:

tf.compat.v1.random.set_random_seed(1234)
a = tf.random.uniform([1])

with tf.compat.v1.Session() as sess1:
  print(sess1.run(a))  # generates 'A1'
  print(sess1.run(a))  # generates 'A2'

Even after setting the op-level seed, still print out different results.

KoalaJ
  • 145
  • 2
  • 11

1 Answers1

0

My understanding is that set seed is used to set the starting position of the the random generator. Therefore the first print is the first random uniform with a seed of 1234, while the second print is the second random uniform with a seed of 1234. For a new session, the seed will again be set to 1234.

Set seed is normally used to ensure that you get the same scenarios on different run and different hardware. Else it would be very hard and frustrating to error search code with errors dependent on random generators.

As an example we can start two different session and check that they are the same:

import tensorflow as tf

tf.compat.v1.random.set_random_seed(1234)
a = tf.random.uniform([1])

print('session 1')
with tf.compat.v1.Session() as sess1:
    print(sess1.run(a))  # generates 'A1'
    print(sess1.run(a))  # generates 'A1'

print('session 2')    
with tf.compat.v1.Session() as sess1:
    print(sess1.run(a))  # generates 'A1'
    print(sess1.run(a))  # generates 'A1'

>> session 1
>> [0.96046877]
>> [0.8362156]
>> session 2
>> [0.96046877]
>> [0.8362156]

As predicted, both session gives the same output. And you should get the same outputs if you try this on your computer.

This is an example of how to set seed between sessions. Instead setting random seed at the session level, do not seem to be possible to date? For a more in depth discussion on this, see the comment for this question

KrisR89
  • 1,483
  • 5
  • 11
  • Thanks. This is a very reasonable interpretation. It provides a clue to check if it is really the case (the second print is the second random uniform with a seed of 1234). After testing it, I will come to confirm if it's the right answer. – KoalaJ Jul 11 '19 at 23:50