当前位置:网站首页>c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
2022-07-24 05:15:00 【小新很新】
随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习
神经网络代码中,随机产生函数几乎都会出现在最开头的位置,足以说明其重要性。
我们得保证,每次代码产生的随机数是相同的,要不然每次运行出的数据都是不同的,不利于我们的研究分析
1.种子是什么
了解种子是什么,首先得了解随机数产生机制。
在numpy中,seed函数的作用为 Seed the random number generator(为随机生成函数生成种子),而seed函数中的参数seed可以理解成为了获取每次产生随机数时的 “开始位置”的数值。
我们可以把seed看作一个映射函数的输入,输入不同的值,都会有唯一对应的数值( “开始位置”)。
至于随机数,是一些顺序已经固定的序列。
通过seed得到的 “开始位置”,就能在固定随机序列中找到位置,进行输出。
至于tensorflow中的seed和numpy相似,可以进行类比。
2.numpy.random.seed() 函数
random_sample() 和 random() 其实是同一个函数,都是随机生成[0,1)之间的均匀分布浮点数
seed() 函数上面提到,是对随机生成函数指定随机 “开始位置”
接下来,我们进行具体实验,来看看numpy.random.seed()的具体效果
import numpy as np
#numpy seed只限制一层
for k in range(3):
np.random.seed(123)
for i in range(5):
print( np.random.random() )
print('\n')
for k in range(3):
for i in range(5):
np.random.seed(123)
print( np.random.random() )
print('\n')
np.random.seed(123)
for k in range(3):
for i in range(5):
print( np.random.random() )
print('\n')
第一个循环结果:
三次相同的五个不同随机数

第二个循环结果:

三次输出完全相同五个随机数
第三个循环结果:
三次完全不同的五个随机数int

总结输出结果,numpy.random.seed()只是针对某一层进行约束,不是全局约束
原文链接:https://blog.csdn.net/qq_38024084/article/details/103465294
3.tf.random.set_seed:设置全局随机种子
依赖随机种子的操作实上是从两个种子派生的:全局种子和操作级别种子。这个设置了全局种子。
#tensorflow2.0
tf.random.set_seed(
seed
)
#tensorflow1.0
tf.set_random_seed(
seed
它与操作级种子的交互如下:
如果既未设置全局种子也未设置操作种子:此操作使用随机选择的种子。
如果设置了图级别的种子(graph-level seed),但未设置操作种子(operation seed):系统确定性地选择一个操作种子和图级别的种子,以便获得唯一的随机序列。在tensorflow和用户代码的相同版本中,此序列是确定性的。但是,在不同版本中,此序列可能会更改。如果代码依赖特定的种子来工作,则明确指定图级别和操作级别种子。
如果设置了操作种子,但未设置全局种子:使用默认的全局种子和指定的操作种子来确定随机序列。
如果同时设置了全局种子和操作种子:将两个种子一起使用以确定随机序列。
如果设置了全局种子,但未设置操作种子,则每次对随机操作的调用都会得到不同的结果,但是对于程序的每次重新运行,其结果都是相同的
如果既未设置全局种子也未设置操作种子,则每次调用随机操作和每次重新运行程序都会得到不同的结果
如果设置了操作种子,则每次对随机操作的调用都会得到不同的结果,但是对于程序的每次重新运行却得到相同的序列:
原文链接:https://blog.csdn.net/qq_36201400/article/details/108512368
边栏推荐
- 1、基于增量式生成遮挡与对抗抑制的行人再识别
- Bear market bottoming Guide
- On the dilemma faced by non transferable reputation points NFT SBTS
- OSS文件上传
- Binary SCA fingerprint extraction black Technology: go language Reverse Technology
- 13. Write a program, in which a user-defined function is used to judge whether an integer is a prime number. The main function inputs a number and outputs whether it is a prime number.
- NFS shared services
- Chapter5 foundation of deep learning
- The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance
- [advanced mathematics] the difference between differentiable and differentiable functions
猜你喜欢
![Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]](/img/c1/320e0349e2edda0eb420ed018aa831.png)
Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]

Reading excerpts from Liu run's "bottom logic"

1、基于增量式生成遮挡与对抗抑制的行人再识别

Binary SCA fingerprint extraction black Technology: go language Reverse Technology

Chapter5 foundation of deep learning

On the dilemma faced by non transferable reputation points NFT SBTS
![[database connection] - excerpt from training](/img/7d/334b4b7f0f6e1de228c4ecb1e600a3.png)
[database connection] - excerpt from training

JSP+Dao整合

Basic knowledge of MySQL database
![Knowledge record of College Physics C in advance in summer [update]](/img/c4/76b669c3229a365a5e2567f7fb824e.png)
Knowledge record of College Physics C in advance in summer [update]
随机推荐
The difference between statement and Preparedstatement and how to use placeholders
PostgreSQL: run PostgreSQL + pgadmin 4 in docker
I'm interested in reading efficient reading - the most cost-effective self investment
generator生成器,只生成两个方法
Pointer learning diary (III)
C table data De duplication
[basic 7] - exceptions, capture and customize exceptions
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
反射的介绍
FTP file transfer protocol
Support complex T4 file systems such as model group monitoring and real-time alarm. e
Knowledge record of College Physics C in advance in summer [update]
The fourth job: about the usage of cat, grep, cut, sort, uniq, VIM, TR and other commands
Reading excerpts from Liu run's "bottom logic"
Ia notes 2
Image to image translation with conditional advantageous networks paper notes
Drools development decision table
MGRE and OSPF comprehensive experiment
。单类型数字传感一个应用程.0。 Up- 开址在出厂
Performance test process