TensorFlow学习(二):tf.random_normal() 和 tf.random_unform()

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: float16float32float64int32, or int64.
  • seed: A Python integer. Used to create a random seed for the distribution. See tf.set_random_seedfor 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  ]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烤粽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值