tf.random.truncated_normal()函数介绍
tf.random.truncated_normal(shape, mean, stddev, dtype=tf.float32, seed=None, name=None)
定义:截断的产生正态分布的随机数,即随机数与均值的差值若大于两倍的标准差,则重新生成。
- shape,生成张量的维度
- mean,均值
- stddev,标准差
- dtype,输出的类型 (可选)
- seed,一个整数,当设置之后,每次生成的随机数都一样 (可选)
- name,操作名称 (可选)
有关正态分布:
- 横轴区间(μ-σ,μ+σ)内的面积为68.268949%
- 横轴区间(μ-2σ,μ+2σ)内的面积为95.449974%
- 横轴区间(μ-3σ,μ+3σ)内的面积为99.730020%
~~对于tf.random.truncated_normal而言, 如果x的取值在区间(μ-2σ,μ+2σ)之外则重新进行选择。这样保证了生成的值都在均值附近。
应用举例:
#举例一
>> import tensorflow as tf
>> c = tf.random.truncated_normal(shape=[3, 2], mean=0, stddev=1)
>> print(c)
tf.Tensor(
[[0.68398845 0.33042762]
[0.32751635 0.07095545]
[0.7058672 0.567991 ]], shape=(3, 2), dtype=float32)
#举例二:
>> d = tf.random.truncated_normal(shape=[3, 2], mean=0, stddev=1,dtype=tf.float64, seed=2, name='aaa')
>> print(d)
tf.Tensor(
[[-1.2973865 0.06965108]
[ 1.2432732 -0.46400306]
[ 0.93317922 0.33969158]], shape=(3, 2), dtype=float64)
>> e = tf.random.truncated_normal(shape=[3, 2], mean=0, stddev=1,dtype=tf.float64, seed=2, name='bbb')
>> print(e)
tf.Tensor(
[[-1.2973865 0.06965108]
[ 1.2432732 -0.46400306]
[ 0.93317922 0.33969158]], shape=(3, 2), dtype=float64)
#可以看到,当固定了seed=2,多次操作得到的随机数仍然一样.
注:Tensorflow1.0版本中, 函数为 tf.truncated_normal