tf.truncated_normal与tf.random_normal的详细用法
本文介绍了tf.truncated_normal与tf.random_normal的详细用法,分享给大家,具体如下:
tf.truncated_normal
tf.truncated_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
从截断的正态分布中输出随机值。
生成的值服从具有指定平均值和标准偏差的正态分布,如果生成的值大于平均值2个标准偏差的值则丢弃重新选择。
在正态分布的曲线中,横轴区间(μ-σ,μ+σ)内的面积为68.268949%。
横轴区间(μ-2σ,μ+2σ)内的面积为95.449974%。
横轴区间(μ-3σ,μ+3σ)内的面积为99.730020%。
X落在(μ-3σ,μ+3σ)以外的概率小于千分之三,在实际问题中常认为相应的事件是不会发生的,基本上可以把区间(μ-3σ,μ+3σ)看作是随机变量X实际可能的取值区间,这称之为正态分布的“3σ”原则。
在tf.truncated_normal中如果x的取值在区间(μ-2σ,μ+2σ)之外则重新进行选择。这样保证了生成的值都在均值附近。
参数:
shape:一维的张量,也是输出的张量。
mean:正态分布的均值。
stddev:正态分布的标准差。
dtype:输出的类型。
seed:一个整数,当设置之后,每次生成的随机数都一样。
name:操作的名字。
importtensorflowastf; importnumpyasnp; importmatplotlib.pyplotasplt; c=tf.truncated_normal(shape=[10,10],mean=0,stddev=1) withtf.Session()assess: printsess.run(c)
输出:
[[1.95758033-0.68666345-1.838603380.78213859-1.08119416-1.44530308
0.380353420.57904619-0.57145643-1.22899497]
[-0.758537950.482029741.034640431.19210851-0.157397180.8506189
1.18259966-0.99061841-0.519684491.38996458]
[1.05636907-0.026685290.641829310.4110294-0.4978295-0.64912242
1.27779591-0.015339930.47417602-1.28639436]
[-1.65927458-0.364887-0.455350280.078814-0.302957361.91779387
-0.66928798-0.148479150.918757140.61889237]
[-0.01308221-0.384682061.347000360.645317081.158994561.09932268
1.22457981-1.16103160.59036094-1.97302651]
[-0.248862130.828579370.090469890.392513220.21155456-0.27749416
0.188832010.08812679-0.329171030.20547724]
[0.053885070.454745650.233988061.32670367-0.019574060.52013856
-1.13907862-1.719578740.75772947-1.01719368]
[0.271559150.059004370.81448066-0.37997526-0.62020499-0.88820189
1.53407145-0.01600445-0.4236775-1.68852305]
[0.78942037-1.32458341-0.91667277-0.009637610.76824385-0.5405798
-0.73307443-1.19854116-0.661790730.26329204]
[0.59473759-0.37507254-1.21623695-1.305282591.18013096-1.32077384
-0.59241474-0.280631330.123411460.48480138]]
tf.random_normal
tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
从正态分布中输出随机值。
参数:
- shape:一维的张量,也是输出的张量。
- mean:正态分布的均值。
- stddev:正态分布的标准差。
- dtype:输出的类型。
- seed:一个整数,当设置之后,每次生成的随机数都一样。
- name:操作的名字。
代码
a=tf.Variable(tf.random_normal([2,2],seed=1)) b=tf.Variable(tf.truncated_normal([2,2],seed=2)) init=tf.global_variables_initializer() withtf.Session()assess: sess.run(init) print(sess.run(a)) print(sess.run(b))
输出:
[[-0.81131822 1.48459876]
[0.06532937-2.44270396]]
[[-0.85811085-0.19662298]
[0.13895047-1.22127688]]
指定seed之后,a的值不变,b的值也不变。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。