Tensorflow中的随机数生成种子是在数据流图资源上运作的。每一个数据流图中,我们可以执行针对随机数生成种子应用不同的操作(operation)。事实上,随机数生成种子作为random系列函数的参数之一,可在相应的参数列表进行设置,这就是op-level的操作。与之对应的是graph-leveltf.set_random_seed()的操作tf.set_random_seed(),它管理着同一数据流图下的资源。
我们结合相应代码做简单的分析
import tensorflow as tf
# Repeatedly running this block with the same graph will generate the same
# sequences of 'a' and 'b'.
g1 = tf.Graph()
g2 = tf.Graph()
print("Graph 1")
with g1.as_default():
tf.set_random_seed(-1)
a = tf.random_uniform([1])
b = tf.random_normal([1])
print("Session 1")
with tf.Session() as sess1:
print(sess1.run(a)) # generates 'A1'
print(sess1.run(a)) # generates 'A2'
print(sess1.run(b)) # generates 'B1'
print(sess1.run(b)) # generates 'B2'
print("Session 2")
with tf.Session() as s