函数形式:
tf.random_normal(
shape,
mean=0.0,
stddev=1.0,
dtype=tf.float32,
seed=None,
name=None
)
参数:
- shape: 输出张量的形状,必选
- mean: 正态分布的均值,默认为0
- stddev: 正态分布的标准差,默认为1.0
- dtype: 输出的类型,默认为tf.float32
- seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
- name: 操作的名称
用途:用于从服从指定正态分布的数值中取出随机数。
代码示例:
# output1每次运行的结果不同
output1 = tf.Variable(tf.random_normal([2], stddev=0.1))
# seed设置数值,每次生成的结果相同
output2 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(output1)
print(output2)
print(sess.run(output1))
print(sess.run(output2))
运行结果:
<tf.Variable 'Variable_33:0' shape=(2,) dtype=float32_ref>
<tf.Variable 'Variable_34:0' shape=(2, 3) dtype=float32_ref>
[0.03875994 0.00234846]
[[-0.8113182 1.4845988 0.06532937]
[-2.4427042 0.0992484 0.5912243 ]]