1. tf.random_normal() #正态分布
tf.random_normal(
shape,
mean=0.0,
stddev=1.0,
dtype=tf.float32,
seed=None,
name=None
)
Args:
shape
: A 1-D integer Tensor or Python array. The shape of the output tensor. #确定输出的张量形状mean
: A 0-D Tensor or Python value of type dtype
. The mean of the normal distribution. #均值stddev
: A 0-D Tensor or Python value of type dtype
. The standard deviation of the normal distribution.#正态分布的标准差dtype
: The type of the output. #输出的数据类型seed
: A Python integer. Used to create a random seed for the distribution. See tf.set_random_seed
for behavior. #随机种子name
: A name for the operation (optional). Returns:
A tensor of the specified shape filled with random normal values.
例子:
import tensorflow as tf
a = tf.random_normal([2,3])
with tf.Session() as sess:
print(sess.run(a))
print(sess.run(a))
[[ 0.08046694 2.1459296 -0.1951714 ]
[ 0.11219931 -0.05139215 0.6772044 ]]
[[-0.77863777 -1.0012143 0.64676356]
[-1.064799 -0.22006823 1.4889574 ]]
[[ 0.05100911 -1.1073893 -0.5293714 ]
[ 0.3451144 -0.27878246 0.20632097]]
[[ 0.22588727 0.3073472 -1.1917537 ]
[ 2.2639475 -0.748026 -1.7960579 ]]
设定随机种子 a=tf.random_normal([2,3],seed=12)
[[-0.43663138 0.8449775 -0.01180986]
[-0.8844008 -0.18527539 0.21195167]]
[[-1.587453 -1.2733358 1.568891 ]
[ 1.3985995 -0.2998891 0.6742214]]
[[-0.43663138 0.8449775 -0.01180986]
[-0.8844008 -0.18527539 0.21195167]]
[[-1.587453 -1.2733358 1.568891 ]
[ 1.3985995 -0.2998891 0.6742214]]
从上面的两次结果我们可以发现,第二次设定随机种子之后的程序输出的结果是相同的,第一次则完全不同。一般计算机产生的随机数都是伪随机数。随机数是由计算机依据随机种子,利用一定的算法计算而出,所以当随机种子确定,算法确定,产生的随机数便能确定。在调用随机数函数时,根据需要自行确定是否需要设定seed。
2. tf.random_uniform() #均匀分布
tf.random_uniform(
shape,
minval=0,
maxval=None,
dtype=tf.float32,
seed=None,
name=None
)
Args:
shape
: A 1-D integer Tensor or Python array. The shape of the output tensor.minval
: A 0-D Tensor or Python value of type dtype
. The lower bound on the range of random values to generate. Defaults to 0. #下限默认为“0”maxval
: A 0-D Tensor or Python value of type dtype
. The upper bound on the range of random values to generate. Defaults to 1 if dtype
is floating point. #上限浮点数类型默认为“1”dtype
: The type of the output: float16
, float32
, float64
, int32
, or int64
.seed
: A Python integer. Used to create a random seed for the distribution. See tf.set_random_seed
for behavior.name
: A name for the operation (optional).Returns:
A tensor of the specified shape filled with random uniform values.
Raises:
ValueError
: If dtype
is integral and maxval
is not specified.例子:
import tensorflow as tf
b = tf.random_uniform([2,3])
with tf.Session() as sess:
print(sess.run(b))
print(sess.run(b))
输出结果:
[[0.7068434 0.37068224 0.98492336]
[0.25193918 0.01160133 0.4997362 ]]
[[0.73202145 0.10125887 0.30887377]
[0.10988557 0.64894116 0.2683978 ]]
例子二:添加数据范围
import tensorflow as tf
b = tf.random_uniform([2,3],minval=-1,maxval=2)
with tf.Session() as sess:
print(sess.run(b))
print(sess.run(b))
输出结果:
[[-0.40992153 0.90792036 -0.38250148]
[ 1.6468053 1.5642762 0.7498528 ]]
[[ 0.398026 -0.02267563 -0.9902872 ]
[ 1.6546245 -0.7437657 1.838615 ]]