https://www.tensorflow.org/api_docs/python/tf/random/set_seed
Operations that rely on a random seed actually derive it from two seeds: the global and operation-level seeds. This sets the global seed.
Its interactions with operation-level seeds is as follows:
If neither the global seed nor the operation seed is set: A randomly picked seed is used for this op.
If the global seed is set, but the operation seed is not: The system deterministically picks an operation seed in conjunction with the global seed so that it gets a unique random sequence. Within the same version of tensorflow and user code, this sequence is deterministic. However across different versions, this sequence might change. If the code depends on particular seeds to work, specify both global and operation-level seeds explicitly.
If the operation seed is set, but the global seed is not set: A default global seed and the specified operation seed are used to determine the random sequence.
If both the global and the operation seed are set: Both seeds are used in conjunction to determine the random sequence.
依赖随机种子的行为实际上从两个种子共同生成该随机种子:全局种子和操作级别的种子。
set_seed
函数用于设置全局种子。全局种子跟操作级别种子的交互如下:
- 如果两个种子都没有设置,一个随机挑选的种子将用于当次操作。
- 如果设置了全局种子但没设置操作种子:系统会确定性地选择一个操作种子,结合全局种子,从而得到一个唯一的随机序列。Tensorflow版本及用户的代码不变的情况下,这个序列是确定性的。然而在不同的版本间,这个序列可能发生变化。如果代码依赖某个特定种子而运行,需要明确地指定全局和操作级别的种子。
- 如果设置了操作级别的种子,而未设置全局种子:默认的全局种子加上指定的操作种子被用于决定随机序列。
- 如果同时设置了全局和操作种子:两个种子结合起来用于决定随机序列。
情况1
- 如果全局种子和操作种子都没有设置,每次随机操作的调用、以及每次程序重新运行,都会得到不同的结果:
print(tf.random.uniform([1])) # 生成 'A1'
print(tf.random.uniform([1])) # 生成 'A2'
(关闭程序重新运行后)
print(tf.random.uniform([1])) # 生成 'A3'
print(tf.random.uniform([1])) # 生成 'A4'
情况2
- 如果设置了全局种子、但没有设置操作种子,每次调用随机操作都会得到不同的结果,但是每次重新运行程序会得到相同的序列:
tf.random.set_seed(1234)
print(tf.random.uniform([1])) # 生成'A1'
print(tf.random.uniform([1])) # 生成'A2'
(关闭程序重新运行后)
tf.random.set_seed(1234)
print(tf.random.uniform([1])) # 生成'A1'
print(tf.random.uniform([1])) # 生成'A2'
上面在第二次调用tf.random.uniform
时会得到A2而不是A1,是因为第二次调用使用了一个不同的操作种子。
注意在这种情况下,tf.function
的表现相当于程序的重新运行。当设置了全局种子但没设置操作种子,对每个tf.function
,随机数序列是相同的。比如说:
tf.random.set_seed(1234)
@tf.function
def f():
a = tf.random.uniform([1])
b = tf.random.uniform([1])
return a, b
@tf.function
def g():
a = tf.random.uniform([1])
b = tf.random.uniform([1])
return a, b
print(f()) # 打印 '(A1, A2)'
print(g()) # 打印 '(A1, A2)'
情况3
- 如果设置了操作种子,每次随机操作的调用会产生不同的结果,但是每次重新运行程序会产生相同的序列:
print(tf.random.uniform([1], seed=1)) # 生成 'A1'
print(tf.random.uniform([1], seed=1)) # 生成 'A2'
(关闭程序重新运行后)
print(tf.random.uniform([1], seed=1)) # 生成 'A1'
print(tf.random.uniform([1], seed=1)) # 生成 'A2'
上面第二次调用tf.random.uniform
得到了'A2而非'A1',原因是Tensorflow对所有参数相同的tf.random.uniform
的调用,会使用相同的内核(即内部表达),这个内核会保持一个内部计数器,每次执行后都递增,因而产生了不同的结果。
调用tf.random.set_seed
会重置这种计数器。
tf.random.set_seed(1234)
print(tf.random.uniform([1], seed=1)) # 生成 'A1'
print(tf.random.uniform([1], seed=1)) # 生成 'A2'
tf.random.set_seed(1234)
print(tf.random.uniform([1], seed=1)) # 生成 'A1'
print(tf.random.uniform([1], seed=1)) # 生成 'A2'
当多个相同的随机操作被包装在一个tf.function
内,它们的行为会发生变化,因为操作不再共享相同的计数器。例如:
@tf.function
def foo():
a = tf.random.uniform([1], seed=1)
b = tf.random.uniform([1], seed=1)
return a, b
print(foo()) # 打印 '(A1, A1)'
print(foo()) # 打印 '(A2, A2)'
@tf.function
def bar():
a = tf.random.uniform([1])
b = tf.random.uniform([1])
return a, b
print(bar()) # 打印 '(A1, A2)'
print(bar()) # 打印 '(A3, A4)'
第二次调用foo
会返回 '(A2, A2)'而非'(A1, A1)' ,原因是tf.random.uniform
保持了一个内部计数器。如果希望foo
每次都返回'(A1, A1)',使用无状态的随机操作,比如tf.random.stateless_uniform
。
另外可参考tf.random.experimental.Generator
,是一组有状态的随机操作,使用外部变量来管理它们的状态。