阅读(1232)
赞(11)
TensorFlow随机张量:tf.set_random_seed函数
2018-01-19 10:34:17 更新
tf.set_random_seed 函数
set_random_seed(seed)
定义在:tensorflow/python/framework/random_seed.py.
请参阅指南:生成常量,序列和随机值>随机张量
设置图形级随机seed.
可以从两个seed中获得依赖随机seed的操作:图形级seed和操作级seed.本节是介绍如何设置图形级别的seed.
它与操作级别seed的交互如下:
- 如果既没有设置图层级也没有设置操作级别的seed:则使用随机seed进行该操作.
- 如果设置了图形级seed,但操作seed没有设置:系统确定性地选择与图形级seed结合的操作seed,以便获得唯一的随机序列.
- 如果未设置图形级seed,但设置了操作seed:使用默认的图层seed和指定的操作seed来确定随机序列.
- 如果图层级seed和操作seed都被设置:则两个seed将一起用于确定随机序列.
为了说明用户可见的效果,请考虑以下示例:
要在会话中生成不同的序列,请不要设置图层级别seed或操作级别seed:
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 sess2:
print(sess2.run(a)) # generates 'A3'
print(sess2.run(a)) # generates 'A4'
print(sess2.run(b)) # generates 'B3'
print(sess2.run(b)) # generates 'B4'
要为会话中的操作生成相同的可重复序列,请为操作设置seed:
a = tf.random_uniform([1], seed=1)
b = tf.random_normal([1])
# Repeatedly running this block with the same graph will generate the same
# sequence of values for 'a', but different sequences of values for 'b'.
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 sess2:
print(sess2.run(a)) # generates 'A1'
print(sess2.run(a)) # generates 'A2'
print(sess2.run(b)) # generates 'B3'
print(sess2.run(b)) # generates 'B4'
要使所有操作生成的随机序列在会话中可重复,请设置图形级别seed:
tf.set_random_seed(1234)
a = tf.random_uniform([1])
b = tf.random_normal([1])
# Repeatedly running this block with the same graph will generate the same
# sequences of 'a' and 'b'.
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 sess2:
print(sess2.run(a)) # generates 'A1'
print(sess2.run(a)) # generates 'A2'
print(sess2.run(b)) # generates 'B1'
print(sess2.run(b)) # generates 'B2'
函数参数
- seed:整数.