tensorflow: tf.set_random_seed does not reset random op state

TF Version: 1.1.0rc1 (installed from nightly: Apr 10, 2017 1:03 AM) (run on CPU, Python 2)

import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

tf.set_random_seed(1)
a = tf.truncated_normal_initializer(seed=None)([1])
print(a.eval())

tf.set_random_seed(1)
b = tf.truncated_normal_initializer(seed=None)([1])
print(b.eval())

Output:

[ 1.05293429]
[-0.4487586]

Expected: The same value, since…

If the graph-level seed is set, but the operation seed is not: The system deterministically picks an operation seed in conjunction with the graph-level seed so that it gets a unique random sequence.

The values are identical in repeated runs of the whole script, but not after resetting the graph-level seed (as in the example above).

(possibly related to https://github.com/tensorflow/tensorflow/issues/9003)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 32 (14 by maintainers)

Most upvoted comments

any progress on this? looking for a simple approach to implement dropout as a bayesian representation, https://arxiv.org/pdf/1506.02142.pdf

Ah, your issue is poorly named; fixed. The thing you want is still impossible, since the state you’re trying to set is not part of the graph: it is part of the session. Resetting the state on the session would be a nice feature (@langmore wants it too).

The other option is to use random ops with custom seed control, which I am about to add as tf.contrib.stateless.stateless_random_uniform, etc.

@frthjf These new additions are useful, but as mentioned above, they are not being used by the common layer building blocks (yet?), so one would need to pass these manually to all layers (including special initialization schemes).

I ran into the same issue. However, for my use case I was able to use the stateless randomness:

for i in range(2):
    seed=(1,1)
    print(tf.contrib.stateless.stateless_random_uniform([1], seed).eval())