tf 随机数

时间:2021-05-11 17:10:37

tf生成随机数

import tensorflow as tf

sess = tf.InteractiveSession()

### 生成符合正态分布的随机值
# tf.random_normal(shape, mean, stddev, dtype, seed, name)
a = tf.random_normal([2, 3], name='a')
print(a.eval())
# [[-1.2077953 -0.69333565 -0.10252991]
# [ 0.51914424 0.7754795 -0.02618051]] ### 生成截断的正态分布的随机值
## 只保留[mean - 2stddev, mean + 2stddev]内的随机数
# tf.truncated_normal(shape, mean, stddev, dtype, seed, name)
b = tf.truncated_normal([2, 3], name='b')
print(b.eval())
# [[-1.8038174 1.521785 0.33182728]
# [ 1.0274183 -0.39916983 -0.50485927]] ### 生成均匀分布的随机数
# tf.random_uniform(shape, minval, maxval, dtype, seed, name)
c = tf.random_uniform([2, 3], name='c')
print(c.eval())
# [[0.6636964 0.20990396 0.44687605]
# [0.64548564 0.22155988 0.19247997]] ### 按行乱序
# tf.random_shuffle(value, dtype, name)
d = tf.random_shuffle([[1, 2], [3, 2]], name='d')
print(d.eval())