Reproducible results in Tensorflow with tf.set_random_seed

后端 未结 7 1736
死守一世寂寞
死守一世寂寞 2020-11-28 13:28

I am trying to generate N sets of independent random numbers. I have a simple code that shows the problem for 3 sets of 10 random numbers. I notice that even though I use th

7条回答
  •  攒了一身酷
    2020-11-28 14:01

    There is a related GitHub issue. But in your case, please refer to the documentation of tf.set_random_seed:

    Sets the graph-level random seed.

    You probably want to use the same graph and same operation to get the same random numbers in different sessions.

    import tensorflow as tf
    
    tf.set_random_seed(1234)
    generate = tf.random_uniform((10,), 0, 10)
    tf.get_default_graph().finalize() # something everybody tends to forget
    
    for i in range(3):
        with tf.Session() as sess:
            b = sess.run(generate)
            print(b)
    

    gives

    [9.604688  5.811516  6.4159    9.621765  0.5434954 4.1893444 5.8865128
     7.9785547 8.296125  8.388672 ]
    [9.604688  5.811516  6.4159    9.621765  0.5434954 4.1893444 5.8865128
     7.9785547 8.296125  8.388672 ]
    [9.604688  5.811516  6.4159    9.621765  0.5434954 4.1893444 5.8865128
     7.9785547 8.296125  8.388672 ]
    

    In your case, you created different operations within the same graph.

提交回复
热议问题