最近在做图片的深度学习时,在程序中遇到了random.seed()、numpy.random.seed()、tf.set_random_seed()三种随机种子。在深度学习中,先确定好随机种子,以后每次随机的结果相同。在每次执行代码时,使每次切分后的训练集、验证集输入结果相同,便于验证学习参数的有效性和查找问题。
但是他们有什么异同?带着这些一连串的疑问,开始了实验。
以上三种随机种子分属于不同的Python模组,看似“长的”差不多、“效果”也差不多。关于他们三的用途,很多博主分开讲的很清楚,但是把他们放到一起做个对比的博文比较少,本文主要把他们放到一起做对比。
一、单独设置random.seed(1)
import numpy as np
import random
import tensorflow as tf
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.seed(1)
a = random.sample(list, 5)
b = np.random.randint(low=1, high=1000)
c = tf.random_normal([2, 3])
print(a)
print(b)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(c))
运行结果:
第一次执行结果:
[3, 2, 5, 1, 4]
560
[[-0.75625855 0.9842202 0.11055301]
[ 1.3178085 1.1288372 -1.3483504 ]]
第二次执行结果:
[3, 2, 5, 1, 4]
414
[[ 1.3603671 -0.8897449 -0.42791504]
[ 0.64151275 -0.2893453 -0.17066564]]
结论:random.seed(seed)对numpy、tensorflow模组内的随机函数不起作用。
二、单独设置numpy.random.seed(1)
from numpy.random import seed
import tensorflow as tf
import random
import numpy as np
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
seed(1)
a = random.sample(list, 5)
b = np.random.randint(low=1, high=1000)
c = tf.random_normal([2, 3])
print(a)
print(b)
with tf.Session() as sess1:
sess1.run(tf.global_variables_initializer())
print(sess1.run(c))
print(sess1.run(c))
with tf.Session() as sess2:
sess2.run(tf.global_variables_initializer())
print(sess2.run(c))
print(sess2.run(c))
执行结果:
# 第一次执行:
[4, 2, 7, 5, 10]
38
[[ 2.3121657 -0.8977492 -0.01008329]
[-0.6408148 0.19963263 -0.7215879 ]]
[[ 0.8633339 -0.69526696 1.6130834 ]
[ 0.30339143 0.77477974 0.5178088 ]]
[[-0.6610677 -0.14873989 0.592189 ]
[-0.691019 0.13392478 -0.03835497]]
[[ 0.9954625 -0.27525836 0.5789619 ]
[-1.0326229 -0.910877 -1.3278433 ]]
# 第二次执行:
[9, 10, 5, 1, 3]
38
[[-1.3300127 0.18582459 -0.19193105]
[-0.29505548 -0.082302 1.2551965 ]]
[[-0.5707125 0.19154593 -0.56202376]
[-1.3819716 -0.06733618 -0.06827015]]
[[-0.9323835 0.41968757 0.50094676]
[ 0.075909 1.0127947 1.3885998 ]]
[[ 0.40404725 -1.4369541 -0.3081668 ]
[ 0.29238573 -2.5474846 0.19280283]]
结论: numpy.random.seed(seed)对venv2、tensorflow模组内的随机函数不起作用。
三、单独设置tf.set_random_seed(1)
from numpy.random import seed
import tensorflow as tf
import numpy as np
import random
tf.set_random_seed(1)
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = random.sample(list, 5)
b = np.random.randint(low=1, high=1000)
c = tf.random_normal([2, 3])
print(a)
print(b)
with tf.Session() as sess1:
sess1.run(tf.global_variables_initializer())
print(sess1.run(c))
print(sess1.run(c))
with tf.Session() as sess2:
sess2.run(tf.global_variables_initializer())
print(sess2.run(c))
print(sess2.run(c))
运行结果:
# 第一次运行
[4, 9, 3, 5, 7]
244
[[-0.67086124 0.22357143 0.79727304]
[ 0.09617059 0.72314787 0.33812162]]
[[ 1.2690554 -2.1627116 1.7188579 ]
[-0.24154273 0.47120836 0.8914816 ]]
[[-0.67086124 0.22357143 0.79727304]
[ 0.09617059 0.72314787 0.33812162]]
[[ 1.2690554 -2.1627116 1.7188579 ]
[-0.24154273 0.47120836 0.8914816 ]]
# 第二次运行
[9, 4, 10, 7, 2]
278
[[-2.092974 0.72193277 0.8222583 ]
[-0.20968539 2.0839696 -0.87704235]]
[[ 0.10735091 -0.5698958 0.5758549 ]
[-0.21616328 -1.3219575 1.5569185 ]]
[[-2.092974 0.72193277 0.8222583 ]
[-0.20968539 2.0839696 -0.87704235]]
[[ 0.10735091 -0.5698958 0.5758549 ]
[-0.21616328 -1.3219575 1.5569185 ]]
结论:
tf.set_random_seed(seed)对venv2、numpy模组内的随机函数不起作用;
tf.set_random_seed(seed)设置的seed值仅一次有效。
通过相同的实验,random.seed(seed)、numpy.random.seed(seed)、tf.set_random_seed(seed)两两组合设置随机种子,均对第三方模组的随机函数不起作用,并且所设置的两两组合随机种子之间无干扰。在此就不罗列实验过程和结果了。
四、 三个种子一起设置
from numpy.random import seed
import tensorflow as tf
from tensorflow import set_random_seed
import numpy as np
import random
seed(1)
random.seed(1)
set_random_seed(1)
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = random.sample(list, 5)
b = np.random.randint(low=1, high=1000)
c = tf.random_normal([2, 3])
for i in range(2):
print(a)
print(b)
with tf.Session() as sess1:
sess1.run(tf.global_variables_initializer())
print(sess1.run(c))
print(sess1.run(c))
with tf.Session() as sess2:
sess2.run(tf.global_variables_initializer())
print(sess2.run(c))
print(sess2.run(c))
执行结果:
# 第一次执行结果
[3, 2, 5, 1, 4]
38
[[-0.67086124 0.22357143 0.79727304]
[ 0.09617059 0.72314787 0.33812162]]
[[ 1.2690554 -2.1627116 1.7188579 ]
[-0.24154273 0.47120836 0.8914816 ]]
[[-0.67086124 0.22357143 0.79727304]
[ 0.09617059 0.72314787 0.33812162]]
[[ 1.2690554 -2.1627116 1.7188579 ]
[-0.24154273 0.47120836 0.8914816 ]]
[3, 2, 5, 1, 4]
38
[[-0.67086124 0.22357143 0.79727304]
[ 0.09617059 0.72314787 0.33812162]]
[[ 1.2690554 -2.1627116 1.7188579 ]
[-0.24154273 0.47120836 0.8914816 ]]
[[-0.67086124 0.22357143 0.79727304]
[ 0.09617059 0.72314787 0.33812162]]
[[ 1.2690554 -2.1627116 1.7188579 ]
[-0.24154273 0.47120836 0.8914816 ]]
# 第二次执行结果
[3, 2, 5, 1, 4]
38
[[-0.3143593 0.6476281 0.01794253]
[-0.1755162 -0.5712612 -0.7455791 ]]
[[ 1.7597818 0.9010502 -0.766882 ]
[-0.16980433 0.98841023 1.1415099 ]]
[[-0.3143593 0.6476281 0.01794253]
[-0.1755162 -0.5712612 -0.7455791 ]]
[[ 1.7597818 0.9010502 -0.766882 ]
[-0.16980433 0.98841023 1.1415099 ]]
[3, 2, 5, 1, 4]
38
[[-0.3143593 0.6476281 0.01794253]
[-0.1755162 -0.5712612 -0.7455791 ]]
[[ 1.7597818 0.9010502 -0.766882 ]
[-0.16980433 0.98841023 1.1415099 ]]
[[-0.3143593 0.6476281 0.01794253]
[-0.1755162 -0.5712612 -0.7455791 ]]
[[ 1.7597818 0.9010502 -0.766882 ]
[-0.16980433 0.98841023 1.1415099 ]]
结论:
seed(1)、random.seed(1)、set_random_seed(1)之间互不干扰;
执行次数、for循环次数对seed(1)、random.seed(1)结果无影响;
执行次数、for循环次数对set_random_seed(1)执行结果有影响;
五、设置的seed()值仅一次有效
from numpy import *
num=0
random.seed(1)
b = random.random()
while(num<5):
print(b)
num+=1
结果:
0.417022004702574
0.417022004702574 # i=1和i=0使用的都是相同的seed(1),所以结果相同
0.417022004702574
0.417022004702574
0.417022004702574
from numpy import *
num=0
random.seed(1)
while(num<5):
b = random.random()
print(b)
num+=1
结果:
# 第一次执行
0.417022004702574
0.7203244934421581 # i=1对应的seed(1)值已失效,所以结果和i=0不同
0.00011437481734488664
0.30233257263183977
0.14675589081711304
# 第二次执行
0.417022004702574
0.7203244934421581
0.00011437481734488664
0.30233257263183977
0.14675589081711304
结论:设置的seed()值仅一次有效
六、总结
随机函数 分属模组 作用范围 参数取值 相同点
random.seed(seed) venv2 venv2模组内随机函数的随机操作
整形、浮点型、字符串。
1、seed值不同,生成的随机数不同
2、设置的seed()值仅一次有效
numpy.random.seed(seed) numpy numpy模组内随机函数的随机操作 整形(0~2**32-1)、一维整形数组。
tf.set_random_seed(seed) tensorflow tensorflow模组内随机函数的随机操作 整形
小小的函数有挺大的内涵。本文如果有描述不正确的地方还请大家多多指正,我们一起进步,谢谢。
参考:
https://www.cnblogs.com/subic/p/8454025.html
https://blog.csdn.net/linzch3/article/details/58220569
https://blog.csdn.net/lgh0824/article/details/93041465
https://blog.csdn.net/qq_31511955/article/details/81948902